Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
236 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
236 daniel-mar 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
511 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
730 daniel-mar 26
abstract class OIDplusQueryResult extends OIDplusBaseClass {
1116 daniel-mar 27
 
28
        /**
29
         * @return bool
30
         */
236 daniel-mar 31
        abstract public function containsResultSet(): bool;
1116 daniel-mar 32
 
33
        /**
34
         * @return int
35
         */
1152 daniel-mar 36
        abstract protected function do_num_rows(): int;
1116 daniel-mar 37
 
38
        /**
1152 daniel-mar 39
         * @return int
40
         */
41
        public final function num_rows(): int {
42
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
43
                return $this->do_num_rows();
44
        }
45
 
46
        /**
1116 daniel-mar 47
         * @return array|null
48
         */
1152 daniel-mar 49
        protected function do_fetch_array()/*: ?array*/ {
50
                assert(false);
51
                return null;
52
        }
1116 daniel-mar 53
 
54
        /**
1152 daniel-mar 55
         * @return array|null
56
         */
57
        public final function fetch_array()/*: ?array*/ {
58
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
59
 
60
                $reflector = new \ReflectionMethod($this, 'do_fetch_array');
61
                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
62
                if ($isImplemented) return $this->do_fetch_array();
63
 
64
                $reflector = new \ReflectionMethod($this, 'do_fetch_object');
65
                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
66
                if (!$isImplemented) {
67
                        throw new OIDplusException(_L("Class %1 is erroneous: At least one fetch-method needs to be overridden", get_class($this)));
68
                }
69
 
70
                // Convert object to array
71
                $obj = $this->do_fetch_object();
72
                if (!$obj) return null;
73
                $ary = array();
74
                foreach ($obj as $name => $val) {
75
                        $ary[$name] = $val;
76
                }
77
                return $ary;
78
        }
79
 
80
        /**
1116 daniel-mar 81
         * @return object|null
82
         */
1152 daniel-mar 83
        protected function do_fetch_object()/*: ?object*/ {
84
                assert(false);
85
                return null;
86
        }
790 daniel-mar 87
 
1116 daniel-mar 88
        /**
1152 daniel-mar 89
         * @return object|null
90
         */
91
        public final function fetch_object()/*: ?object*/ {
92
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
93
 
94
                $reflector = new \ReflectionMethod($this, 'do_fetch_object');
95
                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
96
                if ($isImplemented) return $this->do_fetch_object();
97
 
98
                $reflector = new \ReflectionMethod($this, 'do_fetch_array');
99
                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
100
                if (!$isImplemented) {
101
                        throw new OIDplusException(_L("Class %1 is erroneous: At least one fetch-method needs to be overridden", get_class($this)));
102
                }
103
 
104
                // Convert array of object
105
                $ary = $this->do_fetch_array();
106
                if (!$ary) return null;
107
                $obj = new \stdClass;
108
                foreach ($ary as $name => $val) {
109
                        $obj->$name = $val;
110
                }
111
                return $obj;
112
        }
113
 
114
        /**
1116 daniel-mar 115
         * The any() function returns true if there is at least one
116
         * row in the section. By default, num_rows() will be used.
117
         * Plugins can override this method if they have a possibility
118
         * of making this functionality more efficient.
119
         * @return bool
120
         */
790 daniel-mar 121
        public function any(): bool {
122
                return $this->num_rows() > 0;
123
        }
236 daniel-mar 124
}