PropertyContainer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Property\Factory;
19
20
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
21
use CuyZ\Notiz\Core\Property\PropertyEntry;
22
23
/**
24
 * A container for a list of property entries that are fetched from a property
25
 * definition.
26
 *
27
 * @see \CuyZ\Notiz\Core\Property\Factory\PropertyDefinition
28
 *
29
 * The differences with a property definition is that you can not add more
30
 * entries, you may only modify the data from the existing ones: setting their
31
 * values, manipulating other arbitrary data, other...
32
 */
33
class PropertyContainer
34
{
35
    /**
36
     * @var PropertyDefinition
37
     */
38
    protected $definition;
39
40
    /**
41
     * @var PropertyEntry[]
42
     */
43
    protected $properties = [];
44
45
    /**
46
     * @param PropertyDefinition $definition
47
     */
48
    public function __construct(PropertyDefinition $definition)
49
    {
50
        $this->definition = $definition;
51
52
        foreach ($definition->getEntries() as $property) {
53
            /*
54
             * Every property is cloned, to insure the base property wont be
55
             * modified.
56
             */
57
            $this->properties[$property->getName()] = clone $property;
58
        }
59
    }
60
61
    /**
62
     * Freezes all registered properties entries.
63
     *
64
     * @see \CuyZ\Notiz\Core\Property\PropertyEntry::freeze
65
     */
66
    public function freeze()
67
    {
68
        foreach ($this->properties as $property) {
69
            $property->freeze();
70
        }
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getPropertyType(): string
77
    {
78
        return $this->definition->getPropertyType();
79
    }
80
81
    /**
82
     * @param string $name
83
     * @return bool
84
     */
85
    public function hasEntry(string $name): bool
86
    {
87
        return isset($this->properties[$name]);
88
    }
89
90
    /**
91
     * @param string $name
92
     * @return PropertyEntry
93
     *
94
     * @throws EntryNotFoundException
95
     */
96
    public function getEntry(string $name): PropertyEntry
97
    {
98
        if (false === $this->hasEntry($name)) {
99
            throw EntryNotFoundException::propertyEntryNotFound($name, $this->definition->getEventClassName(), $this->getPropertyType(), $this);
100
        }
101
102
        return $this->properties[$name];
103
    }
104
105
    /**
106
     * @return PropertyEntry[]
107
     */
108
    public function getEntries(): array
109
    {
110
        return $this->properties;
111
    }
112
}
113