Subversion Repositories oidplus

Rev

Rev 1151 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusNaturalSortedQueryResult extends OIDplusQueryResult {
  27.  
  28.         /**
  29.         * @var array
  30.         */
  31.         private $rows = [];
  32.  
  33.         /**
  34.          * @return bool
  35.          */
  36.         private $no_resultset = false;
  37.  
  38.         /**
  39.          * @return int
  40.          */
  41.         private $cursor = 0;
  42.  
  43.         /**
  44.          * @return int
  45.          */
  46.         private $record_count = 0;
  47.  
  48.         /**
  49.         * @param OIDplusQueryResult $res
  50.         * @param string $dbField
  51.         */
  52.         public function __construct(OIDplusQueryResult $res, string $dbField) {
  53.                 $this->no_resultset = !$res->containsResultSet();
  54.  
  55.                 if (!$this->no_resultset) {
  56.                         $this->rows = array();
  57.                         while ($row = $res->fetch_array()) {
  58.                                 $this->rows[] = $row;
  59.                         }
  60.                         $this->cursor = 0;
  61.                         $this->record_count = count($this->rows);
  62.  
  63.                         // Sort $this->rows by field $dbField
  64.                         natsort_field($this->rows, $dbField);
  65.                 } else {
  66.                         $this->rows = array();
  67.                         $this->cursor = 0;
  68.                         $this->record_count = 0;
  69.                 }
  70.         }
  71.  
  72.         /**
  73.          * @return bool
  74.          */
  75.         public function containsResultSet(): bool {
  76.                 return !$this->no_resultset;
  77.         }
  78.  
  79.         /**
  80.          * @return int
  81.          */
  82.         protected function do_num_rows(): int {
  83.                 return count($this->rows);
  84.         }
  85.  
  86.         /**
  87.          * @return array|null
  88.          */
  89.         protected function do_fetch_array()/*: ?array*/ {
  90.                 //return array_shift($this->rows);
  91.                 // This is probably faster for large arrays:
  92.                 if ($this->cursor >= $this->record_count) return null;
  93.                 return $this->rows[$this->cursor++];
  94.         }
  95.  
  96. }
  97.