-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathBooleanUtils.php
65 lines (61 loc) · 1.65 KB
/
BooleanUtils.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Stdlib;
/**
* Utility methods for the boolean data type
*/
// @codingStandardsIgnoreFile
class BooleanUtils
{
/**
* Expressions that mean boolean TRUE
*
* @var array
*/
private $trueValues;
/**
* Expressions that mean boolean FALSE
*
* @var array
*/
private $falseValues;
/**
* BooleanUtils constructor.
* @param array $trueValues
* @param array $falseValues
*/
public function __construct(
array $trueValues = [true, 1, 'true', '1'],
array $falseValues = [false, 0, 'false', '0']
) {
$this->trueValues = $trueValues;
$this->falseValues = $falseValues;
}
/**
* Retrieve boolean value for an expression
*
* @param mixed $value Boolean expression
* @return bool
* @throws \InvalidArgumentException
*/
public function toBoolean($value)
{
/**
* Built-in function filter_var() is not used, because such values as on/off are irrelevant in some contexts
* @link http://www.php.net/manual/en/filter.filters.validate.php
*/
if (in_array($value, $this->trueValues, true)) {
return true;
}
if (in_array($value, $this->falseValues, true)) {
return false;
}
$allowedValues = array_merge($this->trueValues, $this->falseValues);
throw new \InvalidArgumentException(
'Boolean value is expected, supported values: ' . var_export($allowedValues, true)
);
}
}