Skip to content

Commit

Permalink
Fixed the query builder of the generic params applying, added the def…
Browse files Browse the repository at this point in the history
…ault order to the passed params, added uuid to the validators.
  • Loading branch information
ruslanbaidan committed Sep 26, 2024
1 parent 385d042 commit 7a0940e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/InputFormatter/AbstractInputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class AbstractInputFormatter
protected static array $orderParamsToFieldsMap = [];

/**
* The default order fields string.
* The default order fields string. It is always added to the query in addition the order list request param.
* Ex. '-position:name:-date'. '-' means descending order, ':' fields separation.
*/
protected static string $defaultOrderFields = '';
Expand Down Expand Up @@ -153,7 +153,13 @@ protected function processInputParams(array $inputParams): FormattedInputParams
}

/* Add order params. */
$orderFields = $inputParams['order'] ?? static::$defaultOrderFields;
$orderFields = !empty($inputParams['order']) ? $inputParams['order'] : '';
/* Merge with the default ones in case if set. */
if (!empty(static::$defaultOrderFields)) {
$orderFields = $orderFields === ''
? static::$defaultOrderFields
: $orderFields .':' . static::$defaultOrderFields;
}
if (!empty($orderFields)) {
$orderFields = str_contains($orderFields, ':')
? explode(':', $orderFields)
Expand Down
2 changes: 2 additions & 0 deletions src/InputFormatter/Amv/GetAmvsInputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ class GetAmvsInputFormatter extends AbstractInputFormatter
'threat' => 'threat.code',
'vulnerability' => 'vulnerability.code',
];

public static string $defaultOrderFields = 'position';
}
10 changes: 5 additions & 5 deletions src/Table/AbstractTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,14 @@ protected function applyFilter(
return $this;
}

$fieldValueName = str_contains($field, '.') ? explode('.', $field)[1] : $field;
$whereCondition = $fieldNameWithAlias . ' ' . $operator . ' :' . $fieldValueName;
$paramName = str_contains($field, '.') ? str_replace('.', '_', $field) : $field;
$whereCondition = $fieldNameWithAlias . ' ' . $operator . ' :' . $paramName;
if (\is_array($filterParams['value'])
&& \in_array($operator, [Comparison::IN, Comparison::NIN], true)
) {
$whereCondition = $operator === Comparison::IN
? $queryBuilder->expr()->in($fieldNameWithAlias, ':' . $fieldValueName)
: $queryBuilder->expr()->notIn($fieldNameWithAlias, ':' . $fieldValueName);
? $queryBuilder->expr()->in($fieldNameWithAlias, ':' . $paramName)
: $queryBuilder->expr()->notIn($fieldNameWithAlias, ':' . $paramName);
}

/* Used for the 2 fields relation to be able to add the anr property to the joining tables. */
Expand All @@ -435,7 +435,7 @@ protected function applyFilter(

$queryBuilder
->andWhere($whereCondition)
->setParameter($fieldValueName, $filterParams['value']);
->setParameter($paramName, $filterParams['value']);

return $this;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Validator/InputValidator/Asset/PostAssetDataInputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function __construct(
protected function getRules(): array
{
$rules = [
[
'name' => 'uuid',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'code',
'required' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ protected function getRules(): array
}

$rules = [
[
'name' => 'uuid',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'code',
'required' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public function __construct(
protected function getRules(): array
{
$rules = [
[
'name' => 'uuid',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'code',
'required' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public function __construct(
protected function getRules(): array
{
$rules = [
[
'name' => 'uuid',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'code',
'required' => true,
Expand Down

0 comments on commit 7a0940e

Please sign in to comment.