Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
12 / 12
Boolean
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
12 / 12
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validate
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
5 / 5
 getInverseError
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace Verja\Validator;
use Verja\Error;
use Verja\Validator;
class Boolean extends Validator
{
    /** @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);
        }
    }
    /**
     * Validate $value
     *
     * @param mixed $value
     * @param array $context
     * @return bool
     */
    public function validate($value, array $context = []): bool
    {
        if (!is_bool($value) && !is_int($value) &&
            (!is_string($value) || !in_array($value, $this->stringTrue) && !in_array($value, $this->stringFalse))
        ) {
            $this->error = new Error('NOT_BOOLEAN', $value, 'value should be a boolean');
            return false;
        }
        return true;
    }
    public function getInverseError($value)
    {
        return new Error('IS_BOOLEAN', $value, 'value should not be a boolean');
    }
}