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 LogicException("Cannot instantiate static class " . get_class($this));
34: }
35:
36:
37:
38: 39: 40: 41: 42: 43: 44:
45: public static function call($_this, $name, $args)
46: {
47: $class = new NClassReflection($_this);
48:
49: if ($name === '') {
50: throw new MemberAccessException("Call to class '$class->name' method without name.");
51: }
52:
53:
54: if ($class->hasEventProperty($name)) {
55: if (is_array($list = $_this->$name) || $list instanceof Traversable) {
56: foreach ($list as $handler) {
57: callback($handler)->invokeArgs($args);
58: }
59: }
60: return NULL;
61: }
62:
63:
64: if ($cb = $class->getExtensionMethod($name)) {
65: array_unshift($args, $_this);
66: return $cb->invokeArgs($args);
67: }
68:
69: throw new MemberAccessException("Call to undefined method $class->name::$name().");
70: }
71:
72:
73:
74: 75: 76: 77: 78: 79:
80: public static function & get($_this, $name)
81: {
82: $class = get_class($_this);
83:
84: if ($name === '') {
85: throw new MemberAccessException("Cannot read a class '$class' property without name.");
86: }
87:
88: if (!isset(self::$methods[$class])) {
89:
90:
91:
92:
93: self::$methods[$class] = array_flip(get_class_methods($class));
94: }
95:
96:
97: $name[0] = $name[0] & "\xDF";
98: $m = 'get' . $name;
99: if (isset(self::$methods[$class][$m])) {
100:
101:
102:
103: $val = $_this->$m();
104: return $val;
105: }
106:
107: $m = 'is' . $name;
108: if (isset(self::$methods[$class][$m])) {
109: $val = $_this->$m();
110: return $val;
111: }
112:
113: $name = func_get_arg(1);
114: throw new MemberAccessException("Cannot read an undeclared property $class::\$$name.");
115: }
116:
117:
118:
119: 120: 121: 122: 123: 124: 125:
126: public static function set($_this, $name, $value)
127: {
128: $class = get_class($_this);
129:
130: if ($name === '') {
131: throw new MemberAccessException("Cannot assign to a class '$class' property without name.");
132: }
133:
134: if (!isset(self::$methods[$class])) {
135: self::$methods[$class] = array_flip(get_class_methods($class));
136: }
137:
138:
139: $name[0] = $name[0] & "\xDF";
140: if (isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name])) {
141: $m = 'set' . $name;
142: if (isset(self::$methods[$class][$m])) {
143: $_this->$m($value);
144: return;
145:
146: } else {
147: $name = func_get_arg(1);
148: throw new MemberAccessException("Cannot assign to a read-only property $class::\$$name.");
149: }
150: }
151:
152: $name = func_get_arg(1);
153: throw new MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
154: }
155:
156:
157:
158: 159: 160: 161: 162:
163: public static function has($_this, $name)
164: {
165: if ($name === '') {
166: return FALSE;
167: }
168:
169: $class = get_class($_this);
170: if (!isset(self::$methods[$class])) {
171: self::$methods[$class] = array_flip(get_class_methods($class));
172: }
173:
174: $name[0] = $name[0] & "\xDF";
175: return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
176: }
177:
178: }
179: