1 | <?php |
||
37 | class PageImpl extends Object implements IteratorAggregate, Page |
||
38 | { |
||
39 | private $content = []; |
||
40 | |||
41 | /** |
||
42 | * @var Pageable |
||
43 | */ |
||
44 | private $pageable; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $total; |
||
50 | |||
51 | /** |
||
52 | * @param array $content |
||
53 | * @param Pageable $pageable |
||
54 | * @param int $total Total number of all elements (not just the current page's) |
||
55 | */ |
||
56 | public function __construct(array $content, Pageable $pageable = null, $total = null) |
||
62 | |||
63 | /** |
||
64 | * @return array of the records in the current page |
||
65 | */ |
||
66 | public function getContent() |
||
70 | |||
71 | /** |
||
72 | * @return int The page number |
||
73 | */ |
||
74 | public function getNumber() |
||
80 | |||
81 | /** |
||
82 | * @return int size of the content |
||
83 | */ |
||
84 | public function getSize() |
||
90 | |||
91 | public function getSort() |
||
97 | |||
98 | public function getTotalElements() |
||
102 | |||
103 | public function getTotalPages() |
||
109 | |||
110 | public function hasContent() |
||
114 | |||
115 | public function hasNextPage() |
||
119 | |||
120 | public function hasPreviousPage() |
||
124 | |||
125 | public function isFirstPage() |
||
129 | |||
130 | public function isLastPage() |
||
134 | |||
135 | /** |
||
136 | * Create a Pageable object to be able to obtain the next page. |
||
137 | * |
||
138 | * @return Pageable |
||
139 | */ |
||
140 | public function nextPageable() |
||
146 | |||
147 | /** |
||
148 | * Create a Pageable object to be able to obtain the previous page. |
||
149 | * |
||
150 | * @return Pageable |
||
151 | */ |
||
152 | public function previousPageable() |
||
158 | |||
159 | /** |
||
160 | * @return ArrayIterator|\Traversable |
||
161 | */ |
||
162 | public function getIterator() |
||
166 | |||
167 | public function equals(ObjectInterface $object = null) |
||
179 | |||
180 | public function toString() |
||
187 | } |
||
188 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.