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