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