1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: root |
5
|
|
|
* Date: 29.07.2016 |
6
|
|
|
* Time: 21:38. |
7
|
|
|
*/ |
8
|
|
|
namespace samsonframework\container\collection; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\configurator\ClassConfiguratorInterface; |
11
|
|
|
use samsonframework\container\configurator\PropertyConfiguratorInterface; |
12
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
13
|
|
|
use samsonframework\container\metadata\PropertyMetadata; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract configurator resolver class. |
17
|
|
|
* @author Vitaly Iegorov <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractCollectionResolver |
20
|
|
|
{ |
21
|
|
|
/** @var array Collection of collection configurators */ |
22
|
|
|
protected $collectionConfigurators = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ArrayPropertyResolver constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $collectionConfigurators |
28
|
|
|
* |
29
|
|
|
* @throws \InvalidArgumentException |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct(array $collectionConfigurators) |
32
|
|
|
{ |
33
|
|
|
/** @var string $collectionConfigurator */ |
34
|
1 |
|
foreach ($collectionConfigurators as $collectionConfigurator) { |
35
|
|
|
// Autoload and check if passed collection configurator |
36
|
1 |
|
if (in_array(CollectionAttributeConfiguratorInterface::class, class_implements($collectionConfigurator), true)) { |
37
|
1 |
|
$this->collectionConfigurators[$this->getKey($collectionConfigurator)] = $collectionConfigurator; |
38
|
|
|
} else { |
39
|
1 |
|
throw new \InvalidArgumentException($collectionConfigurator . ' is not valid collection configurator or does not exists'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get collection configurator collection key name for resolving. |
46
|
|
|
* |
47
|
|
|
* @param string $className Full collection configurator class name with namespace |
48
|
|
|
* |
49
|
|
|
* @return string Collection configurator collection key name |
50
|
|
|
*/ |
51
|
1 |
|
public function getKey($className) : string |
52
|
|
|
{ |
53
|
1 |
|
$reflection = new \ReflectionClass($className); |
54
|
1 |
|
if ($key = $reflection->getConstant('CONFIGURATOR_KEY')) { |
55
|
1 |
|
return $key; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Get collection configurator key as its lowered class name |
59
|
1 |
|
return strtolower(substr($className, strrpos($className, '\\') + 1)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|