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
|
|
|
/** |
11
|
|
|
* Abstract configurator resolver class. |
12
|
|
|
* |
13
|
|
|
* @author Vitaly Iegorov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractCollectionResolver |
16
|
|
|
{ |
17
|
|
|
/** @var array Collection of collection configurators */ |
18
|
|
|
protected $configurators = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ArrayPropertyResolver constructor. |
22
|
|
|
* |
23
|
|
|
* @param array $collectionConfigurators |
24
|
|
|
* |
25
|
|
|
* @throws \InvalidArgumentException |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(array $collectionConfigurators) |
28
|
|
|
{ |
29
|
|
|
/** @var string $collectionConfigurator */ |
30
|
1 |
|
foreach ($collectionConfigurators as $collectionConfigurator) { |
31
|
|
|
// Autoload and check if passed collection configurator |
32
|
1 |
|
if (in_array(CollectionAttributeConfiguratorInterface::class, class_implements($collectionConfigurator), true)) { |
33
|
1 |
|
$this->configurators[$this->getKey($collectionConfigurator)] = $collectionConfigurator; |
34
|
|
|
} else { |
35
|
1 |
|
throw new \InvalidArgumentException($collectionConfigurator . ' is not valid collection configurator or does not exists'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get collection configurator collection key name for resolving. |
42
|
|
|
* |
43
|
|
|
* @param string $className Full collection configurator class name with namespace |
44
|
|
|
* |
45
|
|
|
* @return string Collection configurator collection key name |
46
|
|
|
*/ |
47
|
1 |
|
public function getKey($className) : string |
48
|
|
|
{ |
49
|
1 |
|
$reflection = new \ReflectionClass($className); |
50
|
1 |
|
if ($key = $reflection->getConstant('CONFIGURATOR_KEY')) { |
51
|
1 |
|
return $key; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Get collection configurator key as its lowered class name |
55
|
1 |
|
return strtolower(substr($className, strrpos($className, '\\') + 1)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Try to find attribute configurators. |
60
|
|
|
* |
61
|
|
|
* @param array $arrayData Configuration data array |
62
|
|
|
* |
63
|
|
|
* @return CollectionAttributeConfiguratorInterface[] Found attribute configurator instances collection |
64
|
|
|
*/ |
65
|
1 |
|
public function getAttributeConfigurator(array $arrayData) : array |
66
|
|
|
{ |
67
|
1 |
|
$configurators = []; |
68
|
|
|
|
69
|
|
|
// If we have @attributes section |
70
|
1 |
|
if (array_key_exists('@attributes', $arrayData)) { |
71
|
|
|
// Iterate collection attribute configurators |
72
|
1 |
|
foreach ($this->configurators as $key => $configurator) { |
73
|
|
|
// If this is supported collection configurator |
74
|
1 |
|
if (array_key_exists($key, $arrayData['@attributes'])) { |
75
|
|
|
// Store new attribute configurator instance |
76
|
1 |
|
$configurators[$key] = new $configurator($arrayData['@attributes'][$key]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $configurators; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|