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