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

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

 ',',
        self::FORMAT_SSV => ' ',
        self::FORMAT_TSV => "\t",
        self::FORMAT_PIPES => '|',
        self::FORMAT_MULTI => null,
    ];

    /**
     * @var string|null
     */
    protected $delimiter;

    public function __construct(array $options)
    {
        parent::__construct($options);
        if (empty($options['format'])) {
            throw new \InvalidArgumentException('Option "format" is required.');
        } elseif (!\array_key_exists($options['format'], self::DELIMITER_MAP)) {
            throw new \InvalidArgumentException(\sprintf('Unknown format "%s".', $options['format']));
        }
        $this->delimiter = self::DELIMITER_MAP[$options['format']];
    }

    /**
     * @inheritdoc
     */
    public function extract($objectValue, $object = null)
    {
        $result = null;
        if (\is_array($objectValue)) {
            if ($this->delimiter === null) {
                $result = $objectValue;
            } else {
                $result = \implode($this->delimiter, $objectValue);
            }
        }
        return $result;
    }

    /**
     * @inheritdoc
     */
    public function hydrate($arrayValue, $objectValue, array $array = null)
    {
        $result = null;
        if ($arrayValue !== null) {
            $list = null;
            if ($this->delimiter === null) {
                $list = (\is_array($arrayValue))? $arrayValue : [$arrayValue];
            } else {
                $list = \explode($this->delimiter, $arrayValue);
            }
            $result = [];
            foreach ($list as $item) {
                $result[] = parent::hydrate($item, null);
            }
        }
        return $result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy