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:
<?php
namespace Coast\Transformer\Rule;
use Coast\Transformer\Rule;
class BooleanType extends Rule
{
protected $_true = [
'true',
'on',
'yes',
'1',
];
protected $_false = [
'false',
'off',
'no',
'0',
];
public function __construct(array $true = null, array $false = null)
{
if (isset($true)) {
$this->true($true);
}
if (isset($false)) {
$this->false($false);
}
}
public function true(array $true = null)
{
if (func_num_args() > 0) {
$this->_true = $true;
return $this;
}
return $this->_true;
}
public function false(array $false = null)
{
if (func_num_args() > 0) {
$this->_false = $false;
return $this;
}
return $this->_false;
}
protected function _transform($value)
{
$value = strtolower($value);
if (in_array($value, $this->_true, true)) {
$value = true;
} else if (in_array($value, $this->_false, true)) {
$value = false;
} else {
$value = (bool) $value;
}
return $value;
}
}