PHP Classes

File: src/Filter/FloatArrayFilter.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Ionizer PHP Filter Input   src/Filter/FloatArrayFilter.php   Download  
File: src/Filter/FloatArrayFilter.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: 2 years ago
Size: 2,292 bytes
 

Contents

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

use
ParagonIE\Ionizer\InvalidDataException;
use
ParagonIE\Ionizer\Util;

/**
 * Class FloatArrayFilter
 * @package ParagonIE\Ionizer\Filter
 */
class FloatArrayFilter extends ArrayFilter
{
   
/**
     * @var float
     */
   
protected $default = 0.0;

   
/**
     * @var string
     */
   
protected $type = 'float[]';

   
/**
     * Apply all of the callbacks for this filter.
     *
     * @param mixed $data
     * @param int $offset
     * @return mixed
     * @throws \TypeError
     * @throws InvalidDataException
     */
   
public function applyCallbacks($data = null, int $offset = 0)
    {
        if (
$offset === 0) {
            if (\
is_null($data)) {
                return
parent::applyCallbacks($data, 0);
            } elseif (!\
is_array($data)) {
                throw new \
TypeError(
                    \
sprintf('Expected an array of floats (%s).', $this->index)
                );
            }

           
/** @var array<string, float> $data */
           
$data = (array) $data;
            if (!
Util::is1DArray($data)) {
                throw new \
TypeError(
                    \
sprintf('Expected a 1-dimensional array (%s).', $this->index)
                );
            }

           
/**
             * @var string|int|float|bool|array|null $val
             */
           
foreach ($data as $key => $val) {
                if (\
is_array($val)) {
                    throw new \
TypeError(
                        \
sprintf('Expected a 1-dimensional array (%s).', $this->index)
                    );
                }
                if (\
is_int($val) || \is_float($val)) {
                   
$data[$key] = (float) $val;
                } elseif (\
is_null($val) || $val === '') {
                   
$data[$key] = (float) $this->default;
                } elseif (\
is_string($val) && \is_numeric($val)) {
                   
$data[$key] = (float) $val;
                } else {
                    throw new \
TypeError(
                        \
sprintf('Expected a float at index %s (%s).', $key, $this->index)
                    );
                }
            }
            return
parent::applyCallbacks($data, 0);
        }
        return
parent::applyCallbacks($data, $offset);
    }
}