All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ze-ph.Type.php.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
 'Invalid type given.',
    ];

    /**
     * @var string
     */
    protected $type;

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param string $type
     * @return self
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function isValid($value)
    {
        $result = true;
        if (!$this->checkType($value)) {
            $this->error(self::INVALID);
            $result = false;
        }
        return $result;
    }

    protected function checkType($value)
    {
        switch ($this->type) {
            case 'int':
                return is_int($value);
            case 'bool':
                return is_bool($value);
            case 'float':
                return is_float($value) || is_int($value);
            case 'string':
                return is_string($value);
            default:
                throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy