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

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

format;
    }

    /**
     * @param string $format
     * @return self
     */
    public function setFormat(string $format): self
    {
        $this->format = $format;
        return $this;
    }

    protected function checkType($value): bool
    {
        $result = true;
        if (!\array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) {
            throw new \InvalidArgumentException(\sprintf('Can not check for format %s.', $this->format));
        }
        $delimiter = QueryParameterArray::DELIMITER_MAP[$this->format];
        if ($delimiter === null) {
            if (\is_array($value)) {
                foreach ($value as $item) {
                    $result = $result && parent::checkType($item);
                }
            } else {
                $result = false;
            }
        } else {
            switch ($this->type) {
                case QueryParameterArray::TYPE_INT:
                    $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value);
                    break;
                case QueryParameterArray::TYPE_BOOL:
                    $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value);
                    break;
                case QueryParameterArray::TYPE_FLOAT:
                    $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value);
                    break;
                case QueryParameterArray::TYPE_STRING:
                    $result = \is_string($value);
                    break;
                default:
                    throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type));
            }
        }
        return $result;
    }

    protected static function prepareRepeatingTypeRegExp(string $typeRegExp, string $delimiter): string
    {
        $escapedDelimiter = \preg_quote($delimiter, '/');
        return '/^(' . $typeRegExp . ')(' . $escapedDelimiter . '('. $typeRegExp . '))*$/';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy