1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://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: * Defines an object that has a modifiable and a read-only (frozen) state.
19: *
20: * @author David Grudl
21: *
22: * @property-read bool $frozen
23: */
24: abstract class FreezableObject extends Object implements IFreezable
25: {
26: /** @var bool */
27: private $frozen = FALSE;
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: * Is the object unmodifiable?
42: * @return bool
43: */
44: final public function isFrozen()
45: {
46: return $this->frozen;
47: }
48:
49:
50: /**
51: * Creates a modifiable clone of the object.
52: * @return void
53: */
54: public function __clone()
55: {
56: $this->frozen = FALSE;
57: }
58:
59:
60: /**
61: * @return void
62: */
63: protected function updating()
64: {
65: if ($this->frozen) {
66: $class = get_class($this);
67: throw new InvalidStateException("Cannot modify a frozen object $class.");
68: }
69: }
70:
71: }
72: