-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectorFactory.php
153 lines (130 loc) · 3.83 KB
/
SelectorFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Selector;
/**
* Selector Factory Class.
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
class SelectorFactory implements SelectorFactoryInterface
{
/**
* @var \ReflectionClass[]
*/
private $selectorClass;
/**
* @var string[]
*/
private $namespaces;
/**
* @param string $defaultNamespace
*/
public function __construct($defaultNamespace = null)
{
$this->selectorClass = array();
$this->namespaces = array();
if (!empty($defaultNamespace)) {
$this->addNamespace($defaultNamespace);
}
}
/**
* {@inheritdoc}
*/
public function addSelector($selectorClass, $selectorName = null)
{
$reflection = $this->validateSelectorClass($selectorClass);
if (empty($selectorName)) {
$selectorName = \lcfirst($reflection->getShortName());
}
if ($this->findSelector($selectorName) !== null) {
throw new \InvalidArgumentException(\sprintf('There is already a selector with name %s', $selectorName));
}
$this->selectorClass[$selectorName] = $reflection;
}
/**
* {@inheritdoc}
*/
public function addNamespace($namespace)
{
if (empty($namespace)) {
throw new \InvalidArgumentException('The namespace name must be non-empty');
}
if (!isset($this->namespaces[$namespace])) {
$this->namespaces[] = $namespace;
}
}
/**
* {@inheritdoc}
*/
public function create($selectorName, array $arguments = array())
{
$reflection = $this->findSelector($selectorName);
if ($reflection !== null) {
return $reflection->newInstanceArgs($arguments);
}
throw new \InvalidArgumentException(\sprintf('There is not a selector with name "%s"', $selectorName));
}
/**
* @param string $selectorName
*
* @return \ReflectionClass
*/
private function findSelector($selectorName)
{
if (isset($this->selectorClass[$selectorName])) {
return $this->selectorClass[$selectorName];
}
foreach ($this->namespaces as $namespace) {
$className = $namespace.'\\'.\ucfirst($selectorName);
$reflection = $this->validateSelectorClass($className, false);
if ($reflection !== null) {
$this->selectorClass[$selectorName] = $reflection;
return $reflection;
}
}
return;
}
/**
* @param string $selectorClass
* @param bool $strict
*/
private function validateSelectorClass($selectorClass, $strict = true)
{
if (!\class_exists($selectorClass)) {
return $this->nullOrException(
new \InvalidArgumentException(\sprintf('There is not a class with name %s', $selectorClass)),
$strict
);
}
$reflection = new \ReflectionClass($selectorClass);
if (!$reflection->isSubclassOf(SelectorInterface::class)) {
return $this->nullOrException(
new \InvalidArgumentException(
\sprintf('%s must be implement %s interface', $selectorClass, SelectorInterface::class)
),
$strict
);
}
return $reflection;
}
/**
* @param \Exception $exception
* @param bool $strict
*
* @throws \Exception
*/
private function nullOrException(\Exception $exception, $strict = true)
{
if ($strict) {
throw $exception;
}
return;
}
}