Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FilterVarValidateBool
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6declare(strict_types=1);
7
8namespace pvc\filtervar;
9
10/**
11 * Class FilterVarValidateBool
12 * see tests.  FilterVar will return true for case-insensitive strings of true, yes, on and the character 1.
13 * Returns false for case-insensitive strings of false, no, off and 0.  The FILTER_NULL_ON_FAILURE sets the behavior
14 * so that it returns null if the string argument is some other string.  So we need to override the validate method
15 * inherited from the parent class because this flavor of filter_var behaves like a parser, not like a validator.  In
16 * other words, we want to return true if the input is true or false, and return false if it is some other non-boolean
17 * kind of string such as 'foo'
18 */
19class FilterVarValidateBool extends FilterVarValidate
20{
21    public function __construct()
22    {
23        $this->setFilter(FILTER_VALIDATE_BOOL);
24        $this->addFlag(FILTER_NULL_ON_FAILURE);
25        $this->setLabel('boolean');
26    }
27
28    public function validate(mixed $value): bool
29    {
30        return !is_null(filter_var($value, $this->getFilter(), $this->getOptionsFlagsArray()));
31    }
32}