Completed
Push — master ( 821e5b...8cc443 )
by Vitaly
24s
created

AbstractCollectionResolver::getKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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