|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Pedigree; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits |
|
7
|
|
|
of supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @package XoopsModules\Pedigree |
|
17
|
|
|
* @author XOOPS Development Team - <https://xoops.org> |
|
18
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
|
19
|
|
|
* @license GPL 2.0 or later |
|
20
|
|
|
* @link https://xoops.org/ |
|
21
|
|
|
* @since 1.32 |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Trait to overload XoopsObject getCount() & getCounts() methods because XOOPS |
|
26
|
|
|
* doesn't return int as expected in XOOPS <= 2.5.10, need to check in later versions |
|
27
|
|
|
* |
|
28
|
|
|
* To use: add 'use CountOverload' in the class that extends \XoopsPersistableObjectHandler |
|
29
|
|
|
* |
|
30
|
|
|
**/ |
|
31
|
|
|
trait CountOverload |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* count objects matching a condition |
|
35
|
|
|
* |
|
36
|
|
|
* Overload of XoopsObject getCount() because XOOPS doesn't return int as expected |
|
37
|
|
|
* in XOOPS <= 2.5.10, need to check in later versions |
|
38
|
|
|
* |
|
39
|
|
|
* @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match |
|
40
|
|
|
* @return int count of objects |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getCount(?\CriteriaElement $criteria = null): int |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
return (int)parent::getCount($criteria = null); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get counts of objects matching a condition |
|
49
|
|
|
* |
|
50
|
|
|
* @param \CriteriaElement|null $criteria {@link CriteriaElement} to match |
|
51
|
|
|
* @return int[] of counts |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getCounts(?\CriteriaElement $criteria = null): array |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$countArray = parent::getCounts($criteria = null); |
|
56
|
|
|
\array_walk($countArray, 'self::castType', 'int'); |
|
57
|
|
|
|
|
58
|
|
|
return $countArray; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Cast a variable to a known type |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $var variable to cast (by reference) |
|
65
|
|
|
* @param string $type new type |
|
66
|
|
|
* @return bool true if type set was successful, false otherwise |
|
67
|
|
|
*/ |
|
68
|
|
|
private static function castType(&$var, $key, ?string $type = 'int'): bool |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$validTypes = ['bool', 'boolean', 'int', 'integer', 'float', 'double', 'string', 'array', 'object', 'null']; |
|
71
|
|
|
$success = false; |
|
72
|
|
|
if (\in_array($type, $validTypes)) { |
|
73
|
|
|
$success = \settype($var, $type); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
return $success; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.