ExplodeStrategy.php in Zircon Profile 8.0
File
vendor/zendframework/zend-hydrator/src/Strategy/ExplodeStrategy.php
View source
<?php
namespace Zend\Hydrator\Strategy;
class ExplodeStrategy implements StrategyInterface {
private $valueDelimiter;
private $explodeLimit;
public function __construct($delimiter = ',', $explodeLimit = null) {
$this
->setValueDelimiter($delimiter);
$this->explodeLimit = $explodeLimit === null ? null : (int) $explodeLimit;
}
private function setValueDelimiter($delimiter) {
if (!is_string($delimiter)) {
throw new Exception\InvalidArgumentException(sprintf('%s expects Delimiter to be string, %s provided instead', __METHOD__, is_object($delimiter) ? get_class($delimiter) : gettype($delimiter)));
}
if (empty($delimiter)) {
throw new Exception\InvalidArgumentException('Delimiter cannot be empty.');
}
$this->valueDelimiter = $delimiter;
}
public function hydrate($value) {
if (null === $value) {
return [];
}
if (!(is_string($value) || is_numeric($value))) {
throw new Exception\InvalidArgumentException(sprintf('%s expects argument 1 to be string, %s provided instead', __METHOD__, is_object($value) ? get_class($value) : gettype($value)));
}
if ($this->explodeLimit !== null) {
return explode($this->valueDelimiter, $value, $this->explodeLimit);
}
return explode($this->valueDelimiter, $value);
}
public function extract($value) {
if (!is_array($value)) {
throw new Exception\InvalidArgumentException(sprintf('%s expects argument 1 to be array, %s provided instead', __METHOD__, is_object($value) ? get_class($value) : gettype($value)));
}
return empty($value) ? null : implode($this->valueDelimiter, $value);
}
}