20 protected $_published = [];
21 protected $_aliases = [];
28 protected function publish($property,$visibility = self::READABLE){
29 if(!is_array($properties = $property)) $properties = [$property => $visibility];
30 elseif(!Record::assoc($properties)) $properties = array_fill_keys($properties,$visibility);
31 $this->_published = array_merge($this->_published,$properties);
39 protected function alias($alias,$property,$object =
null){
40 $this->_aliases[$alias] = [$object,$property];
48 return property_exists($this,$property) || array_key_exists($property,$this->_published);
56 if($config)
foreach($config as $key => $value)
57 if(property_exists($this,$key)) $this->$key = $value;
58 elseif(method_exists($this,$func_name =
'set' . ucfirst($key))) call_user_func([$this,$func_name],$value);
59 elseif(property_exists($this,$property =
'_' . $key) && !method_exists($this,
'get' . ucfirst($key))) $this->$property = $value;
67 $reflect = new \ReflectionClass($this);
68 $length = $prefix ? strlen($prefix) :
null;
70 foreach($reflect->getConstants() as $name => $value)
if(!$prefix || substr($name,0,$length) == $prefix)
71 $constants[$prefix ? substr($name,$length) : $name] = $value;
79 protected function _get($key){
80 throw new \OutOfRangeException(
"Can't get property '$key'");
87 protected function _set($key,$value){
88 throw new \OutOfRangeException(
"Can't set property '$key'");
101 public function get($key){
104 foreach($key as $sub) $result[$sub] = $this->
get($sub);
107 if(property_exists($this,$key) && (new \ReflectionClass($this))->getProperty($key)->isPublic())
return $this->$key;
108 if(method_exists($this,$func_name =
'get' . ucfirst($key)))
return call_user_func([$this,$func_name]);
109 if(array_key_exists($key,$this->_published) && ($this->_published[$key] & self::READABLE)){
110 $property =
'_' . $key;
111 if(($this->$property ===
null) && (method_exists($this,$func_name =
'init' . ucfirst($key)))) $this->$property = call_user_func([$this,$func_name]);
112 return $this->$property;
114 if(array_key_exists($key,$this->_aliases)){
115 list($object,$property) = $this->_aliases[$key];
116 return ($object ?: $this)->$property;
118 return $this->_get($key);
131 public function set($key,$value =
null){
132 if(is_array($key))
foreach($key as $sub => $value) $this->
set($sub,$value);
133 elseif(property_exists($this,$key) && (
new \ReflectionClass($this))->getProperty($key)->isPublic()) $this->$key = $value;
134 elseif(method_exists($this,$func_name =
'set' . ucfirst($key))) call_user_func([$this,$func_name],$value);
135 elseif(array_key_exists($key,$this->_published) && ($this->_published[$key] & self::WRITEABLE)){
136 $property =
'_' . $key;
137 $this->$property = $value;
139 elseif(array_key_exists($key,$this->_aliases)){
140 list($object,$property) = $this->_aliases[$key];
141 if(!$object) $object = $this;
142 $object->$property = $value;
144 else $this->_set($key,$value);
148 return $this->
get($key);
152 $this->
set($key,$value);