public function jsonSerialize()
{
$data = new stdClass();
// Strip undefined and null values.
$classVars = get_class_vars(get_class($this));
foreach (get_object_vars($this) as $property => $value) {
if ($value !== UNDEFINED) {
if ($classVars[$property] === UNDEFINED) {
// When default is undefined, null is allowed.
$data->{$property} = $value;
} elseif ($value !== null) {
$data->{$property} = $value;
}
}
}
// Strip properties that are for internal (swagger-php) use.
foreach (static::$_blacklist as $property) {
unset($data->{$property});
}
// Inject vendor properties.
unset($data->x);
if (is_array($this->x)) {
foreach ($this->x as $property => $value) {
$prefixed = 'x-' . $property;
$data->{$prefixed} = $value;
}
}
// Map nested keys
foreach (static::$_nested as $nested) {
if (is_string($nested) || count($nested) === 1) {
continue;
}
$property = $nested[0];
if ($this->{$property} === null) {
continue;
}
$keyField = $nested[1];
$object = new stdClass();
foreach ($this->{$property} as $item) {
$key = $item->{$keyField};
if ($key && empty($object->{$key})) {
$object->{$key} = $item->jsonSerialize();
unset($object->{$key}->{$keyField});
}
}
$data->{$property} = $object;
}
// $ref
if (isset($data->ref)) {
$dollarRef = '$ref';
$data->{$dollarRef} = $data->ref;
unset($data->ref);
}
return $data;
}