Skip to content

Commit

Permalink
Merge pull request #319 from garak/internal-exception
Browse files Browse the repository at this point in the history
introduce new internal exception
  • Loading branch information
garak authored Dec 19, 2022
2 parents 1e88f99 + 1106200 commit 6b6efa8
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\WhereClause;
use Doctrine\ORM\Query\TreeWalkerAdapter;
use Knp\Component\Pager\Exception\InvalidValueException;

/**
* Where Query TreeWalker for Filtration functionality
Expand Down Expand Up @@ -58,17 +59,17 @@ public function walkSelectStatement(SelectStatement $AST): void
if (2 <= count($parts)) {
$alias = trim(reset($parts));
if (!array_key_exists($alias, $components)) {
throw new \UnexpectedValueException("There is no component aliased by [{$alias}] in the given Query");
throw new InvalidValueException("There is no component aliased by [{$alias}] in the given Query");
}
$meta = $components[$alias];
if (!$meta['metadata']->hasField($field)) {
throw new \UnexpectedValueException("There is no such field [{$field}] in the given Query component, aliased by [$alias]");
throw new InvalidValueException("There is no such field [{$field}] in the given Query component, aliased by [$alias]");
}
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD, $alias, $field);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
} else {
if (!array_key_exists($field, $components)) {
throw new \UnexpectedValueException("There is no component field [{$field}] in the given Query");
throw new InvalidValueException("There is no component field [{$field}] in the given Query");
}
$pathExpression = $components[$field]['resultVariable'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM\Query\WhereWalker;
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function items(ItemsEvent $event): void
if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
foreach ($columns as $column) {
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in allow list");
throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function items(ItemsEvent $event): void
if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
foreach ($columns as $column) {
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in allow list");
throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function items(ItemsEvent $event): void
$event->setCustomPaginationParameter('sorted', true);

if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
}

$sortFunction = $event->options['sortFunction'] ?? [$this, 'proxySortFunction'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ODM\MongoDB\Query\Query;
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -35,7 +36,7 @@ public function items(ItemsEvent $event): void

if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
if (!in_array($field, $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot sort by: [{$field}] this field is not in allow list.");
throw new InvalidValueException("Cannot sort by: [{$field}] this field is not in allow list.");
}
}
static $reflectionProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Doctrine\ORM\Query\AST\PathExpression,
Doctrine\ORM\Query\AST\SelectStatement,
Doctrine\ORM\Query\TreeWalkerAdapter;
use Knp\Component\Pager\Exception\InvalidValueException;

/**
* OrderBy Query TreeWalker for Sortable functionality
Expand Down Expand Up @@ -48,15 +49,15 @@ public function walkSelectStatement(SelectStatement $AST): string
$alias = $aliases[$index];
if ($alias !== false) {
if (!array_key_exists($alias, $components)) {
throw new \UnexpectedValueException("There is no component aliased by [{$alias}] in the given Query");
throw new InvalidValueException("There is no component aliased by [{$alias}] in the given Query");
}
$meta = $components[$alias];
if (!$meta['metadata']->hasField($field)) {
throw new \UnexpectedValueException("There is no such field [{$field}] in the given Query component, aliased by [$alias]");
throw new InvalidValueException("There is no such field [{$field}] in the given Query component, aliased by [$alias]");
}
} else {
if (!array_key_exists($field, $components)) {
throw new \UnexpectedValueException("There is no component field [{$field}] in the given Query");
throw new InvalidValueException("There is no component field [{$field}] in the given Query");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
use Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -36,15 +37,15 @@ public function items(ItemsEvent $event): void

if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
if (!in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
}
}

$sortFieldParameterNames = $this->argumentAccess->get($sortField);
$fields = [];
$aliases = [];
if (!is_string($sortFieldParameterNames)) {
throw new \UnexpectedValueException('Cannot sort with array parameter.');
throw new InvalidValueException('Cannot sort with array parameter.');
}

foreach (explode('+', $sortFieldParameterNames) as $sortFieldParameterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\SearchableInterface;
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -36,7 +37,7 @@ public function items(ItemsEvent $event): void
$dir = null !== $sortDir && $this->argumentAccess->has($sortDir) && strtolower($this->argumentAccess->get($sortDir)) === 'asc' ? 'asc' : 'desc';

if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($field, $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in allow list.', $field));
throw new InvalidValueException(sprintf('Cannot sort by: [%s] this field is not in allow list.', $field));
}

$query->setSort([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -36,7 +37,7 @@ public function items(ItemsEvent $event): void

if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
if (!in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Exception\InvalidValueException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -39,7 +40,7 @@ public function items(ItemsEvent $event): void
if (null !== $sortField && $this->argumentAccess->has($sortField)) {
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
if (!in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
throw new \UnexpectedValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Knp/Component/Pager/Exception/InvalidValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Knp\Component\Pager\Exception;

class InvalidValueException extends \UnexpectedValueException
{
}

0 comments on commit 6b6efa8

Please sign in to comment.