PropertyEntry::freeze()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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;
19
20
use CuyZ\Notiz\Core\Exception\PropertyNotAccessibleException;
21
use CuyZ\Notiz\Service\LocalizationService;
22
23
/**
24
 * An entry of a property definition, that is created in an event:
25
 *
26
 * @see \CuyZ\Notiz\Core\Event\Support\HasProperties::buildPropertyDefinition
27
 *
28
 * An entry must be named, and can contain a label. You may add more class
29
 * properties with own getters/setters, that can be filled during the definition
30
 * build and used during the notification dispatching.
31
 *
32
 * You may set its value with arbitrary data that can later be used by a
33
 * notification, but you wont be able to force a value after the method below
34
 * was called:
35
 *
36
 * @see \CuyZ\Notiz\Core\Event\Support\HasProperties::fillPropertyEntries
37
 */
38
abstract class PropertyEntry
39
{
40
    /**
41
     * @var string
42
     */
43
    private $name;
44
45
    /**
46
     * @var mixed
47
     */
48
    private $value;
49
50
    /**
51
     * @var string
52
     */
53
    protected $label;
54
55
    /**
56
     * @var bool
57
     */
58
    private $frozen = false;
59
60
    /**
61
     * @param string $name
62
     */
63
    public function __construct(string $name)
64
    {
65
        $this->name = $name;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    final public function getName(): string
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    final public function getValue()
80
    {
81
        return $this->value;
82
    }
83
84
    /**
85
     * @param mixed $value
86
     * @return $this
87
     *
88
     * @throws PropertyNotAccessibleException
89
     */
90
    final public function setValue($value)
91
    {
92
        if ($this->frozen) {
93
            throw PropertyNotAccessibleException::propertyEntryValueNotAccessible($this);
94
        }
95
96
        $this->value = $value;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getLabel(): string
105
    {
106
        return LocalizationService::localize($this->label);
107
    }
108
109
    /**
110
     * @param string $label
111
     * @return $this
112
     */
113
    public function setLabel(string $label)
114
    {
115
        $this->label = $label;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Once the entry has been processed by the event, we freeze this entry to
122
     * prevent external value changes.
123
     *
124
     * @internal
125
     */
126
    final public function freeze()
127
    {
128
        $this->frozen = true;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function __toString(): string
135
    {
136
        return (string)$this->getValue();
137
    }
138
}
139