1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
use \KGzocha\Searcher\Criteria\CriteriaInterface; |
4
|
|
|
use \KGzocha\Searcher\CriteriaBuilder\CriteriaBuilderInterface; |
5
|
|
|
use \KGzocha\Searcher\Context\Doctrine\QueryBuilderSearchingContext; |
6
|
|
|
use \KGzocha\Searcher\Context\SearchingContextInterface; |
7
|
|
|
use \KGzocha\Searcher\CriteriaBuilder\Collection\CriteriaBuilderCollection; |
8
|
|
|
use \KGzocha\Searcher\Criteria\Collection\CriteriaCollection; |
9
|
|
|
use \KGzocha\Searcher\Searcher; |
10
|
|
|
|
11
|
|
|
class HeightCriteria implements CriteriaInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var float height in meters |
15
|
|
|
*/ |
16
|
|
|
private $height; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param null|float $height |
20
|
|
|
*/ |
21
|
|
|
public function __construct($height = null) |
22
|
|
|
{ |
23
|
|
|
$this->height = $height; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return float |
28
|
|
|
*/ |
29
|
|
|
public function getHeight() |
30
|
|
|
{ |
31
|
|
|
return $this->height; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param float $height |
36
|
|
|
*/ |
37
|
|
|
public function setHeight($height) |
38
|
|
|
{ |
39
|
|
|
$this->height = (float) $height; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function shouldBeApplied() |
46
|
|
|
{ |
47
|
|
|
return $this->height != null; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
class HeightCriteriaBuilder implements CriteriaBuilderInterface |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @param HeightCriteria $criteria |
55
|
|
|
* @param QueryBuilderSearchingContext $searchingContext |
56
|
|
|
*/ |
57
|
|
|
public function buildCriteria( |
58
|
|
|
CriteriaInterface $criteria, |
59
|
|
|
SearchingContextInterface $searchingContext |
60
|
|
|
) { |
61
|
|
|
$searchingContext |
62
|
|
|
->getQueryBuilder() |
63
|
|
|
->andWhere('t.height = :number') |
64
|
|
|
->setParameter('number', $criteria->getHeight()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function allowsCriteria(CriteriaInterface $criteria) |
71
|
|
|
{ |
72
|
|
|
return $criteria instanceof HeightCriteria; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function supportsSearchingContext( |
79
|
|
|
SearchingContextInterface $searchingContext |
80
|
|
|
) { |
81
|
|
|
return $searchingContext instanceof QueryBuilderSearchingContext; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @var \Doctrine\ORM\QueryBuilder $queryBuilder */ |
86
|
|
|
$queryBuilder = /** lets assume its already created QueryBuilder */null; |
87
|
|
|
|
88
|
|
|
$criteria = new HeightCriteria(200); |
89
|
|
|
$criteriaBuilder = new HeightCriteriaBuilder(); |
90
|
|
|
$context = new QueryBuilderSearchingContext($queryBuilder); |
91
|
|
|
|
92
|
|
|
$searcher = new Searcher( |
93
|
|
|
new CriteriaBuilderCollection([$criteriaBuilder]), |
94
|
|
|
$context |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$results = $searcher->search(new CriteriaCollection([$criteria])); |
98
|
|
|
|
99
|
|
|
foreach ($results as $result) { |
100
|
|
|
var_dump($result); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.