public function propertyToArray($k, $printDefaults = false)
{
if (!$this->canPropertyBeExported($k)) {
return;
}
$reflection = new \ReflectionClass($this);
$properties = $reflection->getDefaultProperties();
$getter = 'get' . ucfirst($k) . 'Array';
if (!method_exists($this, $getter) || !is_callable(array($this, $getter))) {
$getter = 'get' . ucfirst($k);
if (!method_exists($this, $getter) || !is_callable(array($this, $getter))) {
return null;
}
}
$value = $this->{$getter}();
if (!$printDefaults && $value === $properties[$k]) {
return null;
}
if (is_array($value)) {
$result = [];
foreach ($value as $key => $item) {
if (is_object($item)) {
if ($item instanceof Model) {
$result[$item->getArrayKey() ? $item->getArrayKeyValue() : $key] = $item->toArray($printDefaults);
} else {
$result[$key] = (array) $item;
}
} else {
$result[$key] = $item;
}
}
return $result;
} else {
if (is_object($value) && $value instanceof Model) {
$value = $value->toArray($printDefaults);
}
}
return $value;
}