Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
10 / 10
Boolean
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
10 / 10
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 filter
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
<?php
namespace Verja\Filter;
use Verja\Filter;
use Verja\Gate;
use Verja\Validator;
class Boolean extends Filter
{
    /** @var string[] */
    protected $stringTrue = ['1', 'true', 't', 'yes', 'y'];
    /** @var string[] */
    protected $stringFalse = ['0', 'false', 'f', 'no', 'n'];
    /**
     * Boolean constructor.
     *
     * @param string[] $stringTrue
     * @param string[] $stringFalse
     * @param bool     $overwrite   Overwrite the arrays instead of merging
     */
    public function __construct(array $stringTrue = [], array $stringFalse = [], $overwrite = false)
    {
        if ($overwrite) {
            $this->stringTrue = $stringTrue;
            $this->stringFalse = $stringFalse;
        } else {
            $this->stringTrue  = array_merge($this->stringTrue, $stringTrue);
            $this->stringFalse = array_merge($this->stringFalse, $stringFalse);
        }
    }
    /**
     * Filter $value
     *
     * @param mixed $value
     * @param array $context
     * @return boolean
     */
    public function filter($value, array $context = [])
    {
        Gate::assert(new Validator\Boolean($this->stringTrue, $this->stringFalse, true), $value);
        // the validator says it's either true or false string
        if (is_string($value)) {
            return in_array(strtolower($value), $this->stringTrue);
        }
        return (bool)$value;
    }
}