Elgg\AttributeLoader::getRequiredAttributes PHP Méthode

getRequiredAttributes() public méthode

This function splits loading between "primary" attributes (those in {prefix}entities table) and "secondary" attributes (e.g. those in {prefix}objects_entity), but can load all at once if a combined loader is available.
public getRequiredAttributes ( mixed $row ) : array
$row mixed a row loaded from DB (array or \stdClass) or a GUID
Résultat array will be empty if failed to load all attributes (access control or entity doesn't exist)
    public function getRequiredAttributes($row)
    {
        if (!is_array($row) && !$row instanceof \stdClass) {
            // assume row is the GUID
            $row = array('guid' => $row);
        }
        $row = (array) $row;
        if (empty($row['guid'])) {
            throw new \InvalidArgumentException('$row must be or contain a GUID');
        }
        $was_missing_primaries = $this->isMissingPrimaries($row);
        $was_missing_secondaries = $this->isMissingSecondaries($row);
        // some types have a function to load all attributes at once, it should be faster
        if (($was_missing_primaries || $was_missing_secondaries) && is_callable($this->full_loader)) {
            $fetched = (array) call_user_func($this->full_loader, $row['guid']);
            if (!$fetched) {
                return array();
            }
            $row = array_merge($row, $fetched);
            $this->checkType($row);
        } else {
            if ($was_missing_primaries) {
                if (!is_callable($this->primary_loader)) {
                    throw new \LogicException('Primary attribute loader must be callable');
                }
                if ($this->requires_access_control) {
                    $fetched = (array) call_user_func($this->primary_loader, $row['guid']);
                } else {
                    $ignoring_access = elgg_set_ignore_access();
                    $fetched = (array) call_user_func($this->primary_loader, $row['guid']);
                    elgg_set_ignore_access($ignoring_access);
                }
                if (!$fetched) {
                    return array();
                }
                $row = array_merge($row, $fetched);
            }
            // We must test type before trying to load the secondaries so that InvalidClassException
            // gets thrown. Otherwise the secondary loader will fail and return false.
            $this->checkType($row);
            if ($was_missing_secondaries) {
                if (!is_callable($this->secondary_loader)) {
                    throw new \LogicException('Secondary attribute loader must be callable');
                }
                $fetched = (array) call_user_func($this->secondary_loader, $row['guid']);
                if (!$fetched) {
                    throw new \IncompleteEntityException("Secondary loader failed to return row for {$row['guid']}");
                }
                $row = array_merge($row, $fetched);
            }
        }
        $row = $this->filterAddedColumns($row);
        $row['subtype'] = (int) $row['subtype'];
        // set to null when reading empty value, to match default empty value; See #5456
        foreach (self::$null_attr_names as $key) {
            if (isset($row[$key]) && !$row[$key]) {
                $row[$key] = null;
            }
        }
        // Note: If there are still missing attributes, we're running on a 1.7 or earlier schema. We let
        // this pass so the upgrades can run.
        // guid needs to be an int  https://github.com/elgg/elgg/issues/4111
        foreach (self::$integer_attr_names as $key) {
            if (isset($row[$key])) {
                $row[$key] = (int) $row[$key];
            }
        }
        return $row;
    }