Passed
Push — master ( ed53b1...556216 )
by Thorsten
03:33
created

AttributeMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/entity project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Entity\Entity;
12
13
use Daikon\DataStructure\TypedMapTrait;
14
15
final class AttributeMap implements \IteratorAggregate, \Countable
16
{
17
    use TypedMapTrait;
18
19
    public function __construct(array $attributes = [])
20
    {
21 10
        $this->init(array_reduce($attributes, function (array $carry, AttributeInterface $attribute) {
22 10
            $carry[$attribute->getName()] = $attribute; // enforce consistent attribute keys
23 10
            return $carry;
24 10
        }, []), AttributeInterface::class);
25 10
    }
26
27
    public function byClassNames(array $classNames = []): self
28
    {
29
        $clonedMap = clone $this;
30
        (function (string ...$classNames) use ($clonedMap): void {
31
            $clonedMap->compositeMap = $clonedMap->compositeMap->filter(
32
                function (string $name, AttributeInterface $attribute) use ($classNames): bool {
33
                    return in_array(get_class($attribute), $classNames);
34
                }
35
            );
36
        })(...$classNames);
37
        return $clonedMap;
38
    }
39
}
40