1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sirius\Upload\Result; |
5
|
|
|
|
6
|
|
|
use Sirius\Upload\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class File implements ResultInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Array containing the details of the uploaded file: |
13
|
|
|
* - name (uploaded name) |
14
|
|
|
* - original name |
15
|
|
|
* - tmp_name |
16
|
|
|
* etc |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $file; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The container to which this file belongs to |
24
|
|
|
* @var ContainerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $file |
30
|
|
|
* @param ContainerInterface $container |
31
|
11 |
|
*/ |
32
|
|
|
public function __construct($file, ContainerInterface $container) |
33
|
11 |
|
{ |
34
|
11 |
|
$this->file = $file; |
35
|
11 |
|
$this->container = $container; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns if the uploaded file is valid |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
1 |
|
*/ |
43
|
|
|
public function isValid():bool |
44
|
1 |
|
{ |
45
|
|
|
return $this->file['name'] && count($this->getMessages()) === 0; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the validation error messages |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
3 |
|
*/ |
53
|
|
|
public function getMessages():array |
54
|
3 |
|
{ |
55
|
2 |
|
if (isset($this->file['messages'])) { |
56
|
|
|
return $this->file['messages']; |
57
|
1 |
|
} else { |
58
|
|
|
return []; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The file that was saved during process() and has a .lock file attached |
64
|
|
|
* will be cleared, in case the form processing fails |
65
|
2 |
|
*/ |
66
|
|
|
public function clear() |
67
|
2 |
|
{ |
68
|
2 |
|
$this->container->delete($this->name); |
|
|
|
|
69
|
2 |
|
$this->container->delete($this->name . '.lock'); |
|
|
|
|
70
|
2 |
|
$this->file['name'] = null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove the .lock file attached to the file that was saved during process() |
75
|
|
|
* This should happen if the form fails validation/processing |
76
|
3 |
|
*/ |
77
|
|
|
public function confirm() |
78
|
3 |
|
{ |
79
|
3 |
|
$this->container->delete($this->name . '.lock'); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* File attribute getter |
84
|
|
|
* |
85
|
|
|
* @param $name |
86
|
|
|
* @return mixed |
87
|
8 |
|
*/ |
88
|
|
|
public function __get($name) |
89
|
8 |
|
{ |
90
|
7 |
|
if (isset($this->file[$name])) { |
91
|
|
|
return $this->file[$name]; |
92
|
|
|
} |
93
|
2 |
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.