Spot\Mapper::validateRelatedEntity PHP Method

validateRelatedEntity() protected method

Validate related entity if it is new or modified only
protected validateRelatedEntity ( EntityInterface $relatedEntity, EntityInterface $entity, RelationAbstract $relation ) : array
$relatedEntity EntityInterface
$entity EntityInterface
$relation Spot\Relation\RelationAbstract
return array Related entity errors
    protected function validateRelatedEntity(EntityInterface $relatedEntity, EntityInterface $entity, \Spot\Relation\RelationAbstract $relation)
    {
        $tainted = $relatedEntity->isNew() || $relatedEntity->isModified();
        $errorsRelated = [];
        if ($tainted && !$this->getMapper(get_class($relatedEntity))->validate($relatedEntity)) {
            $errorsRelated = $relatedEntity->errors();
            //Disable validation on foreign key field it will be filled up later on when the new entity is persisted
            if (($relation instanceof Relation\HasMany || $relation instanceof Relation\HasOne) && $relatedEntity->isNew()) {
                unset($errorsRelated[$relation->foreignKey()]);
            }
            $relatedEntity->errors($errorsRelated);
        }
        if ($relation instanceof Relation\BelongsTo && $entity->isNew()) {
            $errors = $entity->errors();
            unset($errors[$relation->localKey()]);
            $entity->errors($errors);
        }
        return $errorsRelated;
    }