o2system /
database
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Database\DataObjects; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Database\DataObjects\Result\Info; |
||||
| 19 | use O2System\Database\DataObjects\Result\Row; |
||||
| 20 | use O2System\Spl\DataStructures\Traits\ArrayConversionTrait; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * Class Result |
||||
| 24 | * |
||||
| 25 | * @package O2System\Database\DataObjects |
||||
| 26 | */ |
||||
| 27 | class Result extends \SplFixedArray |
||||
| 28 | {
|
||||
| 29 | use ArrayConversionTrait; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * Result::$info |
||||
| 33 | * |
||||
| 34 | * @var Info |
||||
| 35 | */ |
||||
| 36 | protected $info; |
||||
| 37 | |||||
| 38 | // ------------------------------------------------------------------------ |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Result::__construct |
||||
| 42 | * |
||||
| 43 | * @param array $rows |
||||
| 44 | */ |
||||
| 45 | public function __construct(array $rows) |
||||
| 46 | {
|
||||
| 47 | $this->info = new Info(); |
||||
| 48 | $this->info->num_total = $this->info->num_rows = count($rows); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 49 | |||||
| 50 | parent::__construct($this->info->num_rows); |
||||
|
0 ignored issues
–
show
The property
num_rows does not exist on O2System\Database\DataObjects\Result\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 51 | |||||
| 52 | foreach($rows as $offset => $row) {
|
||||
| 53 | $this->offsetSet($offset, $row); |
||||
| 54 | } |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | // ------------------------------------------------------------------------ |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Result::setNumPerPage |
||||
| 61 | * |
||||
| 62 | * @param int $numPerPage |
||||
| 63 | */ |
||||
| 64 | public function setNumPerPage($numPerPage) |
||||
| 65 | {
|
||||
| 66 | $this->info->num_per_page = (int)$numPerPage; |
||||
|
0 ignored issues
–
show
|
|||||
| 67 | } |
||||
| 68 | |||||
| 69 | // ------------------------------------------------------------------------ |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * Result::setNumFoundRows |
||||
| 73 | * |
||||
| 74 | * @param int $numFounds |
||||
| 75 | */ |
||||
| 76 | public function setNumFounds($numFounds) |
||||
| 77 | {
|
||||
| 78 | $this->info->num_founds = (int)$numFounds; |
||||
|
0 ignored issues
–
show
|
|||||
| 79 | |||||
| 80 | if($this->info->num_founds > 0 and $this->info->num_per_page > 0) {
|
||||
|
0 ignored issues
–
show
The property
num_founds does not exist on O2System\Database\DataObjects\Result\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
The property
num_per_page does not exist on O2System\Database\DataObjects\Result\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 81 | $this->info->num_pages = round($this->info->num_founds / $this->info->num_per_page); |
||||
|
0 ignored issues
–
show
|
|||||
| 82 | } |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | // ------------------------------------------------------------------------ |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Result::setTotalRows |
||||
| 89 | * |
||||
| 90 | * @param int $totalRows |
||||
| 91 | */ |
||||
| 92 | public function setNumTotal($totalRows) |
||||
| 93 | {
|
||||
| 94 | $this->info->num_total = (int)$totalRows; |
||||
|
0 ignored issues
–
show
|
|||||
| 95 | } |
||||
| 96 | |||||
| 97 | // ------------------------------------------------------------------------ |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * Result::first |
||||
| 101 | * |
||||
| 102 | * Gets first result row data. |
||||
| 103 | * |
||||
| 104 | * @return \O2System\Database\DataObjects\Result\Row|null |
||||
| 105 | */ |
||||
| 106 | public function first() |
||||
| 107 | {
|
||||
| 108 | if($this->count()) {
|
||||
| 109 | $this->rewind(); |
||||
| 110 | |||||
| 111 | return $this->current(); |
||||
| 112 | } |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | // ------------------------------------------------------------------------ |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Result::last |
||||
| 119 | * |
||||
| 120 | * Gets last result row data. |
||||
| 121 | * |
||||
| 122 | * @return \O2System\Database\DataObjects\Result\Row|null |
||||
| 123 | */ |
||||
| 124 | public function last() |
||||
| 125 | {
|
||||
| 126 | if($this->count()) {
|
||||
| 127 | $index = $this->count() - 1; |
||||
| 128 | |||||
| 129 | if ($this->offsetExists($index)) {
|
||||
| 130 | return $this->offsetGet($index); |
||||
| 131 | } |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | // ------------------------------------------------------------------------ |
||||
| 136 | |||||
| 137 | /** |
||||
| 138 | * Result::previous |
||||
| 139 | * |
||||
| 140 | * Move backward to previous element. |
||||
| 141 | * |
||||
| 142 | * @return void Any returned value is ignored. |
||||
| 143 | * @since 5.0.0 |
||||
| 144 | */ |
||||
| 145 | public function previous() |
||||
| 146 | {
|
||||
| 147 | prev($this); |
||||
|
0 ignored issues
–
show
$this of type O2System\Database\DataObjects\Result is incompatible with the type array expected by parameter $array of prev().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 148 | } |
||||
| 149 | |||||
| 150 | // ------------------------------------------------------------------------ |
||||
| 151 | |||||
| 152 | /** |
||||
| 153 | * Result::isEmpty |
||||
| 154 | * |
||||
| 155 | * Checks if the array storage is empty. |
||||
| 156 | * |
||||
| 157 | * @return bool |
||||
| 158 | */ |
||||
| 159 | public function isEmpty() |
||||
| 160 | {
|
||||
| 161 | return ($this->count() == 0 ? true : false); |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | // ------------------------------------------------------------------------ |
||||
| 165 | |||||
| 166 | /** |
||||
| 167 | * Result::getArrayCopy |
||||
| 168 | * |
||||
| 169 | * Creates a copy of result rows. |
||||
| 170 | * |
||||
| 171 | * @return array A copy of the result rows. |
||||
| 172 | */ |
||||
| 173 | public function getArrayCopy() |
||||
| 174 | {
|
||||
| 175 | return $this->toArray(); |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | // ------------------------------------------------------------------------ |
||||
| 179 | |||||
| 180 | /** |
||||
| 181 | * Result::offsetSet |
||||
| 182 | * |
||||
| 183 | * Offset to set |
||||
| 184 | * |
||||
| 185 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||||
| 186 | * |
||||
| 187 | * @param mixed $offset <p> |
||||
| 188 | * The offset to assign the value to. |
||||
| 189 | * </p> |
||||
| 190 | * @param mixed $value <p> |
||||
| 191 | * The value to set. |
||||
| 192 | * </p> |
||||
| 193 | * |
||||
| 194 | * @return void |
||||
| 195 | * @since 5.0.0 |
||||
| 196 | */ |
||||
| 197 | public function offsetSet($offset, $value) |
||||
| 198 | {
|
||||
| 199 | if(is_array($value)) {
|
||||
| 200 | parent::offsetSet($offset, new Row($value)); |
||||
| 201 | } else {
|
||||
| 202 | parent::offsetSet($offset, $value); |
||||
| 203 | } |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | // ------------------------------------------------------------------------ |
||||
| 207 | |||||
| 208 | /** |
||||
| 209 | * Result::__get |
||||
| 210 | * |
||||
| 211 | * @param string $property |
||||
| 212 | * |
||||
| 213 | * @return mixed |
||||
| 214 | */ |
||||
| 215 | public function __get($property) |
||||
| 216 | {
|
||||
| 217 | if (property_exists($this, $property)) {
|
||||
| 218 | return $this->{$property};
|
||||
| 219 | } |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | // ------------------------------------------------------------------------ |
||||
| 223 | |||||
| 224 | /** |
||||
| 225 | * Result::getInfo |
||||
| 226 | * |
||||
| 227 | * @return \O2System\Database\DataObjects\Result\Info |
||||
| 228 | */ |
||||
| 229 | public function getInfo() |
||||
| 230 | {
|
||||
| 231 | return $this->info; |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | // ------------------------------------------------------------------------ |
||||
| 235 | |||||
| 236 | /** |
||||
| 237 | * Result::countAll |
||||
| 238 | * |
||||
| 239 | * Count all elements |
||||
| 240 | * |
||||
| 241 | * @return int Total row as an integer. |
||||
| 242 | * </p> |
||||
| 243 | * <p> |
||||
| 244 | * The return value is cast to an integer. |
||||
| 245 | * |
||||
| 246 | */ |
||||
| 247 | public function countAll() |
||||
| 248 | {
|
||||
| 249 | return $this->info->num_founds; |
||||
|
0 ignored issues
–
show
The property
num_founds does not exist on O2System\Database\DataObjects\Result\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 250 | } |
||||
| 251 | } |