|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace DoctrineORMModule\Paginator\Adapter; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
23
|
|
|
use Zend\Paginator\Adapter\AdapterInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Paginator adapter for the Zend\Paginator component |
|
27
|
|
|
* |
|
28
|
|
|
* @license MIT |
|
29
|
|
|
* @link http://www.doctrine-project.org/ |
|
30
|
|
|
* @since 0.1.0 |
|
31
|
|
|
* @author Tõnis Tobre <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class DoctrinePaginator implements AdapterInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var Paginator |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $paginator; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param Paginator $paginator |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Paginator $paginator) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->paginator = $paginator; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Paginator $paginator |
|
52
|
|
|
* @return self |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setPaginator(Paginator $paginator) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->paginator = $paginator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return Paginator |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getPaginator() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->paginator; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritDoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getItems($offset, $itemCountPerPage) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->paginator |
|
73
|
|
|
->getQuery() |
|
74
|
|
|
->setFirstResult($offset) |
|
75
|
|
|
->setMaxResults($itemCountPerPage); |
|
76
|
|
|
|
|
77
|
|
|
return $this->paginator->getIterator(); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function count() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->paginator->count(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.