|
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\ServiceArgument; |
|
18
|
|
|
use Psr\Container\ContainerInterface; |
|
19
|
|
|
use Webmozart\Assert\Assert; |
|
20
|
|
|
use Addiks\SymfonyGenerics\Arguments\LiteralArgument; |
|
21
|
|
|
|
|
22
|
|
|
final class ServiceArgumentFactory implements ArgumentFactory |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ContainerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $container; |
|
29
|
|
|
|
|
30
|
14 |
|
public function __construct(ContainerInterface $container) |
|
31
|
|
|
{ |
|
32
|
14 |
|
$this->container = $container; |
|
33
|
14 |
|
} |
|
34
|
|
|
|
|
35
|
7 |
|
public function understandsString(string $source): bool |
|
36
|
|
|
{ |
|
37
|
7 |
|
return strpos($source, '@') === 0 && strlen($source) > 1; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
7 |
|
public function understandsArray(array $source): bool |
|
41
|
|
|
{ |
|
42
|
7 |
|
return isset($source['service-id']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function createArgumentFromString(string $source): Argument |
|
46
|
|
|
{ |
|
47
|
3 |
|
Assert::true($this->understandsString($source)); |
|
48
|
|
|
|
|
49
|
|
|
/** @var string $serviceId */ |
|
50
|
2 |
|
$serviceId = substr($source, 1); |
|
51
|
|
|
|
|
52
|
2 |
|
return new ServiceArgument($this->container, new LiteralArgument($serviceId)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
public function createArgumentFromArray(array $source): Argument |
|
56
|
|
|
{ |
|
57
|
4 |
|
Assert::true($this->understandsArray($source)); |
|
58
|
|
|
|
|
59
|
2 |
|
return new ServiceArgument($this->container, new LiteralArgument($source['service-id'])); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|