Bravo3\Orm\Services\RelationshipManager::persistForwardSortIndices PHP Method

persistForwardSortIndices() private method

Persist forward sorted indices
private persistForwardSortIndices ( Relationship $relationship, string $local_id, object | object[] $value )
$relationship Bravo3\Orm\Mappers\Metadata\Relationship
$local_id string
$value object | object[]
    private function persistForwardSortIndices(Relationship $relationship, $local_id, $value)
    {
        if ($value === null) {
            $value = [];
        } elseif (!is_array($value)) {
            $value = [$value];
        }
        $this->getDriver()->debugLog('@Setting forward sort indices for "' . $local_id . '"');
        foreach ($relationship->getSortableBy() as $sortable) {
            $key = $this->getSortIndexKey($relationship, $sortable->getName(), $local_id);
            $this->getDriver()->clearSortedIndex($key);
            foreach ($value as $entity) {
                $metadata = $this->getMapper()->getEntityMetadata($entity);
                $reader = new Reader($metadata, $entity);
                $foreign_id = $reader->getId();
                // Test conditions
                foreach ($sortable->getConditions() as $condition) {
                    if ($condition->getColumn()) {
                        if (!$condition->test($reader->getPropertyValue($condition->getColumn()))) {
                            continue 2;
                        }
                    } elseif ($condition->getMethod()) {
                        if (!$condition->test($reader->getMethodValue($condition->getMethod()))) {
                            continue 2;
                        }
                    }
                }
                $score = $reader->getPropertyValue($sortable->getColumn());
                $this->getDriver()->addSortedIndex($key, $score, $foreign_id);
            }
        }
    }