1: <?php
2:
3: /**
4: * Orchestra: A minimalist object-oriented superset for WordPress using Phalcon.
5: *
6: * This source file is subject to the MIT license that is bundled
7: * with this package in the file LICENSE and is available through
8: * the world-wide-web at the following URI:
9: * http://opensource.org/licenses/MIT
10: *
11: * @copyright Akufen Atelier Creatif
12: * @author Nicholas Charbonneau <nicholas@akufen.ca>
13: * @license http://opensource.org/licenses/MIT
14: * @version 0.1.3
15: * @link https://github.com/akufenstudio/orchestra
16: */
17:
18: namespace Akufen\Orchestra\Traits;
19:
20: use Akufen\Orchestra\Helpers\Strings;
21:
22: /**
23: * Akufen\Orchestra\Traits\AccessibleTrait
24: *
25: * A trait for easy class property access.
26: *
27: * @package Traits
28: */
29: trait AccessibleTrait
30: {
31: /**
32: * Magic method for fast get & set
33: *
34: * @param string $method The name of the method that was executed
35: * @param mixed $value The value that will be used to set a property
36: * @return mixed The value of a property or null
37: */
38: public function __call($method, $value = null)
39: {
40: // Handle a get method on a service
41: if (preg_match('/^get/', $method)) {
42: $property = lcfirst(substr($method, 3));
43: if (!isset($this->{$property})) {
44: $property = Strings::toUnderscores($property);
45: }
46: return $this->get($property);
47: }
48:
49: // Handle a set method on a service
50: if (preg_match('/^set/', $method)) {
51: if (is_array($value) && !empty($value)) {
52: $this->set(lcfirst(substr($method, 3)), $value[0]);
53: }
54: return null;
55: }
56:
57: throw new Exception("Method '".$method."' wasn't found in the class.");
58: }
59:
60: /**
61: * Set a class property
62: *
63: * @param string $name The name of the property to set
64: * @param mixed $value The value of the property to set
65: * @return void
66: */
67: public function set($name, $value)
68: {
69: $name = strval($name);
70:
71: $this->$name = $value;
72: }
73:
74: /**
75: * Get a class property
76: *
77: * @param string $name The requested variable name
78: * @return mixed $var The instance of the variable or null
79: */
80: public function get($name)
81: {
82: $name = strval($name);
83:
84: if (!isset($this->$name)) {
85: throw new Exception("Access to undefined property");
86: }
87:
88: return $this->$name;
89: }
90: }
91: