1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
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: */
22: abstract class NFreezableObject extends NObject implements IFreezable
23: {
24: /** @var bool */
25: private $frozen = FALSE;
26:
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: /**
41: * Is the object unmodifiable?
42: * @return bool
43: */
44: final public function isFrozen()
45: {
46: return $this->frozen;
47: }
48:
49:
50:
51: /**
52: * Creates a modifiable clone of the object.
53: * @return void
54: */
55: public function __clone()
56: {
57: $this->frozen = FALSE;
58: }
59:
60:
61:
62: /**
63: * @return void
64: */
65: protected function updating()
66: {
67: if ($this->frozen) {
68: throw new InvalidStateException("Cannot modify a frozen object {$this->reflection->name}.");
69: }
70: }
71:
72: }
73: