protected function entityToArray(IEntity $entity)
{
$return = [];
$metadata = $entity->getMetadata();
foreach ($metadata->getProperties() as $name => $metadataProperty) {
if ($metadataProperty->isVirtual) {
continue;
} elseif ($metadataProperty->isPrimary && ($entity->isPersisted() || !$entity->hasValue($name))) {
continue;
} elseif ($entity->isPersisted() && !$entity->isModified($name)) {
continue;
}
if ($metadataProperty->relationship !== null) {
$rel = $metadataProperty->relationship;
$canSkip = $rel->type === Relationship::ONE_HAS_MANY || $rel->type === Relationship::MANY_HAS_MANY || $rel->type === Relationship::ONE_HAS_ONE && !$rel->isMain;
if ($canSkip) {
continue;
}
}
$property = $entity->getProperty($name);
if ($property instanceof IProperty) {
$value = $property->getRawValue();
} else {
$value = $entity->getValue($name);
}
$return[$name] = $value;
}
return $return;
}