1 | <?php |
||
54 | class SystemProperties extends AbstractProperties |
||
55 | { |
||
56 | /** |
||
57 | * Collection name |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | const COLLECTION = 'system'; |
||
62 | /** |
||
63 | * ID property |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const PROPERTY_ID = 'id'; |
||
68 | /** |
||
69 | * Type property |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const PROPERTY_TYPE = 'type'; |
||
74 | /** |
||
75 | * Revision property |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | const PROPERTY_REVISION = 'revision'; |
||
80 | /** |
||
81 | * Created property |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | const PROPERTY_CREATED = 'created'; |
||
86 | /** |
||
87 | * Published property |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | const PROPERTY_PUBLISHED = 'published'; |
||
92 | /** |
||
93 | * Hash property |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | const PROPERTY_HASH = 'hash'; |
||
98 | /** |
||
99 | * Object ID (constant throughout revisions) |
||
100 | * |
||
101 | * @var Id |
||
102 | */ |
||
103 | protected $uid = null; |
||
104 | /** |
||
105 | * Object type (constant throughout revisions) |
||
106 | * |
||
107 | * @var Type |
||
108 | */ |
||
109 | protected $type = null; |
||
110 | /** |
||
111 | * Object revision |
||
112 | * |
||
113 | * @var Revision |
||
114 | */ |
||
115 | protected $revision = null; |
||
116 | /** |
||
117 | * Creation date of this revision |
||
118 | * |
||
119 | * @var \DateTimeImmutable |
||
120 | */ |
||
121 | protected $created = null; |
||
122 | /** |
||
123 | * Publication date of this revision |
||
124 | * |
||
125 | * @var \DateTimeImmutable |
||
126 | */ |
||
127 | protected $published = null; |
||
128 | /** |
||
129 | * Object hash of this revision |
||
130 | * |
||
131 | * @var string |
||
132 | */ |
||
133 | protected $hash = ''; |
||
134 | |||
135 | /******************************************************************************* |
||
136 | * PUBLIC METHODS |
||
137 | *******************************************************************************/ |
||
138 | |||
139 | /** |
||
140 | * System properties constructor |
||
141 | * |
||
142 | * @param array $data Property data |
||
143 | * @param ObjectInterface $object Owner object |
||
144 | */ |
||
145 | 22 | public function __construct(array $data, ObjectInterface $object) |
|
146 | { |
||
147 | 22 | parent::__construct($data, $object); |
|
148 | |||
149 | // Initialize the object ID |
||
150 | 22 | if (array_key_exists(self::PROPERTY_ID, $data)) { |
|
151 | 20 | $this->uid = Id::unserialize($data[self::PROPERTY_ID]); |
|
152 | } |
||
153 | |||
154 | // Initialize the object type |
||
155 | 22 | if (array_key_exists(self::PROPERTY_TYPE, $data)) { |
|
156 | 21 | $this->type = Type::unserialize($data[self::PROPERTY_TYPE]); |
|
157 | } |
||
158 | |||
159 | // Initialize the object revision |
||
160 | 21 | if (array_key_exists(self::PROPERTY_REVISION, $data)) { |
|
161 | 20 | $this->revision = Revision::unserialize($data[self::PROPERTY_REVISION]); |
|
162 | } |
||
163 | |||
164 | // Initialize the object creation date |
||
165 | 21 | if (array_key_exists(self::PROPERTY_CREATED, $data)) { |
|
166 | 20 | $this->created = new \DateTimeImmutable('@'.$data[self::PROPERTY_CREATED]); |
|
167 | } |
||
168 | |||
169 | // Initialize the object publication date |
||
170 | 21 | if (array_key_exists(self::PROPERTY_PUBLISHED, $data)) { |
|
171 | 15 | $this->published = new \DateTimeImmutable('@'.$data[self::PROPERTY_PUBLISHED]); |
|
172 | } |
||
173 | |||
174 | // Initialize the object hash |
||
175 | 21 | if (array_key_exists(self::PROPERTY_HASH, $data)) { |
|
176 | 19 | $this->hash = $data[self::PROPERTY_HASH]; |
|
177 | } |
||
178 | |||
179 | // Test if all mandatory properties are set |
||
180 | 21 | if (!($this->uid instanceof Id) |
|
181 | 20 | || !($this->type instanceof Type) |
|
182 | 20 | || !($this->revision instanceof Revision) |
|
183 | 21 | || !($this->created instanceof \DateTimeImmutable) |
|
184 | // || !$this->hasValidHash() |
||
185 | ) { |
||
186 | 1 | throw new InvalidArgumentException( |
|
187 | 1 | 'Invalid system properties', |
|
188 | 1 | InvalidArgumentException::INVALID_SYSTEM_PROPERTIES |
|
189 | ); |
||
190 | } |
||
191 | 20 | } |
|
192 | |||
193 | /** |
||
194 | * Return the object ID |
||
195 | * |
||
196 | * @return Id Object ID |
||
197 | */ |
||
198 | 5 | public function getId() |
|
199 | { |
||
200 | 5 | return $this->uid; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Return the object type |
||
205 | * |
||
206 | * @return Type Object type |
||
207 | */ |
||
208 | 1 | public function getType() |
|
209 | { |
||
210 | 1 | return $this->type; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Return the object revision |
||
215 | * |
||
216 | * @return Revision Object revision |
||
217 | */ |
||
218 | 19 | public function getRevision() |
|
219 | { |
||
220 | 19 | return $this->revision; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Return the object draft mode |
||
225 | * |
||
226 | * @return boolean Object draft mode |
||
227 | */ |
||
228 | 4 | public function isDraft() |
|
229 | { |
||
230 | 4 | return !($this->published instanceof \DateTimeImmutable); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Return the creation date & time of this revision |
||
235 | * |
||
236 | * @return \DateTimeImmutable Creation date & time |
||
237 | */ |
||
238 | 1 | public function getCreated() |
|
242 | |||
243 | /** |
||
244 | * Return the publication date & time of this revision |
||
245 | * |
||
246 | * @return \DateTimeImmutable|null Publication date & time |
||
247 | */ |
||
248 | 1 | public function getPublished() |
|
252 | |||
253 | /** |
||
254 | * Return the object hash of this revision |
||
255 | * |
||
256 | * @return string Object hash |
||
257 | */ |
||
258 | 1 | public function getHash() |
|
262 | |||
263 | /** |
||
264 | * Derive draft system properties |
||
265 | * |
||
266 | * @param Revision $draftRevision Draft revision |
||
267 | * @return SystemProperties Draft system properties |
||
268 | */ |
||
269 | 3 | public function createDraft(Revision $draftRevision) |
|
281 | |||
282 | /** |
||
283 | * Indicate that the object got published |
||
284 | * |
||
285 | * @return SystemProperties System properties |
||
286 | * @throws RuntimeException If the object is already published |
||
287 | */ |
||
288 | 2 | public function publish() |
|
302 | |||
303 | /** |
||
304 | * Return the property values as array |
||
305 | * |
||
306 | * @return array Property values |
||
307 | */ |
||
308 | 5 | public function toArray() |
|
320 | |||
321 | /******************************************************************************* |
||
322 | * PRIVATE METHODS |
||
323 | *******************************************************************************/ |
||
324 | |||
325 | /** |
||
326 | * Test if the object hash is a valid sha1 value |
||
327 | * |
||
328 | * @return bool The object hash is a valid sha1 value |
||
329 | */ |
||
330 | protected function hasValidHash() |
||
334 | } |
||
335 |