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