Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DomCrawler\Field;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * FileFormField represents a file form field (an HTML file input tag).
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class FileFormField extends FormField
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Sets the PHP error code associated with the field.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @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)
|
Chris@0
|
25 *
|
Chris@0
|
26 * @throws \InvalidArgumentException When error code doesn't exist
|
Chris@0
|
27 */
|
Chris@0
|
28 public function setErrorCode($error)
|
Chris@0
|
29 {
|
Chris@17
|
30 $codes = [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];
|
Chris@17
|
31 if (!\in_array($error, $codes)) {
|
Chris@0
|
32 throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@17
|
35 $this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0];
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Sets the value of the field.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param string $value The value of the field
|
Chris@0
|
42 */
|
Chris@0
|
43 public function upload($value)
|
Chris@0
|
44 {
|
Chris@0
|
45 $this->setValue($value);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Sets the value of the field.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param string $value The value of the field
|
Chris@0
|
52 */
|
Chris@0
|
53 public function setValue($value)
|
Chris@0
|
54 {
|
Chris@0
|
55 if (null !== $value && is_readable($value)) {
|
Chris@0
|
56 $error = UPLOAD_ERR_OK;
|
Chris@0
|
57 $size = filesize($value);
|
Chris@0
|
58 $info = pathinfo($value);
|
Chris@0
|
59 $name = $info['basename'];
|
Chris@0
|
60
|
Chris@0
|
61 // copy to a tmp location
|
Chris@12
|
62 $tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_');
|
Chris@18
|
63 if (\array_key_exists('extension', $info)) {
|
Chris@0
|
64 $tmp .= '.'.$info['extension'];
|
Chris@0
|
65 }
|
Chris@0
|
66 if (is_file($tmp)) {
|
Chris@0
|
67 unlink($tmp);
|
Chris@0
|
68 }
|
Chris@0
|
69 copy($value, $tmp);
|
Chris@0
|
70 $value = $tmp;
|
Chris@0
|
71 } else {
|
Chris@0
|
72 $error = UPLOAD_ERR_NO_FILE;
|
Chris@0
|
73 $size = 0;
|
Chris@0
|
74 $name = '';
|
Chris@0
|
75 $value = '';
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@17
|
78 $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Sets path to the file as string for simulating HTTP request.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param string $path The path to the file
|
Chris@0
|
85 */
|
Chris@0
|
86 public function setFilePath($path)
|
Chris@0
|
87 {
|
Chris@0
|
88 parent::setValue($path);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Initializes the form field.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @throws \LogicException When node type is incorrect
|
Chris@0
|
95 */
|
Chris@0
|
96 protected function initialize()
|
Chris@0
|
97 {
|
Chris@0
|
98 if ('input' !== $this->node->nodeName) {
|
Chris@0
|
99 throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 if ('file' !== strtolower($this->node->getAttribute('type'))) {
|
Chris@0
|
103 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')));
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 $this->setValue(null);
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|