Skip to content

Commit

Permalink
exclude post list from the default of saved search query
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi committed Jan 21, 2025
1 parent e838a53 commit c41989d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static function fromRequest(int $id, Request $request): self
throw new \InvalidArgumentException('Id must be a positive number');
}
$query = new self($id);
$query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS);
$excluded_relations = ['posts'];
$query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS, $excluded_relations);
return $query;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static function fromRequest(Request $request): self
$query = new self();
$query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT);
$query->setSearchFields(new SavedSearchSearchFields($request));
$query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS);
$excluded_relations = ['posts'];
$query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS, $excluded_relations);
return $query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait QueryWithOnlyParameter
{
/**
/**
* @var array
*/
private $fields;
Expand Down Expand Up @@ -46,10 +46,14 @@ public function getFieldsForRelationship(): array
* @param Request $request
* @param array $approved_fields
*/
private function getOnlyValuesFromRequest(Request $request, array $allowed_fields, array $allowed_relations): void
{
private function getOnlyValuesFromRequest(
Request $request,
array $allowed_fields,
array $allowed_relations,
array $excluded_relations = [] // exclude them if they are not in only in request
): void {
$this->fields = $allowed_fields;
$this->hydrates = array_keys($allowed_relations);
$this->hydrates = array_diff(array_keys($allowed_relations), $excluded_relations);

if ($request->query('format') === 'minimal') {
$this->fields = ['id', 'name', 'description', 'translations'];
Expand Down Expand Up @@ -88,15 +92,24 @@ private function getNeededRelations(array $allowed_relations)
}
}
}
public function addOnlyParameteresFromRequest(Request $request, array $allowed_fields, array $allowed_relations, array $required_fields): void
{
$this->getOnlyValuesFromRequest($request, $allowed_fields, $allowed_relations);
public function addOnlyParameteresFromRequest(
Request $request,
array $allowed_fields,
array $allowed_relations,
array $required_fields,
array $relations_to_prune = [] // exclude them if they are not in request
): void {
$this->getOnlyValuesFromRequest($request, $allowed_fields, $allowed_relations, $relations_to_prune);
$this->fields = array_unique(array_merge($this->fields, $required_fields));
$this->getNeededRelations($allowed_relations);
}

public function addOnlyValues(array $fields, array $hydrates, array $allowed_relations, array $required_fields)
{

public function addOnlyValues(
array $fields,
array $hydrates,
array $allowed_relations,
array $required_fields
) {
$this->fields = $fields;
$this->hydrates = $hydrates;
$this->fields = array_unique(array_merge($this->fields, $required_fields));
Expand Down

0 comments on commit c41989d

Please sign in to comment.