|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory; |
|
14
|
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
|
16
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
|
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\RequestFileArgument; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
19
|
|
|
use Webmozart\Assert\Assert; |
|
20
|
|
|
|
|
21
|
|
|
final class RequestFileArgumentFactory implements ArgumentFactory |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var RequestStack |
|
26
|
|
|
*/ |
|
27
|
|
|
private $requestStack; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(RequestStack $requestStack) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->requestStack = $requestStack; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function understandsString(string $source): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return strpos($source, '$files.') === 0 && strlen($source) > 7; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function understandsArray(array $source): bool |
|
40
|
|
|
{ |
|
41
|
|
|
return isset($source['key']) && isset($source['type']) && $source['type'] === 'request-file'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function createArgumentFromString(string $source): Argument |
|
45
|
|
|
{ |
|
46
|
|
|
Assert::startsWith($source, '$files.'); |
|
47
|
|
|
|
|
48
|
|
|
[, $key, $property] = explode(".", $source); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
return new RequestFileArgument($this->requestStack, $key, $property); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function createArgumentFromArray(array $source): Argument |
|
54
|
|
|
{ |
|
55
|
|
|
Assert::keyExists($source, 'key'); |
|
56
|
|
|
|
|
57
|
|
|
/** @var string $property */ |
|
58
|
|
|
$property = 'content'; |
|
59
|
|
|
|
|
60
|
|
|
if (isset($source['property'])) { |
|
61
|
|
|
$property = $source['property']; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new RequestFileArgument($this->requestStack, $source['key'], $property); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.