RSI helpers  0.1
RSI helpers
Thing.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 /**
6  * Basic object.
7  *
8  * By making use of the magic getter and setter it is not necessary to define a specific getter and setter for every property
9  * upfront. All properties can be used as normal properties (that is: without using getXxx() and setXxx() functions). A
10  * property which first was public can be made private/protected, after which it is possible to use a specific getter and/or
11  * setter. Private properties can also be published, which makes it unnecessary to create a specic getter and/or setter.
12  */
13 class Thing{
14 
15  const HIDDEN = 0; //!< Property is hidden.
16  const READABLE = 1; //!< Property is readable.
17  const WRITEABLE = 2; //!< Property is writeable.
18  const READWRITE = 3; //!< Property is readable and writeable.
19 
20  protected $_published = []; //!< Published properties (key = name of property, value = visibility).
21 
22  /**
23  * Publish a property (or hide it again).
24  * @param string|array $property Name of the property, or an array with name-visibility pairs.
25  * @param int $visibility Visibility for the property (when a name is given). See the constants for possibilities.
26  */
27  protected function publish($property,$visibility = self::READABLE){
28  if(!is_array($properties = $property)) $properties = [$property => $visibility];
29  elseif(!Record::assoc($properties)) $properties = array_fill_keys($properties,$visibility);
30  $this->_published = array_merge($this->_published,$properties);
31  }
32  /**
33  * Check if a property exists (public or published).
34  * @param string $property Name of the property.
35  * @return bool True if the property exists.
36  */
37  public function propertyExists($property){
38  return property_exists($this,$property) || array_key_exists($property,$this->_published);
39  }
40  /**
41  * Configure the object.
42  * This will also alter protected and read-only properties. If a specific setter exists it will be used.
43  * @param array $config Array with the configuration (key-value pairs).
44  */
45  protected function configure($config){
46  if($config) foreach($config as $key => $value)
47  if(property_exists($this,$key)) $this->$key = $value;
48  elseif(method_exists($this,$func_name = 'set' . ucfirst($key))) call_user_func([$this,$func_name],$value);
49  elseif(property_exists($this,$property = '_' . $key) && !method_exists($this,'get' . ucfirst($key))) $this->$property = $value;
50  }
51  /**
52  * Return all constants.
53  * @param string $prefix Only constants starting with this prefix.
54  * @return array Key = constant name (without prefix), value = constant value.
55  */
56  public function constants($prefix = null){
57  $reflect = new \ReflectionClass($this);
58  $length = strlen($prefix);
59  $constants = [];
60  foreach($reflect->getConstants() as $name => $value) if(!$prefix || substr($name,0,$length) == $prefix)
61  $constants[substr($name,$length)] = $value;
62  return $constants;
63  }
64  /**
65  * Default getter if no specific setter is defined, and the property is also not published (readable).
66  * @param string $key Name of the property.
67  * @return mixed Value.
68  */
69  protected function _get($key){
70  throw new \Exception("Can't get property '$key'");
71  }
72  /**
73  * Default setter if no specific setter is defined, and the property is also not published (writeable).
74  * @param string $key Name of the property.
75  * @param mixed $value Value for the property.
76  */
77  protected function _set($key,$value){
78  throw new \Exception("Can't set property '$key'");
79  }
80  /**
81  * Get one or more properties.
82  * Evaluation order:
83  * - public property.
84  * - specific setter.
85  * - published property.
86  * - default _get() function.
87  * @see _get()
88  * @param string|array $key Name of the property, or an array with keys.
89  * @return mixed Value(s).
90  */
91  public function get($key){
92  if(is_array($key)){
93  $result = [];
94  foreach($key as $sub) $result[$sub] = $this->get($sub);
95  return $result;
96  }
97  if(property_exists($this,$key)) return $this->$key;
98  if(method_exists($this,$func_name = 'get' . ucfirst($key))) return call_user_func([$this,$func_name]);
99  if(array_key_exists($key,$this->_published) && ($this->_published[$key] & self::READABLE)){
100  $property = '_' . $key;
101  return $this->$property;
102  }
103  return $this->_get($key);
104  }
105  /**
106  * Set one or more properties.
107  * Evaluation order:
108  * - public property.
109  * - specific setter.
110  * - published property.
111  * - default _set() function.
112  * @see _set()
113  * @param string|array $key Name of the property, or an assoc.array with key-value pairs.
114  * @param mixed $value Value(s).
115  */
116  public function set($key,$value = null){
117  if(is_array($key)) foreach($key as $sub => $value) $this->set($sub,$value);
118  elseif(property_exists($this,$key)) $this->$key = $value;
119  elseif(method_exists($this,$func_name = 'set' . ucfirst($key))) call_user_func([$this,$func_name],$value);
120  elseif(array_key_exists($key,$this->_published) && ($this->_published[$key] & self::WRITEABLE)){
121  $property = '_' . $key;
122  $this->$property = $value;
123  }
124  else $this->_set($key,$value);
125  }
126 
127  public function __get($key){
128  return $this->get($key);
129  }
130 
131  public function __set($key,$value){
132  $this->set($key,$value);
133  }
134 
135 }
__get($key)
Definition: Thing.php:127
__set($key, $value)
Definition: Thing.php:131
Basic object.
Definition: Thing.php:13
configure($config)
Configure the object.
Definition: Thing.php:45
_set($key, $value)
Default setter if no specific setter is defined, and the property is also not published (writeable)...
Definition: Thing.php:77
constants($prefix=null)
Return all constants.
Definition: Thing.php:56
Definition: Color.php:3
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
Definition: Thing.php:27
propertyExists($property)
Check if a property exists (public or published).
Definition: Thing.php:37
_get($key)
Default getter if no specific setter is defined, and the property is also not published (readable)...
Definition: Thing.php:69