1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class NObjectMixin
22: {
23:
24: private static $methods;
25:
26:
27:
28: 29: 30:
31: final public function __construct()
32: {
33: throw new NStaticClassException;
34: }
35:
36:
37:
38: 39: 40: 41: 42: 43: 44: 45:
46: public static function call($_this, $name, $args)
47: {
48: $class = new NClassReflection($_this);
49:
50: if ($name === '') {
51: throw new MemberAccessException("Call to class '$class->name' method without name.");
52: }
53:
54:
55: if ($class->hasEventProperty($name)) {
56: if (is_array($list = $_this->$name) || $list instanceof Traversable) {
57: foreach ($list as $handler) {
58: callback($handler)->invokeArgs($args);
59: }
60: }
61: return NULL;
62: }
63:
64:
65: if ($cb = $class->getExtensionMethod($name)) {
66: array_unshift($args, $_this);
67: return $cb->invokeArgs($args);
68: }
69:
70: throw new MemberAccessException("Call to undefined method $class->name::$name().");
71: }
72:
73:
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: public static function callStatic($class, $name, $args)
84: {
85: throw new MemberAccessException("Call to undefined static method $class::$name().");
86: }
87:
88:
89:
90: 91: 92: 93: 94: 95: 96:
97: public static function & get($_this, $name)
98: {
99: $class = get_class($_this);
100:
101: if ($name === '') {
102: throw new MemberAccessException("Cannot read a class '$class' property without name.");
103: }
104:
105: if (!isset(self::$methods[$class])) {
106:
107:
108:
109:
110: self::$methods[$class] = array_flip(get_class_methods($class));
111: }
112:
113:
114: $name[0] = $name[0] & "\xDF";
115: $m = 'get' . $name;
116: if (isset(self::$methods[$class][$m])) {
117:
118:
119:
120: $val = $_this->$m();
121: return $val;
122: }
123:
124: $m = 'is' . $name;
125: if (isset(self::$methods[$class][$m])) {
126: $val = $_this->$m();
127: return $val;
128: }
129:
130: $type = isset(self::$methods[$class]['set' . $name]) ? 'a write-only' : 'an undeclared';
131: $name = func_get_arg(1);
132: throw new MemberAccessException("Cannot read $type property $class::\$$name.");
133: }
134:
135:
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: public static function set($_this, $name, $value)
146: {
147: $class = get_class($_this);
148:
149: if ($name === '') {
150: throw new MemberAccessException("Cannot write to a class '$class' property without name.");
151: }
152:
153: if (!isset(self::$methods[$class])) {
154: self::$methods[$class] = array_flip(get_class_methods($class));
155: }
156:
157:
158: $name[0] = $name[0] & "\xDF";
159:
160: $m = 'set' . $name;
161: if (isset(self::$methods[$class][$m])) {
162: $_this->$m($value);
163: return;
164: }
165:
166: $type = isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name])
167: ? 'a read-only' : 'an undeclared';
168: $name = func_get_arg(1);
169: throw new MemberAccessException("Cannot write to $type property $class::\$$name.");
170: }
171:
172:
173:
174: 175: 176: 177: 178: 179: 180:
181: public static function remove($_this, $name)
182: {
183: $class = get_class($_this);
184: throw new MemberAccessException("Cannot unset the property $class::\$$name.");
185: }
186:
187:
188:
189: 190: 191: 192: 193: 194:
195: public static function has($_this, $name)
196: {
197: if ($name === '') {
198: return FALSE;
199: }
200:
201: $class = get_class($_this);
202: if (!isset(self::$methods[$class])) {
203: self::$methods[$class] = array_flip(get_class_methods($class));
204: }
205:
206: $name[0] = $name[0] & "\xDF";
207: return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
208: }
209:
210: }
211: