Passed
Push — master ( 5613e1...3c05f7 )
by Mr
02:15
created

ValueObjectCollectionTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/value-object 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
namespace Daikon\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\SupportsAnnotations;
0 ignored issues
show
Bug introduced by
The type Daikon\Interop\SupportsAnnotations was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * @type(Daikon\ValueObject\ValueObjectInterface)
16
 */
17
trait ValueObjectCollectionTrait
18
{
19
    use SupportsAnnotations;
20
21
    /** @return static */
22
    public static function makeEmpty(): self
23
    {
24
        return new static;
25
    }
26
27
    public function isEmpty(): bool
28
    {
29
        return count($this) === 0;
30
    }
31
32
    /** @param static $comparator */
33 4
    public function equals($comparator): bool
34
    {
35 4
        $this->assertInitialized();
36 4
        Assertion::isInstanceOf($comparator, static::class);
37
38
        /** @var ValueObjectInterface $object */
39 4
        foreach ($this as $key => $object) {
40 4
            $comparison = $comparator->get($key, null);
41 4
            if (!$comparison || !$object->equals($comparison)) {
42 2
                return false;
43
            }
44
        }
45
46 2
        return true;
47
    }
48
49
    /**
50
     * @param null|iterable $state
51
     * @return static
52
     */
53 1
    public static function fromNative($state): self
54
    {
55 1
        Assertion::nullOrIsTraversable($state, 'State provided to '.static::class.' must be null or iterable.');
56
        // Override fromNative() to support multiple types, currently first seen type factory is used.
57 1
        $typeFactory = current(static::inferTypeFactories());
58 1
        Assertion::isCallable($typeFactory, 'No valid type factory specified.');
59 1
        $objects = [];
60 1
        if (!is_null($state)) {
61 1
            foreach ($state as $key => $data) {
62 1
                $objects[$key] = $typeFactory($data);
63
            }
64
        }
65
66 1
        return new static($objects);
67
    }
68
69
    public function toNative(): array
70
    {
71
        $this->assertInitialized();
72
        $objects = [];
73
        foreach ($this as $key => $object) {
74
            $objects[$key] = $object->toNative();
75
        }
76
        return $objects;
77
    }
78
79 2
    public function __toString(): string
80
    {
81 2
        $this->assertInitialized();
82 2
        $parts = [];
83 2
        foreach ($this as $key => $object) {
84 2
            $parts[] = $key.':'.(string)$object;
85
        }
86 2
        return implode(', ', $parts);
87
    }
88
}
89