File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ namespace JsonLogic \Support ;
3+
4+ use Closure ;
5+ use JsonLogic \Configurations as Config ;
6+
7+ use function gettype ;
8+ use function strlen ;
9+
10+ class Logical
11+ {
12+ private static $ handlers = [];
13+
14+ public static function truthy ($ logic ): bool
15+ {
16+ switch (gettype ($ logic )) {
17+ case 'string ' :
18+ return (static ::handle ('string ' ) ?: static ::createStringHandler ())($ logic );
19+ case 'array ' :
20+ return (static ::handle ('array ' ) ?: static ::createArrayHandler ())($ logic );
21+ default :
22+ return (bool ) $ logic ;
23+ }
24+ }
25+
26+ public static function falsy ($ logic ): bool
27+ {
28+ return !static ::truthy ($ logic );
29+ }
30+
31+ protected static function handle (string $ type ): ?Closure
32+ {
33+ return static ::$ handlers [$ type ] ?? null ;
34+ }
35+
36+ protected static function createStringHandler (): Closure
37+ {
38+ if (Config::get (Config::TRUTHY_STRING_0 )) {
39+ static ::$ handlers ['string ' ] = function (string &$ str ): bool {
40+ return strlen ($ str ) > 0 ;
41+ };
42+ } else {
43+ static ::$ handlers ['string ' ] = function (string &$ str ): bool {
44+ return (bool ) $ str ;
45+ };
46+ }
47+ return static ::handle ('string ' );
48+ }
49+
50+ protected static function createArrayHandler (): Closure
51+ {
52+ if (Config::get (Config::TRUTHY_EMPTY_ARRAY )) {
53+ static ::$ handlers ['array ' ] = function (array &$ arr ): bool {
54+ return true ;
55+ };
56+ } else {
57+ static ::$ handlers ['array ' ] = function (array &$ arr ): bool {
58+ return (bool ) $ arr ;
59+ };
60+ }
61+ return static ::handle ('array ' );
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments