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