Skip to content

Instantly share code, notes, and snippets.

@davidtsadler
Created July 28, 2016 14:49
Show Gist options
  • Save davidtsadler/cd2e09dbc4455541093f22c3686d4834 to your computer and use it in GitHub Desktop.
Save davidtsadler/cd2e09dbc4455541093f22c3686d4834 to your computer and use it in GitHub Desktop.
<?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