-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zero.php
114 lines (98 loc) · 3.34 KB
/
Zero.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace DCarbone\Go\JSON;
/*
Copyright 2021-2023 Daniel Carbone (daniel.p.carbone@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
class Zero
{
public const STRING = '';
public const INTEGER = 0;
public const DOUBLE = 0.0;
public const FLOAT = self::DOUBLE;
public const BOOLEAN = false;
public const ARRAY = [];
public const OBJECT = null;
/** @var \DCarbone\Go\JSON\ZeroStates */
public static ZeroStates $zeroStates;
/**
* @param string $type
* @param mixed $value
* @return mixed
*/
public static function forType(string $type, mixed $value): mixed
{
switch ($type) {
case Type::STRING:
return Zero::STRING;
case Type::INTEGER:
return Zero::INTEGER;
case Type::DOUBLE:
return Zero::DOUBLE;
case Type::BOOLEAN:
return Zero::BOOLEAN;
case Type::ARRAY:
return Zero::ARRAY;
case Type::OBJECT:
if ($value instanceof ZeroVal) {
return $value->zeroVal();
}
$zs = self::$zeroStates->getZeroState($value);
if (null === $zs) {
return Zero::OBJECT;
}
return $zs->zeroVal();
case Type::RESOURCE:
return null;
default:
throw new \UnexpectedValueException(sprintf('Zero val for type "%s" is not defined', gettype($type)));
}
}
/**
* TODO: resources will currently always return non-zero.
*
* @param mixed $value
* @return bool
*/
public static function isZero(mixed $value): bool
{
// NULL and empty array are always zero'
if (null === $value || [] === $value) {
return true;
}
$type = \gettype($value);
if (Type::STRING === $type) {
return Zero::STRING === $value;
} elseif (Type::INTEGER === $type) {
return Zero::INTEGER === $value;
} elseif (Type::DOUBLE === $type) {
return Zero::DOUBLE === $value;
} elseif (Type::BOOLEAN === $type) {
return Zero::BOOLEAN === $value;
} elseif (Type::OBJECT === $type) {
if ($value instanceof ZeroVal) {
return $value->isZero();
} elseif (null !== ($zs = static::$zeroStates->getZeroState($value))) {
return $zs->isZero($value);
} elseif ($value instanceof \Countable) {
return 0 === \count($value);
} else {
return [] === \get_object_vars($value);
}
} else {
return false;
}
}
}
if (!isset(Zero::$zeroStates)) {
Zero::$zeroStates = new ZeroStates();
}