Fix isset for models/records

This commit is contained in:
Gregory Oschwald
2013-12-02 10:02:37 -08:00
parent 0bed941634
commit fbbe2dba24
3 changed files with 43 additions and 4 deletions
+19 -4
View File
@@ -19,19 +19,34 @@ abstract class AbstractRecord implements \JsonSerializable
*/
public function __get($attr)
{
$valid = in_array($attr, $this->validAttributes);
// XXX - kind of ugly but greatly reduces boilerplate code
$key = strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
$key = $this->attributeToKey($attr);
if ($valid && isset($this->record[$key])) {
if ($this->__isset($attr)) {
return $this->record[$key];
} elseif ($valid) {
} elseif ($this->validAttribute($attr)) {
return null;
} else {
throw new \RuntimeException("Unknown attribute: $attr");
}
}
public function __isset($attr)
{
return $this->validAttribute($attr) &&
isset($this->record[$this->attributeToKey($attr)]);
}
private function attributeToKey($attr)
{
return strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
}
public function validAttribute($attr)
{
return in_array($attr, $this->validAttributes);
}
public function jsonSerialize()
{
return $this->record;