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