Skip to content

Commit b513b79

Browse files
authored
Merge pull request #177 from ostrolucky/constants
replace common magic strings with constants
2 parents 8ac9e19 + 08f81cc commit b513b79

21 files changed

+136
-106
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ cache:
77
- $HOME/.composer/cache
88

99
php:
10-
- 5.3
1110
- 5.4
1211
- 5.5
1312
- 5.6
1413
- 7.0
1514
- hhvm
1615

1716
matrix:
17+
include:
18+
- php: 5.3
19+
dist: precise
1820
allow_failures:
1921
- php: 7.0
2022
- php: hhvm

src/Knp/Component/Pager/Event/Subscriber/Filtration/Doctrine/ORM/QuerySubscriber.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,35 @@
66
use Knp\Component\Pager\Event\ItemsEvent;
77
use Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM\Query\WhereWalker;
88
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
9+
use Knp\Component\Pager\PaginatorInterface;
910
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1011

1112
class QuerySubscriber implements EventSubscriberInterface
1213
{
1314
public function items(ItemsEvent $event)
1415
{
1516
if ($event->target instanceof Query) {
16-
if (!isset($_GET[$event->options['filterValueParameterName']]) || (empty($_GET[$event->options['filterValueParameterName']]) && $_GET[$event->options['filterValueParameterName']] !== "0")) {
17+
if (!isset($_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]]) || (empty($_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]]) && $_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]] !== "0")) {
1718
return;
1819
}
19-
if (!empty($_GET[$event->options['filterFieldParameterName']])) {
20-
$columns = $_GET[$event->options['filterFieldParameterName']];
21-
} elseif (!empty($event->options['defaultFilterFields'])) {
22-
$columns = $event->options['defaultFilterFields'];
20+
if (!empty($_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]])) {
21+
$columns = $_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]];
22+
} elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
23+
$columns = $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
2324
} else {
2425
return;
2526
}
26-
$value = $_GET[$event->options['filterValueParameterName']];
27+
$value = $_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]];
2728
if (false !== strpos($value, '*')) {
2829
$value = str_replace('*', '%', $value);
2930
}
3031
if (is_string($columns) && false !== strpos($columns, ',')) {
3132
$columns = explode(',', $columns);
3233
}
3334
$columns = (array) $columns;
34-
if (isset($event->options['filterFieldWhitelist'])) {
35+
if (isset($event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
3536
foreach ($columns as $column) {
36-
if (!in_array($column, $event->options['filterFieldWhitelist'])) {
37+
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
3738
throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in whitelist");
3839
}
3940
}

src/Knp/Component/Pager/Event/Subscriber/Filtration/PropelQuerySubscriber.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Knp\Component\Pager\Event\Subscriber\Filtration;
44

5+
use Knp\Component\Pager\PaginatorInterface;
56
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
67
use Knp\Component\Pager\Event\ItemsEvent;
78

@@ -11,28 +12,28 @@ public function items(ItemsEvent $event)
1112
{
1213
$query = $event->target;
1314
if ($query instanceof \ModelCriteria) {
14-
if (empty($_GET[$event->options['filterValueParameterName']])) {
15+
if (empty($_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]])) {
1516
return;
1617
}
17-
if (!empty($_GET[$event->options['filterFieldParameterName']])) {
18-
$columns = $_GET[$event->options['filterFieldParameterName']];
19-
} elseif (!empty($event->options['defaultFilterFields'])) {
20-
$columns = $event->options['defaultFilterFields'];
18+
if (!empty($_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]])) {
19+
$columns = $_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]];
20+
} elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
21+
$columns = $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
2122
} else {
2223
return;
2324
}
2425
if (is_string($columns) && false !== strpos($columns, ',')) {
2526
$columns = explode(',', $columns);
2627
}
2728
$columns = (array) $columns;
28-
if (isset($event->options['filterFieldWhitelist'])) {
29+
if (isset($event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
2930
foreach ($columns as $column) {
30-
if (!in_array($column, $event->options['filterFieldWhitelist'])) {
31+
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
3132
throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in whitelist");
3233
}
3334
}
3435
}
35-
$value = $_GET[$event->options['filterValueParameterName']];
36+
$value = $_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]];
3637
$criteria = \Criteria::EQUAL;
3738
if (false !== strpos($value, '*')) {
3839
$value = str_replace('*', '%', $value);

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ORM/QuerySubscriber.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function items(ItemsEvent $event)
5353
if ($useDoctrineWalkers) {
5454
$countQuery->setHint(
5555
DoctrineCountWalker::HINT_DISTINCT,
56-
$event->options['distinct']
56+
$event->options[PaginatorInterface::DISTINCT]
5757
);
5858
} else {
5959
$countQuery->setHint(
6060
CountWalker::HINT_DISTINCT,
61-
$event->options['distinct']
61+
$event->options[PaginatorInterface::DISTINCT]
6262
);
6363
}
6464
$countQuery
@@ -75,7 +75,7 @@ public function items(ItemsEvent $event)
7575
// process items
7676
$result = null;
7777
if ($event->count) {
78-
if ($event->options['distinct']) {
78+
if ($event->options[PaginatorInterface::DISTINCT]) {
7979
$limitSubQuery = QueryHelper::cloneQuery($event->target);
8080
$limitSubQuery
8181
->setFirstResult($event->getOffset())

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ORM/QuerySubscriber/UsesPaginator.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\QuerySubscriber;
44

5+
use Knp\Component\Pager\PaginatorInterface;
56
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
67
use Knp\Component\Pager\Event\ItemsEvent;
78
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\QuerySubscriber;
@@ -31,14 +32,14 @@ public function items(ItemsEvent $event)
3132
$event->target
3233
->setFirstResult($event->getOffset())
3334
->setMaxResults($event->getLimit())
34-
->setHint(CountWalker::HINT_DISTINCT, $event->options['distinct'])
35+
->setHint(CountWalker::HINT_DISTINCT, $event->options[PaginatorInterface::DISTINCT])
3536
;
3637

3738
$fetchJoinCollection = true;
3839
if ($event->target->hasHint(self::HINT_FETCH_JOIN_COLLECTION)) {
3940
$fetchJoinCollection = $event->target->getHint(self::HINT_FETCH_JOIN_COLLECTION);
40-
} else if (isset($event->options['distinct'])) {
41-
$fetchJoinCollection = $event->options['distinct'];
41+
} else if (isset($event->options[PaginatorInterface::DISTINCT])) {
42+
$fetchJoinCollection = $event->options[PaginatorInterface::DISTINCT];
4243
}
4344

4445
$paginator = new Paginator($event->target, $fetchJoinCollection);

src/Knp/Component/Pager/Event/Subscriber/Paginate/PropelQuerySubscriber.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public function items(ItemsEvent $event)
1818
->limit(0)
1919
->offset(0)
2020
;
21-
if ($event->options['distinct']) {
21+
if ($event->options[PaginatorInterface::DISTINCT]) {
2222
$countQuery->distinct();
2323
}
2424
$event->count = intval($countQuery->count());
2525
// process items
2626
$result = null;
2727
if ($event->count) {
2828
$resultQuery = clone $event->target;
29-
if ($event->options['distinct']) {
29+
if ($event->options[PaginatorInterface::DISTINCT]) {
3030
$resultQuery->distinct();
3131
}
3232
$resultQuery

src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
77
use Symfony\Component\PropertyAccess\PropertyAccess;
88
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
9+
use Knp\Component\Pager\PaginatorInterface;
910

1011
class ArraySubscriber implements EventSubscriberInterface
1112
{
@@ -35,16 +36,16 @@ public function __construct(PropertyAccessorInterface $accessor = null)
3536

3637
public function items(ItemsEvent $event)
3738
{
38-
if (!is_array($event->target) || empty($_GET[$event->options['sortFieldParameterName']])) {
39+
if (!is_array($event->target) || empty($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
3940
return;
4041
}
4142

42-
if (isset($event->options['sortFieldWhitelist']) && !in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
43-
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
43+
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST]) && !in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
44+
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
4445
}
4546

4647
$sortFunction = isset($event->options['sortFunction']) ? $event->options['sortFunction'] : array($this, 'proxySortFunction');
47-
$sortField = $_GET[$event->options['sortFieldParameterName']];
48+
$sortField = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
4849

4950
// compatibility layer
5051
if ($sortField[0] === '.') {
@@ -54,7 +55,7 @@ public function items(ItemsEvent $event)
5455
call_user_func_array($sortFunction, array(
5556
&$event->target,
5657
$sortField,
57-
isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc'
58+
isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' ? 'asc' : 'desc'
5859
));
5960
}
6061

src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ODM/MongoDB/QuerySubscriber.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
66
use Knp\Component\Pager\Event\ItemsEvent;
77
use Doctrine\ODM\MongoDB\Query\Query;
8+
use Knp\Component\Pager\PaginatorInterface;
89

910
class QuerySubscriber implements EventSubscriberInterface
1011
{
1112
public function items(ItemsEvent $event)
1213
{
1314
if ($event->target instanceof Query) {
14-
if (isset($_GET[$event->options['sortFieldParameterName']])) {
15-
$field = $_GET[$event->options['sortFieldParameterName']];
16-
$dir = strtolower($_GET[$event->options['sortDirectionParameterName']]) == 'asc' ? 1 : -1;
15+
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
16+
$field = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
17+
$dir = strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) == 'asc' ? 1 : -1;
1718

18-
if (isset($event->options['sortFieldWhitelist'])) {
19-
if (!in_array($field, $event->options['sortFieldWhitelist'])) {
19+
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
20+
if (!in_array($field, $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
2021
throw new \UnexpectedValueException("Cannot sort by: [{$field}] this field is not in whitelist");
2122
}
2223
}

src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/QuerySubscriber.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
use Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker;
88
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
99
use Doctrine\ORM\Query;
10+
use Knp\Component\Pager\PaginatorInterface;
1011

1112
class QuerySubscriber implements EventSubscriberInterface
1213
{
1314
public function items(ItemsEvent $event)
1415
{
1516
if ($event->target instanceof Query) {
16-
if (isset($_GET[$event->options['sortFieldParameterName']])) {
17-
$dir = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
17+
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
18+
$dir = isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' ? 'asc' : 'desc';
1819

19-
if (isset($event->options['sortFieldWhitelist'])) {
20-
if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
21-
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
20+
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
21+
if (!in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
22+
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
2223
}
2324
}
2425

25-
$sortFieldParameterNames = $_GET[$event->options['sortFieldParameterName']];
26+
$sortFieldParameterNames = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
2627
$fields = array();
2728
$aliases = array();
2829
foreach (explode('+', $sortFieldParameterNames) as $sortFieldParameterName) {

src/Knp/Component/Pager/Event/Subscriber/Sortable/ElasticaQuerySubscriber.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Knp\Component\Pager\Event\ItemsEvent;
77
use Elastica\Query;
88
use Elastica\SearchableInterface;
9+
use Knp\Component\Pager\PaginatorInterface;
910

1011
class ElasticaQuerySubscriber implements EventSubscriberInterface
1112
{
@@ -14,11 +15,11 @@ public function items(ItemsEvent $event)
1415
if (is_array($event->target) && 2 === count($event->target) && reset($event->target) instanceof SearchableInterface && end($event->target) instanceof Query) {
1516
list($searchable, $query) = $event->target;
1617

17-
if (isset($_GET[$event->options['sortFieldParameterName']])) {
18-
$field = $_GET[$event->options['sortFieldParameterName']];
19-
$dir = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
18+
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
19+
$field = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
20+
$dir = isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' ? 'asc' : 'desc';
2021

21-
if (isset($event->options['sortFieldWhitelist']) && !in_array($field, $event->options['sortFieldWhitelist'])) {
22+
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST]) && !in_array($field, $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
2223
throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist',$field));
2324
}
2425

src/Knp/Component/Pager/Event/Subscriber/Sortable/PropelQuerySubscriber.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Knp\Component\Pager\Event\Subscriber\Sortable;
44

5+
use Knp\Component\Pager\PaginatorInterface;
56
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
67
use Knp\Component\Pager\Event\ItemsEvent;
78

@@ -11,16 +12,16 @@ public function items(ItemsEvent $event)
1112
{
1213
$query = $event->target;
1314
if ($query instanceof \ModelCriteria) {
14-
if (isset($_GET[$event->options['sortFieldParameterName']])) {
15-
$part = $_GET[$event->options['sortFieldParameterName']];
16-
$directionParam = $event->options['sortDirectionParameterName'];
15+
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
16+
$part = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
17+
$directionParam = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
1718

1819
$direction = (isset($_GET[$directionParam]) && strtolower($_GET[$directionParam]) === 'asc')
1920
? 'asc' : 'desc';
2021

21-
if (isset($event->options['sortFieldWhitelist'])) {
22-
if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
23-
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
22+
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
23+
if (!in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
24+
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
2425
}
2526
}
2627

0 commit comments

Comments
 (0)