Rev 60 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 60 | Rev 72 | ||
---|---|---|---|
Line 38... | Line 38... | ||
38 | * @author Thomas 'PointedEars' Lahn
|
38 | * @author Thomas 'PointedEars' Lahn
|
39 | */
|
39 | */
|
40 | abstract class Base |
40 | abstract class Base |
41 | {
|
41 | {
|
42 | /**
|
42 | /**
|
- | 43 | * Determines if a strict model is enforced.
|
|
- | 44 | *
|
|
- | 45 | * If <code>true</code>, all publicly accessible properties
|
|
- | 46 | * must have a getter if readable, and a setter if writable.
|
|
- | 47 | * Otherwise, accesses to non-existing public properties will be
|
|
- | 48 | * forwarded to the correspnding underline property if no getter
|
|
- | 49 | * or setter has been defined for it. The default is
|
|
- | 50 | * <code>false</code> (non-strict) as that speeds up property
|
|
- | 51 | * accesses and eases implementation considerably.
|
|
- | 52 | *
|
|
- | 53 | * @var bool
|
|
- | 54 | */
|
|
- | 55 | protected static $_strict = false; |
|
- | 56 | ||
- | 57 | /**
|
|
43 | * Retrieves a property value.
|
58 | * Retrieves a property value.
|
44 | *
|
59 | *
|
45 | * Automagically called when attempting to read data
|
60 | * Automagically called when attempting to read data
|
46 | * from inaccessible properties.
|
61 | * from inaccessible properties.
|
47 | *
|
62 | *
|
Line 61... | Line 76... | ||
61 | {
|
76 | {
|
62 | return $this->$getter(); |
77 | return $this->$getter(); |
63 | }
|
78 | }
|
64 | 79 | ||
65 | if (property_exists($this, "_$name")) |
80 | if (property_exists($this, "_$name")) |
66 | {
|
81 | {
|
- | 82 | $class = get_class($this); |
|
- | 83 | if ($class::$_strict) |
|
- | 84 | {
|
|
67 | throw new \InvalidArgumentException("Property '{$name}' has no getter"); |
85 | throw new \InvalidArgumentException("Strict model: Property '{$name}' has no getter"); |
- | 86 | }
|
|
- | 87 | ||
- | 88 | return $this->{"_$name"}; |
|
68 | }
|
89 | }
|
69 | 90 | ||
70 | throw new \InvalidArgumentException("No such property: '{$name}'"); |
91 | throw new \InvalidArgumentException("No such property: '{$name}'"); |
71 | }
|
92 | }
|
72 | 93 | ||
Line 92... | Line 113... | ||
92 | $setter = 'set' . ucfirst($name); |
113 | $setter = 'set' . ucfirst($name); |
93 | if (method_exists($this, $setter)) |
114 | if (method_exists($this, $setter)) |
94 | {
|
115 | {
|
95 | return $this->$setter($value); |
116 | return $this->$setter($value); |
96 | }
|
117 | }
|
97 | 118 | ||
98 | if (property_exists($this, "_$name")) |
119 | if (property_exists($this, "_$name")) |
99 | {
|
120 | {
|
- | 121 | $class = get_class($this); |
|
- | 122 | if ($class::$_strict) |
|
- | 123 | {
|
|
100 | throw new \InvalidArgumentException("Property '{$name}' has no setter"); |
124 | throw new \InvalidArgumentException("Strict model: Property '{$name}' has no setter"); |
- | 125 | }
|
|
- | 126 | ||
- | 127 | $this->{"_$name"} = $value; |
|
- | 128 | return $value; |
|
101 | }
|
129 | }
|
102 | 130 | ||
103 | throw new \InvalidArgumentException("No such property: '{$name}'"); |
131 | throw new \InvalidArgumentException("No such property: '{$name}'"); |
104 | }
|
132 | }
|
105 | }
|
133 | }
|
106 | 134 |