PHP Classes

File: src/Filter/BoolFilter.php

Recommend this page to a friend!
  Packages of Scott Arciszewski   Ionizer PHP Filter Input   src/Filter/BoolFilter.php   Download  
File: src/Filter/BoolFilter.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Ionizer PHP Filter Input
Filter input values by chaining filter objects
Author: By
Last change:
Date: 5 years ago
Size: 1,620 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace ParagonIE\Ionizer\Filter;

use ParagonIE\Ionizer\Contract\FilterInterface;
use ParagonIE\Ionizer\InputFilter;
use ParagonIE\Ionizer\InvalidDataException;

/**
 * Class BoolFilter
 * @package ParagonIE\Ionizer\Filter
 */
class BoolFilter extends InputFilter
{
    /**
     * @var mixed
     */
    protected $default = false;

    /**
     * @var string
     */
    protected $type = 'bool';

    /**
     * Process data using the filter rules.
     *
     * @param mixed $data
     * @return bool
     * @throws \TypeError
     * @throws InvalidDataException
     */
    public function process($data = null)
    {
        if (\is_array($data)) {
            throw new \TypeError(
                \sprintf('Unexpected array for boolean filter (%s).', $this->index)
            );
        }
        return (bool) parent::process(!empty($data));
    }

    /**
     * Sets the expected input type (e.g. string, boolean)
     *
     * @param string $typeIndicator
     * @return FilterInterface
     * @throws \TypeError
     */
    public function setType(string $typeIndicator): FilterInterface
    {
        if ($typeIndicator !== 'bool') {
            throw new \TypeError(
                'Type must always be set to "bool".'
            );
        }
        return parent::setType('bool');
    }

    /**
     * Set the default value (not applicable to booleans)
     *
     * @param string|int|float|bool|array|null $value
     * @return FilterInterface
     */
    public function setDefault($value): FilterInterface
    {
        return parent::setDefault($value);
    }
}