1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette
11: */
12:
13:
14:
15: /**
16: * Defines an object that has a modifiable state and a read-only (frozen) state.
17: *
18: * @author David Grudl
19: *
20: * @property-read bool $frozen
21: * @package Nette
22: */
23: abstract class NFreezableObject extends NObject
24: {
25: /** @var bool */
26: private $frozen = FALSE;
27:
28:
29:
30: /**
31: * Makes the object unmodifiable.
32: * @return void
33: */
34: public function freeze()
35: {
36: $this->frozen = TRUE;
37: }
38:
39:
40:
41: /**
42: * Is the object unmodifiable?
43: * @return bool
44: */
45: final public function isFrozen()
46: {
47: return $this->frozen;
48: }
49:
50:
51:
52: /**
53: * Creates a modifiable clone of the object.
54: * @return void
55: */
56: public function __clone()
57: {
58: $this->frozen = FALSE;
59: }
60:
61:
62:
63: /**
64: * @return void
65: */
66: protected function updating()
67: {
68: if ($this->frozen) {
69: throw new InvalidStateException("Cannot modify a frozen object {$this->reflection->name}.");
70: }
71: }
72:
73: }
74: