Skip to content

Commit be10138

Browse files
committed
add global function isTrue
1 parent c5dca41 commit be10138

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v2.9.0
8+
9+
### Added
10+
11+
- Add global function: `isTrue`
12+
713
## v2.8.0
814

915
### Added

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ composer require efureev/support "^2.6"
6363
- Global functions
6464
+ value
6565
+ classNamespace
66+
+ isTrue
6667
- Exceptions
6768
+ ConfigException
6869
+ Exception

src/Global/base.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ function classNamespace($class): string
3131
return implode('\\', array_slice(explode("\\", $class), 0, -1));
3232
}
3333
}
34+
35+
if (!function_exists('isTrue')) {
36+
/**
37+
* Returns bool value of a value
38+
*
39+
* @param mixed $val
40+
* @param bool $return_null
41+
*
42+
* @return bool|null
43+
*/
44+
function isTrue($val, bool $return_null = false): ?bool
45+
{
46+
$boolVal = (is_string($val)
47+
? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
48+
: (bool)$val);
49+
return ($boolVal === null && !$return_null ? false : $boolVal);
50+
}
51+
}

tests/Global/BaseTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,41 @@ public function testValue(): void
5858
}
5959
}
6060
}
61+
62+
public function testIsTrue(): void
63+
{
64+
foreach (
65+
[
66+
['val' => new \stdClass, 'res' => true,],
67+
['val' => [1, 2], 'res' => true,],
68+
['val' => [1], 'res' => true,],
69+
['val' => [0], 'res' => true,],
70+
['val' => 1, 'res' => true,],
71+
['val' => 42, 'res' => true,],
72+
['val' => -42, 'res' => true,],
73+
['val' => 'true', 'res' => true,],
74+
['val' => 'off', 'res' => false,],
75+
['val' => 'yes', 'res' => true,],
76+
['val' => 'no', 'res' => false,],
77+
['val' => 'ja', 'res' => false,],
78+
['val' => 'nein', 'res' => false,],
79+
['val' => 'нет', 'res' => false,],
80+
['val' => 'да', 'res' => false,],
81+
['val' => '1', 'res' => true,],
82+
['val' => null, 'res' => false,],
83+
['val' => 0, 'res' => false,],
84+
['val' => 'false', 'res' => false,],
85+
['val' => 'string', 'res' => false,],
86+
['val' => 'bool', 'res' => false,],
87+
['val' => '0.0', 'res' => false,],
88+
['val' => '4.2', 'res' => false,],
89+
['val' => '0', 'res' => false,],
90+
['val' => '', 'res' => false,],
91+
['val' => '[]', 'res' => false,],
92+
['val' => '{}', 'res' => false,],
93+
] as $data
94+
) {
95+
$this->assertEquals(isTrue($data['val']), $data['res']);
96+
}
97+
}
6198
}

0 commit comments

Comments
 (0)