FileFormField.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Symfony\Components\DomCrawler\Field;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * FileFormField represents a file form field (an HTML file input tag).
  13. *
  14. * @package Symfony
  15. * @subpackage Components_DomCrawler
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class FileFormField extends FormField
  19. {
  20. /**
  21. * Sets the PHP error code associated with the field.
  22. *
  23. * @param integer $error The error code (one of UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, or UPLOAD_ERR_EXTENSION)
  24. *
  25. * @throws \InvalidArgumentException When error code doesn't exist
  26. */
  27. public function setErrorCode($error)
  28. {
  29. $codes = array(UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION);
  30. if (!in_array($error, $codes)) {
  31. throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
  32. }
  33. $this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
  34. }
  35. /**
  36. * Sets the value of the field.
  37. *
  38. * @param string $value The value of the field
  39. */
  40. public function upload($value)
  41. {
  42. $this->setValue($value);
  43. }
  44. /**
  45. * Sets the value of the field.
  46. *
  47. * @param string $value The value of the field
  48. */
  49. public function setValue($value)
  50. {
  51. if (null !== $value && is_readable($value)) {
  52. $error = UPLOAD_ERR_OK;
  53. $size = filesize($value);
  54. } else {
  55. $error = UPLOAD_ERR_NO_FILE;
  56. $size = 0;
  57. $value = '';
  58. }
  59. $this->value = array('name' => basename($value), 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
  60. }
  61. /**
  62. * Initializes the form field.
  63. *
  64. * @throws \LogicException When node type is incorrect
  65. */
  66. protected function initialize()
  67. {
  68. if ('input' != $this->node->nodeName) {
  69. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
  70. }
  71. if ('file' != $this->node->getAttribute('type')) {
  72. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $this->node->getAttribute('type')));
  73. }
  74. $this->setValue(null);
  75. }
  76. }