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: */
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Defines an object that has a modifiable state and a read-only (frozen) state.
20: *
21: * @author David Grudl
22: *
23: * @property-read bool $frozen
24: */
25: abstract class FreezableObject extends Object
26: {
27: /** @var bool */
28: private $frozen = FALSE;
29:
30:
31:
32: /**
33: * Makes the object unmodifiable.
34: * @return void
35: */
36: public function freeze()
37: {
38: $this->frozen = TRUE;
39: }
40:
41:
42:
43: /**
44: * Is the object unmodifiable?
45: * @return bool
46: */
47: final public function isFrozen()
48: {
49: return $this->frozen;
50: }
51:
52:
53:
54: /**
55: * Creates a modifiable clone of the object.
56: * @return void
57: */
58: public function __clone()
59: {
60: $this->frozen = FALSE;
61: }
62:
63:
64:
65: /**
66: * @return void
67: */
68: protected function updating()
69: {
70: if ($this->frozen) {
71: throw new \InvalidStateException("Cannot modify a frozen object {$this->reflection->name}.");
72: }
73: }
74:
75: }
76: