vendor/symfony/src/Symfony/Component/DomCrawler/Field/FileFormField.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.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 namespace Symfony\Component\DomCrawler\Field;
       
    13 
       
    14 /**
       
    15  * FileFormField represents a file form field (an HTML file input tag).
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  *
       
    19  * @api
       
    20  */
       
    21 class FileFormField extends FormField
       
    22 {
       
    23     /**
       
    24      * Sets the PHP error code associated with the field.
       
    25      *
       
    26      * @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)
       
    27      *
       
    28      * @throws \InvalidArgumentException When error code doesn't exist
       
    29      */
       
    30     public function setErrorCode($error)
       
    31     {
       
    32         $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);
       
    33         if (!in_array($error, $codes)) {
       
    34             throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
       
    35         }
       
    36 
       
    37         $this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
       
    38     }
       
    39 
       
    40     /**
       
    41      * Sets the value of the field.
       
    42      *
       
    43      * @param string $value The value of the field
       
    44      *
       
    45      * @api
       
    46      */
       
    47     public function upload($value)
       
    48     {
       
    49         $this->setValue($value);
       
    50     }
       
    51 
       
    52     /**
       
    53      * Sets the value of the field.
       
    54      *
       
    55      * @param string $value The value of the field
       
    56      */
       
    57     public function setValue($value)
       
    58     {
       
    59         if (null !== $value && is_readable($value)) {
       
    60             $error = UPLOAD_ERR_OK;
       
    61             $size = filesize($value);
       
    62             $name = basename($value);
       
    63 
       
    64             // copy to a tmp location
       
    65             $tmp = tempnam(sys_get_temp_dir(), 'upload');
       
    66             unlink($tmp);
       
    67             copy($value, $tmp);
       
    68             $value = $tmp;
       
    69         } else {
       
    70             $error = UPLOAD_ERR_NO_FILE;
       
    71             $size = 0;
       
    72             $name = '';
       
    73             $value = '';
       
    74         }
       
    75 
       
    76         $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
       
    77     }
       
    78 
       
    79     /**
       
    80      * Initializes the form field.
       
    81      *
       
    82      * @throws \LogicException When node type is incorrect
       
    83      */
       
    84     protected function initialize()
       
    85     {
       
    86         if ('input' != $this->node->nodeName) {
       
    87             throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
       
    88         }
       
    89 
       
    90         if ('file' != $this->node->getAttribute('type')) {
       
    91             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')));
       
    92         }
       
    93 
       
    94         $this->setValue(null);
       
    95     }
       
    96 }