Airship\Engine\Security\Filter\InputFilter::process PHP Method

process() public method

Process data using the filter rules.
public process ( mixed $data = null ) : mixed
$data mixed
return mixed
    public function process($data = null)
    {
        if ($this->type === 'string') {
            if (\is_array($data)) {
                throw new \TypeError(\sprintf('Unexpected array for string filter (%s).', $this->index));
            }
            if (\is_string($data)) {
            } elseif (\is_object($data) && \method_exists($data, '__toString')) {
                $data = (string) $data;
            } elseif (\is_numeric($data)) {
                $data = (string) $data;
            } elseif (\is_null($data)) {
                $data = null;
            } else {
                throw new \TypeError(\sprintf('Expected a string (%s).', $this->index));
            }
        }
        if ($this->type === 'int') {
            if (\is_array($data)) {
                throw new \TypeError(\sprintf('Unexpected array for integer filter (%s).', $this->index));
            }
            if (\is_int($data) || \is_float($data)) {
                $data = (int) $data;
            } elseif (\is_null($data) || $data === '') {
                $data = null;
            } elseif (\is_string($data) && \preg_match('#^\\-?[0-9]+$#', $data)) {
                $data = (int) $data;
            } else {
                throw new \TypeError(\sprintf('Expected an integer (%s).', $this->index));
            }
        }
        if ($this->type === 'float') {
            if (\is_array($data)) {
                throw new \TypeError(\sprintf('Unexpected array for float filter (%s).', $this->index));
            }
            if (\is_int($data) || \is_float($data)) {
                $data = (double) $data;
            } elseif (\is_null($data) || $data === '') {
                $data = null;
            } elseif (\is_string($data) && \is_numeric($data)) {
                $data = (double) $data;
            } else {
                throw new \TypeError(\sprintf('Expected an integer or floating point number (%s).', $this->index));
            }
        }
        if ($this->type === 'array' || Util::subString($this->type, -2) === '[]') {
            if (\is_array($data)) {
                $data = (array) $data;
            } elseif (\is_null($data)) {
                $data = [];
            } else {
                throw new \TypeError(\sprintf('Expected an array (%s).', $this->index));
            }
        }
        if ($this->type === 'bool') {
            if (\is_array($data)) {
                throw new \TypeError(\sprintf('Unexpected array for boolean filter (%s).', $this->index));
            }
            $data = !empty($data);
        }
        $data = $this->applyCallbacks($data, 0);
        if ($data === null) {
            $data = $this->default;
        }
        // For type strictness:
        switch ($this->type) {
            case 'bool':
                return (bool) $data;
            case 'float':
                return (double) $data;
            case 'int':
                return (int) $data;
            case 'string':
                return (string) $data;
            default:
                return $data;
        }
    }