1: <?php
2:
3: namespace Teto\Object;
4:
5: use function call_user_func;
6: use function in_array;
7:
8: /**
9: * Make Property like method
10: *
11: * @author USAMI Kenta <tadsan@zonu.me>
12: * @copyright 2016 Baguette HQ
13: * @license http://www.apache.org/licenses/LICENSE-2.0
14: */
15: trait MethodAlias
16: {
17: /**
18: * Call alias method (magic method)
19: *
20: * @param string $name
21: * @return mixed
22: * @see http://php.net/manual/language.oop5.magic.php
23: * @suppress PhanUndeclaredStaticProperty
24: */
25: public function __call($name, array $args)
26: {
27: if (!isset(self::$method_aliases)) {
28: throw new \LogicException(static::class . '::$method_aliases is not set.');
29: }
30:
31: if (isset(self::$method_aliases[$name])) {
32: $method = self::$method_aliases[$name];
33: } elseif (in_array($name, self::$method_aliases)) {
34: $method = $name;
35: } else {
36: throw new \BadMethodCallException(static::class . "::{$name}() is not exists.");
37: }
38:
39: return call_user_func([$this, $method]);
40: }
41: }
42: