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\metadata\ClassMetadata; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Array class resolver class. |
15
|
|
|
* @author Vitaly Iegorov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class CollectionClassResolver implements CollectionResolverInterface |
18
|
|
|
{ |
19
|
|
|
/** Collection class key */ |
20
|
|
|
const KEY = 'instance'; |
21
|
|
|
|
22
|
|
|
/** @var array Collection of collection configurators */ |
23
|
|
|
protected $collectionConfigurators = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ArrayClassResolver constructor. |
27
|
|
|
* |
28
|
|
|
* @param string[] $collectionConfigurators |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $collectionConfigurators) |
33
|
|
|
{ |
34
|
|
|
/** @var string $collectionConfigurator */ |
35
|
|
|
foreach ($collectionConfigurators as $collectionConfigurator) { |
36
|
|
|
// Autoload and check if passed collection configurator |
37
|
|
|
if (in_array(CollectionAttributeConfiguratorInterface::class, class_implements($collectionConfigurator), true)) { |
38
|
|
|
$this->collectionConfigurators[$this->getKey($collectionConfigurator)] = $collectionConfigurator; |
39
|
|
|
} else { |
40
|
|
|
throw new \InvalidArgumentException($collectionConfigurator . ' is not valid collection configurator or does not exists'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get collection configurator collection key name for resolving. |
47
|
|
|
* |
48
|
|
|
* @param string $className Full collection configurator class name with namespace |
49
|
|
|
* |
50
|
|
|
* @return string Collection configurator collection key name |
51
|
|
|
*/ |
52
|
|
|
public function getKey($className) : string |
53
|
|
|
{ |
54
|
|
|
// Get collection configurator key as its lowered class name |
55
|
|
|
return strtolower(substr($className, strrpos($className, '\\') + 1)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function resolve(array $classDataArray, ClassMetadata $classMetadata) |
62
|
|
|
{ |
63
|
|
|
// Iterate only supported collection key |
64
|
|
|
if (array_key_exists(self::KEY, $classDataArray)) { |
65
|
|
|
// Iterate collection |
66
|
|
|
if (array_key_exists('@attributes', $classDataArray[self::KEY])) { |
67
|
|
|
// Iterate collection attribute configurators |
68
|
|
|
foreach ($this->collectionConfigurators as $key => $collectionConfigurator) { |
69
|
|
|
// If this is supported collection configurator |
70
|
|
|
if (array_key_exists($key, $classDataArray[self::KEY]['@attributes'])) { |
71
|
|
|
/** @var ClassConfiguratorInterface $configurator Create instance */ |
72
|
|
|
$configurator = new $collectionConfigurator($classDataArray[self::KEY]['@attributes'][$key]); |
73
|
|
|
// Fill in class metadata |
74
|
|
|
$configurator->toClassMetadata($classMetadata); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $classMetadata; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function resolveArrayKeys() |
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|