Created
July 28, 2016 14:49
-
-
Save davidtsadler/cd2e09dbc4455541093f22c3686d4834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class StdClassLike implements JmesPathableObjectInterface | |
{ | |
private $values = []; | |
public function __get($name) | |
{ | |
return $this->values[$name]; | |
} | |
public function __set($name, $value) | |
{ | |
$this->values[$name] = $value; | |
} | |
public function __isset($name) | |
{ | |
return array_key_exists($name, $this->values); | |
} | |
public function toArray() | |
{ | |
$array = []; | |
foreach ($this->values as $name => $value) { | |
if ($value instanceof ArrayLike) { | |
$array[$name] = []; | |
foreach ($value as $property) { | |
$array[$name][] = self::propertyToArrayValue($property); | |
} | |
} else { | |
$array[$name] = self::propertyToArrayValue($value); | |
} | |
} | |
return $array; | |
} | |
private static function propertyToArrayValue($value) | |
{ | |
if ($value instanceof StdClassLike) { | |
return $value->toArray(); | |
} else { | |
return $value; | |
} | |
} | |
public function __toString() | |
{ | |
return json_encode($this->toArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment