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 Webmozart\Assert\Assert; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
18
|
|
|
|
19
|
|
|
final class ArgumentFactoryAggregate implements ArgumentFactory |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array<ArgumentFactory> |
24
|
|
|
*/ |
25
|
|
|
private $innerArgumentFactories; |
26
|
|
|
|
27
|
9 |
|
public function __construct(array $innerArgumentFactories) |
28
|
|
|
{ |
29
|
9 |
|
Assert::null($this->innerArgumentFactories); |
30
|
9 |
|
$this->innerArgumentFactories = array(); |
31
|
|
|
|
32
|
9 |
|
foreach ($innerArgumentFactories as $innerArgumentFactory) { |
33
|
|
|
/** @var ArgumentFactory $innerArgumentFactory */ |
34
|
|
|
|
35
|
9 |
|
Assert::isInstanceOf($innerArgumentFactory, ArgumentFactory::class); |
36
|
|
|
|
37
|
9 |
|
$this->innerArgumentFactories[] = $innerArgumentFactory; |
38
|
|
|
} |
39
|
9 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function understandsString(string $source): bool |
42
|
|
|
{ |
43
|
|
|
/** @var bool $understandsString */ |
44
|
4 |
|
$understandsString = false; |
45
|
|
|
|
46
|
4 |
|
foreach ($this->innerArgumentFactories as $innerArgumentFactory) { |
47
|
|
|
/** @var ArgumentFactory $innerArgumentFactory */ |
48
|
|
|
|
49
|
4 |
|
$understandsString = $innerArgumentFactory->understandsString($source); |
50
|
|
|
|
51
|
4 |
|
if ($understandsString) { |
52
|
4 |
|
break; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return $understandsString; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
public function understandsArray(array $source): bool |
60
|
|
|
{ |
61
|
|
|
/** @var bool $understandsArray */ |
62
|
4 |
|
$understandsArray = false; |
63
|
|
|
|
64
|
4 |
|
foreach ($this->innerArgumentFactories as $innerArgumentFactory) { |
65
|
|
|
/** @var ArgumentFactory $innerArgumentFactory */ |
66
|
|
|
|
67
|
4 |
|
$understandsArray = $innerArgumentFactory->understandsArray($source); |
68
|
|
|
|
69
|
4 |
|
if ($understandsArray) { |
70
|
4 |
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
return $understandsArray; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function createArgumentFromString(string $source): Argument |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
/** @var Argument $argument */ |
80
|
|
|
$argument = null; |
81
|
|
|
|
82
|
|
|
foreach ($this->innerArgumentFactories as $innerArgumentFactory) { |
83
|
|
|
/** @var ArgumentFactory $innerArgumentFactory */ |
84
|
|
|
|
85
|
|
|
if ($innerArgumentFactory->understandsString($source)) { |
86
|
|
|
$argument = $innerArgumentFactory->createArgumentFromString($source); |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
Assert::isInstanceOf($argument, Argument::class, sprintf( |
92
|
|
|
"Could not parse '%s' into argument!", |
93
|
|
|
$source |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
return $argument; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function createArgumentFromArray(array $source): Argument |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
/** @var Argument $argument */ |
102
|
|
|
$argument = null; |
103
|
|
|
|
104
|
|
|
foreach ($this->innerArgumentFactories as $innerArgumentFactory) { |
105
|
|
|
/** @var ArgumentFactory $innerArgumentFactory */ |
106
|
|
|
|
107
|
|
|
if ($innerArgumentFactory->understandsArray($source)) { |
108
|
|
|
$argument = $innerArgumentFactory->createArgumentFromArray($source); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
Assert::isInstanceOf($argument, Argument::class, "Could not parse array into argument!"); |
114
|
|
|
|
115
|
|
|
return $argument; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.