@@ 21-61 (lines=41) @@ | ||
18 | use Webmozart\Assert\Assert; |
|
19 | use Addiks\SymfonyGenerics\Arguments\ArgumentContextInterface; |
|
20 | ||
21 | final class AdditionalDataArgumentFactory implements ArgumentFactory |
|
22 | { |
|
23 | ||
24 | /** |
|
25 | * @var ArgumentContextInterface |
|
26 | */ |
|
27 | private $context; |
|
28 | ||
29 | public function __construct(ArgumentContextInterface $context) |
|
30 | { |
|
31 | $this->context = $context; |
|
32 | } |
|
33 | ||
34 | public function understandsString(string $source): bool |
|
35 | { |
|
36 | return strpos($source, '%') === 0 && strlen($source) > 1; |
|
37 | } |
|
38 | ||
39 | public function understandsArray(array $source): bool |
|
40 | { |
|
41 | return isset($source['key']) && isset($source['type']) && $source['type'] === 'additional-data'; |
|
42 | } |
|
43 | ||
44 | public function createArgumentFromString(string $source): Argument |
|
45 | { |
|
46 | Assert::startsWith($source, '%'); |
|
47 | ||
48 | /** @var string $key */ |
|
49 | $key = substr($source, 1); |
|
50 | ||
51 | return new AdditionalDataArgument($key, $this->context); |
|
52 | } |
|
53 | ||
54 | public function createArgumentFromArray(array $source): Argument |
|
55 | { |
|
56 | Assert::keyExists($source, 'key'); |
|
57 | ||
58 | return new AdditionalDataArgument($source['key'], $this->context); |
|
59 | } |
|
60 | ||
61 | } |
|
62 |
@@ 21-61 (lines=41) @@ | ||
18 | use Symfony\Component\HttpFoundation\RequestStack; |
|
19 | use Webmozart\Assert\Assert; |
|
20 | ||
21 | final class RequestArgumentFactory 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, '$') === 0 && strlen($source) > 1; |
|
37 | } |
|
38 | ||
39 | public function understandsArray(array $source): bool |
|
40 | { |
|
41 | return isset($source['key']) && isset($source['type']) && $source['type'] === 'request'; |
|
42 | } |
|
43 | ||
44 | public function createArgumentFromString(string $source): Argument |
|
45 | { |
|
46 | Assert::startsWith($source, "$"); |
|
47 | ||
48 | /** @var string $key */ |
|
49 | $key = substr($source, 1); |
|
50 | ||
51 | return new RequestArgument($this->requestStack, $key); |
|
52 | } |
|
53 | ||
54 | public function createArgumentFromArray(array $source): Argument |
|
55 | { |
|
56 | Assert::keyExists($source, 'key'); |
|
57 | ||
58 | return new RequestArgument($this->requestStack, $source['key']); |
|
59 | } |
|
60 | ||
61 | } |
|
62 |