Skip to content

Commit

Permalink
[#9] FetchAncestors trie bien par profondeur et fetchAncestor peut re…
Browse files Browse the repository at this point in the history
…tourner le plus lointain ou proche parent

Signed-off-by: daverner <[email protected]>
  • Loading branch information
daverner committed Jul 30, 2021
1 parent 06f81fa commit 3f5c2d4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 90 deletions.
42 changes: 17 additions & 25 deletions Services/FetchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,20 @@ public function fetchChildren($parentLocation, $contentClass = null, $limit = se
$parentLocationId = $parentLocation instanceof Location ? $parentLocation->id : $parentLocation;
$params[] = new Criterion\ParentLocationId($parentLocationId);

return $this->fetchLocationList($params, $limit);
return $this->fetchLocationList($params, null, $limit);
}

/**
* @param array $params
* @param int $limit
* @param int $offset
* @param string $sortClauses
* parametre which specifies the way the request will be sorted : "depth" or "prioriy"
* @param SortClause[]|null $sortClauses If null, results will be sorted by priority
* @return Location[]
* @throws InvalidArgumentException
*/
private function fetchLocationList(
array $params,
$sortClauses = "priority",
$sortClauses = null,
$limit = self::LIMIT,
$offset = 0
): array {
Expand All @@ -127,16 +126,8 @@ private function fetchLocationList(
$query->performCount = true;
$query->limit = $limit;
$query->offset = $offset;
if ($sortClauses == "depth") {
$query->sortClauses = [
new SortClause\Location\Depth(Query::SORT_ASC),
];
}
if ($sortClauses == "priority") {
$query->sortClauses = [
new SortClause\Location\Priority(Query::SORT_ASC),
];
}
$query->sortClauses = is_null($sortClauses) ?
[ new SortClause\Location\Priority(Query::SORT_ASC) ] : $sortClauses;
$results = $this->searchService->findLocations($query);
$items = [];

Expand Down Expand Up @@ -231,9 +222,8 @@ public function fetchAncestors($location, string $contentType): ?array
new Criterion\Ancestor($location->pathString),
];

$sortClauses = "depth";
$items = $this->fetchLocationList($params, $sortClauses);
return $items;
$sortClauses = [ new SortClause\Location\Depth(Query::SORT_ASC) ];
return $this->fetchLocationList($params, $sortClauses);
}

/**
Expand All @@ -242,17 +232,20 @@ public function fetchAncestors($location, string $contentType): ?array
* @param Location|int $location
* @param string $contentType
* @param bool $highest
* param which specifies if fetchAncestor must return the most distant or closest ancestor
* param which specifies if fetchAncestor must return the highest ancestor in tree structure (smallest depth)
* @return Location|null
* @throws InvalidArgumentException
*/
public function fetchAncestor($location, string $contentType, bool $highest = true): ?Location
{
$results = $this->fetchAncestors($location, $contentType);
if ($highest) {
$itemHit = array_shift($results);
} elseif ($highest == false) {
$itemHit = array_pop($results);
$itemHit = null;
if (is_array($results) && count($results)) {
if ($highest) {
$itemHit = array_shift($results);
} else {
$itemHit = array_pop($results);
}
}
return ($itemHit instanceof Location) ? $itemHit : null;
}
Expand All @@ -264,7 +257,7 @@ public function fetchAncestor($location, string $contentType, bool $highest = tr
*/
private function fetchLocation(array $params): ?Location
{
$results = $this->fetchLocationList($params, 1);
$results = $this->fetchLocationList($params, null, 1);

$itemHit = reset($results);
return ($itemHit instanceof Location) ? $itemHit : null;
Expand Down Expand Up @@ -340,8 +333,7 @@ public function recursiveAncestorGetField(Location $location, string $fieldIdent
$content = $location->getContent();

// Check if $location has a relation in field
if (
$content->getContentType()->getFieldDefinition($fieldIdentifier) == null ||
if ($content->getContentType()->getFieldDefinition($fieldIdentifier) == null ||
$this->fieldhelper->isFieldEmpty($content, $fieldIdentifier)
) {
// No relation to a header object then check parent location
Expand Down
70 changes: 5 additions & 65 deletions Services/Twig/FetchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function getFunctions()
return
[
new TwigFunction('render_children', [$this, 'renderChildren'], ['is_safe' => ['all']]),
new TwigFunction('fetch_children', [$this, 'fetchChildren']),
new TwigFunction('fetch_ancestor', [$this, 'fetchAncestor']),
new TwigFunction('fetch_ancestors', [$this, 'fetchAncestors']),
new TwigFunction('fetch_content', [$this, 'fetchContent']),
new TwigFunction('fetch_location', [$this, 'fetchLocation']),
new TwigFunction('fetch_children', [$this->fetchHelper, 'fetchChildren']),
new TwigFunction('fetch_ancestor', [$this->fetchHelper, 'fetchAncestor']),
new TwigFunction('fetch_ancestors', [$this->fetchHelper, 'fetchAncestors']),
new TwigFunction('fetch_content', [$this->repository->getContentService(), 'loadContent']),
new TwigFunction('fetch_location', [$this->repository->getLocationService(), 'loadLocation']),
];
}

Expand Down Expand Up @@ -117,66 +117,6 @@ public function renderChildren(
return $render;
}

/**
* @param Location|int $parentLocation
* @param string|null $contentClass
* @return array
* @throws InvalidArgumentException
*/
public function fetchChildren($parentLocation, $contentClass = null): array
{
return $this->fetchHelper->fetchChildren($parentLocation, $contentClass);
}

/**
* Fetch ancestor of $location with specified $contentType
*
* @param Location|int $location
* @param string $contentType
* @param bool $highest
* @return Location|null
* @throws InvalidArgumentException
*/
public function fetchAncestor($location, string $contentType, bool $highest): ?Location
{
return $this->fetchHelper->fetchAncestor($location, $contentType, $highest);
}

/**
* Fetch ancestor of $location with specified $contentType
*
* @param Location|int $location
* @param string $contentType
* @return Location|null
* @throws InvalidArgumentException
*/
public function fetchAncestors($location, string $contentType): ?array
{
return $this->fetchHelper->fetchAncestors($location, $contentType);
}

/**
* @param int $contentId
* @return Content
* @throws NotFoundException
* @throws UnauthorizedException
*/
public function fetchContent(int $contentId): Content
{
return $this->repository->getContentService()->loadContent(intval($contentId));
}

/**
* @param int $locationId
* @return Location
* @throws NotFoundException
* @throws UnauthorizedException
*/
public function fetchLocation(int $locationId): Location
{
return $this->repository->getLocationService()->loadLocation(intval($locationId));
}

public function getName(): string
{
return 'sqli_twig_extension_fetch';
Expand Down

0 comments on commit 3f5c2d4

Please sign in to comment.