You are here

class FileFormField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/dom-crawler/Field/FileFormField.php \Symfony\Component\DomCrawler\Field\FileFormField

FileFormField represents a file form field (an HTML file input tag).

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of FileFormField

2 files declare their use of FileFormField
BrowserKitDriver.php in vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php
FileFormFieldTest.php in vendor/symfony/dom-crawler/Tests/Field/FileFormFieldTest.php
1 string reference to 'FileFormField'
FormTest::provideInitializeValues in vendor/symfony/dom-crawler/Tests/FormTest.php

File

vendor/symfony/dom-crawler/Field/FileFormField.php, line 19

Namespace

Symfony\Component\DomCrawler\Field
View source
class FileFormField extends FormField {

  /**
   * Sets the PHP error code associated with the field.
   *
   * @param int $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)
   *
   * @throws \InvalidArgumentException When error code doesn't exist
   */
  public function setErrorCode($error) {
    $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,
    );
    if (!in_array($error, $codes)) {
      throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
    }
    $this->value = array(
      'name' => '',
      'type' => '',
      'tmp_name' => '',
      'error' => $error,
      'size' => 0,
    );
  }

  /**
   * Sets the value of the field.
   *
   * @param string $value The value of the field
   */
  public function upload($value) {
    $this
      ->setValue($value);
  }

  /**
   * Sets the value of the field.
   *
   * @param string $value The value of the field
   */
  public function setValue($value) {
    if (null !== $value && is_readable($value)) {
      $error = UPLOAD_ERR_OK;
      $size = filesize($value);
      $info = pathinfo($value);
      $name = $info['basename'];

      // copy to a tmp location
      $tmp = sys_get_temp_dir() . '/' . sha1(uniqid(mt_rand(), true));
      if (array_key_exists('extension', $info)) {
        $tmp .= '.' . $info['extension'];
      }
      if (is_file($tmp)) {
        unlink($tmp);
      }
      copy($value, $tmp);
      $value = $tmp;
    }
    else {
      $error = UPLOAD_ERR_NO_FILE;
      $size = 0;
      $name = '';
      $value = '';
    }
    $this->value = array(
      'name' => $name,
      'type' => '',
      'tmp_name' => $value,
      'error' => $error,
      'size' => $size,
    );
  }

  /**
   * Sets path to the file as string for simulating HTTP request.
   *
   * @param string $path The path to the file
   */
  public function setFilePath($path) {
    parent::setValue($path);
  }

  /**
   * Initializes the form field.
   *
   * @throws \LogicException When node type is incorrect
   */
  protected function initialize() {
    if ('input' !== $this->node->nodeName) {
      throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
    }
    if ('file' !== strtolower($this->node
      ->getAttribute('type'))) {
      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')));
    }
    $this
      ->setValue(null);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileFormField::initialize protected function Initializes the form field. Overrides FormField::initialize
FileFormField::setErrorCode public function Sets the PHP error code associated with the field.
FileFormField::setFilePath public function Sets path to the file as string for simulating HTTP request.
FileFormField::setValue public function Sets the value of the field. Overrides FormField::setValue
FileFormField::upload public function Sets the value of the field.
FormField::$disabled protected property
FormField::$document protected property
FormField::$name protected property
FormField::$node protected property
FormField::$value protected property
FormField::$xpath protected property
FormField::getName public function Returns the name of the field.
FormField::getValue public function Gets the value of the field.
FormField::hasValue public function Returns true if the field should be included in the submitted values. 1
FormField::isDisabled public function Check if the current field is disabled. 1
FormField::__construct public function Constructor.