Overview

Namespaces

  • Baguette
    • Application
    • Response
    • Serializer
    • Session
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 
<?php

namespace Baguette\Application;

/**
 * Param trait for Application (filter_var wrapper)
 *
 * @author    USAMI Kenta <tadsan@zonu.me>
 * @copyright 2016 Baguette HQ
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
 * @link      http://php.net/manual/function.filter-var.php
 */
trait Param
{
    private static $BODY_METHODS = ['POST', 'PUT'];

    /**
     * @param  string $name
     * @param  int    $filter
     * @param  array  $option
     * @return mixed
     */
    public function getParam($name, $filter = FILTER_DEFAULT, array $option = [])
    {
        $value = $this->getParamFilter($name, $filter, ['options' => $option], false);

        if ($value === null && isset($option['default'])) {
            return $option['default'];
        }

        return $value;
    }

    /**
     * @param  string $name
     * @param  int    $filter
     * @param  array  $option
     * @return array
     */
    public function getParamAsArray($name, $filter = FILTER_DEFAULT, array $option = [])
    {
        $value = $this->getParamFilter($name, $filter, ['options' => $option], true);

        if ($value === [] && isset($option['default'])) {
            return $option['default'];
        }

        return $value;
    }

    /**
     * @param  $name
     * @param  int   $filter
     * @param  mixed $filter_option
     * @param  bool  $get_array
     * @return mixed
     */
    public function getParamFilter($name, $filter, $filter_option = [], $get_array = null)
    {
        /** @var \Baguette\Application $this */

        $params = [$this->get];

        if (in_array($this->server['REQUEST_METHOD'], self::$BODY_METHODS, true)) {
            array_unshift($params, $this->post);
        }

        $value = $get_array ? [] : null;

        foreach ($params as $p) {
            if (!isset($p[$name])) { continue; }

            $v = $p[$name];
            $is_array = is_array($v);

            if (($get_array !== null) && ($is_array !== $get_array)) { continue; }

            $value = self::filterParam($v, $filter, $filter_option);
            break;
        }

        return $value;
    }

    /**
     * @param   mixed $value
     * @param   int   $filter
     * @param   mixed $filter_option
     * @return  mixed
     */
    protected static function filterParam($value, $filter = FILTER_DEFAULT, $filter_option = null)
    {
        $is_array = is_array($value);
        $values = $is_array ? $value : [$value];

        $result = [];
        foreach ($values as $k => $v) {
            $result[$k] = filter_var($v, $filter, $filter_option);
        }

        return $is_array ? $result : $result[0];
    }
}
ξ ^ω^)ξ < Baguette Not Framework API documentation generated by ApiGen