friendship ended with social-app. php is my new best friend
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
12namespace 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 */
19class 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(int $error): void
29 {
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];
31 if (!\in_array($error, $codes)) {
32 throw new \InvalidArgumentException(\sprintf('The error code "%s" is not valid.', $error));
33 }
34
35 $this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0];
36 }
37
38 /**
39 * Sets the value of the field.
40 */
41 public function upload(?string $value): void
42 {
43 $this->setValue($value);
44 }
45
46 /**
47 * Sets the value of the field.
48 */
49 public function setValue(?string $value): void
50 {
51 if (null !== $value && is_readable($value)) {
52 $error = \UPLOAD_ERR_OK;
53 $size = filesize($value);
54 $info = pathinfo($value);
55 $name = $info['basename'];
56
57 // copy to a tmp location
58 $tmp = tempnam(sys_get_temp_dir(), $name);
59 if (\array_key_exists('extension', $info)) {
60 unlink($tmp);
61 $tmp .= '.'.$info['extension'];
62 }
63 if (is_file($tmp)) {
64 unlink($tmp);
65 }
66 copy($value, $tmp);
67 $value = $tmp;
68 } else {
69 $error = \UPLOAD_ERR_NO_FILE;
70 $size = 0;
71 $name = '';
72 $value = '';
73 }
74
75 $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
76 }
77
78 /**
79 * Sets path to the file as string for simulating HTTP request.
80 */
81 public function setFilePath(string $path): void
82 {
83 parent::setValue($path);
84 }
85
86 /**
87 * Initializes the form field.
88 *
89 * @throws \LogicException When node type is incorrect
90 */
91 protected function initialize(): void
92 {
93 if ('input' !== $this->node->nodeName) {
94 throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
95 }
96
97 if ('file' !== strtolower($this->node->getAttribute('type'))) {
98 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')));
99 }
100
101 $this->setValue(null);
102 }
103}