scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 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 | class FileFormField extends FormField |
||
20 | { |
||
21 | /** |
||
22 | * Sets the PHP error code associated with the field. |
||
23 | * |
||
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) |
||
25 | * |
||
26 | * @throws \InvalidArgumentException When error code doesn't exist |
||
27 | */ |
||
28 | public function setErrorCode($error) |
||
29 | { |
||
30 | $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); |
||
31 | if (!in_array($error, $codes)) { |
||
32 | throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error)); |
||
33 | } |
||
34 | |||
35 | $this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Sets the value of the field. |
||
40 | * |
||
41 | * @param string $value The value of the field |
||
42 | */ |
||
43 | public function upload($value) |
||
44 | { |
||
45 | $this->setValue($value); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Sets the value of the field. |
||
50 | * |
||
51 | * @param string $value The value of the field |
||
52 | */ |
||
53 | public function setValue($value) |
||
54 | { |
||
55 | if (null !== $value && is_readable($value)) { |
||
56 | $error = UPLOAD_ERR_OK; |
||
57 | $size = filesize($value); |
||
58 | $info = pathinfo($value); |
||
59 | $name = $info['basename']; |
||
60 | |||
61 | // copy to a tmp location |
||
62 | $tmp = sys_get_temp_dir().'/'.sha1(uniqid(mt_rand(), true)); |
||
63 | if (array_key_exists('extension', $info)) { |
||
64 | $tmp .= '.'.$info['extension']; |
||
65 | } |
||
66 | if (is_file($tmp)) { |
||
67 | unlink($tmp); |
||
68 | } |
||
69 | copy($value, $tmp); |
||
70 | $value = $tmp; |
||
71 | } else { |
||
72 | $error = UPLOAD_ERR_NO_FILE; |
||
73 | $size = 0; |
||
74 | $name = ''; |
||
75 | $value = ''; |
||
76 | } |
||
77 | |||
78 | $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Sets path to the file as string for simulating HTTP request. |
||
83 | * |
||
84 | * @param string $path The path to the file |
||
85 | */ |
||
86 | public function setFilePath($path) |
||
87 | { |
||
88 | parent::setValue($path); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Initializes the form field. |
||
93 | * |
||
94 | * @throws \LogicException When node type is incorrect |
||
95 | */ |
||
96 | protected function initialize() |
||
97 | { |
||
98 | if ('input' !== $this->node->nodeName) { |
||
99 | throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName)); |
||
100 | } |
||
101 | |||
102 | if ('file' !== strtolower($this->node->getAttribute('type'))) { |
||
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'))); |
||
104 | } |
||
105 | |||
106 | $this->setValue(null); |
||
107 | } |
||
108 | } |