CHANGELOG.md 0000666 00000001555 13605043111 0006361 0 ustar 00 # Changelog
All notable changes to this project will be documented in this file, in reverse chronological order by release.
## 2.5.2 - 2015-07-21
### Added
- Nothing.
### Deprecated
- Nothing.
### Removed
- Nothing.
### Fixed
- [zendframework/zend-stdlib#9](https://github.com/zendframework/zend-stdlib/pull/9) fixes an issue with
count incrementation during insert in PriorityList, ensuring that incrementation only
occurs when the item inserted was not previously present in the list.
## 2.4.4 - 2015-07-21
### Added
- Nothing.
### Deprecated
- Nothing.
### Removed
- Nothing.
### Fixed
- [zendframework/zend-stdlib#9](https://github.com/zendframework/zend-stdlib/pull/9) fixes an issue with
count incrementation during insert in PriorityList, ensuring that incrementation only
occurs when the item inserted was not previously present in the list.
COPYRIGHT.md 0000666 00000000127 13605043111 0006434 0 ustar 00 Copyright (c) 2019, Laminas Foundation.
All rights reserved. (https://getlaminas.org/)
LICENSE.md 0000666 00000002725 13605043111 0006154 0 ustar 00 Copyright (c) 2019, Laminas Foundation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Laminas Foundation nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.md 0000666 00000001246 13605043111 0006024 0 ustar 00 # laminas-stdlib
[![Build Status](https://travis-ci.org/laminas/laminas-stdlib.svg?branch=master)](https://travis-ci.org/laminas/laminas-stdlib)
[![Coverage Status](https://coveralls.io/repos/laminas/laminas-stdlib/badge.svg?branch=master)](https://coveralls.io/r/laminas/laminas-stdlib?branch=master)
`Laminas\Stdlib` is a set of components that implements general purpose utility
class for different scopes like:
- array utilities functions;
- hydrators;
- json serializable interfaces;
- general messaging systems;
- string wrappers;
- etc.
-
- File issues at https://github.com/laminas/laminas-stdlib/issues
- Documentation is at https://docs.laminas.dev/laminas-stdlib
composer.json 0000666 00000003500 13605043111 0007262 0 ustar 00 {
"name": "laminas/laminas-stdlib",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"laminas",
"stdlib"
],
"homepage": "https://laminas.dev",
"support": {
"docs": "https://docs.laminas.dev/laminas-stdlib/",
"issues": "https://github.com/laminas/laminas-stdlib/issues",
"source": "https://github.com/laminas/laminas-stdlib",
"rss": "https://github.com/laminas/laminas-stdlib/releases.atom",
"chat": "https://laminas.dev/chat",
"forum": "https://discourse.laminas.dev"
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "2.5-dev",
"dev-develop": "2.6-dev"
}
},
"require": {
"php": ">=5.5",
"laminas/laminas-zendframework-bridge": "^1.0"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"laminas/laminas-config": "~2.5",
"laminas/laminas-eventmanager": "~2.5",
"laminas/laminas-filter": "~2.5",
"laminas/laminas-inputfilter": "~2.5",
"laminas/laminas-serializer": "~2.5",
"laminas/laminas-servicemanager": "~2.5",
"phpunit/phpunit": "~4.0"
},
"suggest": {
"laminas/laminas-eventmanager": "To support aggregate hydrator usage",
"laminas/laminas-filter": "To support naming strategy hydrator usage",
"laminas/laminas-serializer": "Laminas\\Serializer component",
"laminas/laminas-servicemanager": "To support hydrator plugin manager usage"
},
"autoload": {
"psr-4": {
"Laminas\\Stdlib\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"LaminasTest\\Stdlib\\": "test/"
}
},
"replace": {
"zendframework/zend-stdlib": "self.version"
}
}
src/AbstractOptions.php 0000666 00000011120 13605043111 0011154 0 ustar 00 setFromArray($options);
}
}
/**
* Set one or more configuration properties
*
* @param array|Traversable|AbstractOptions $options
* @throws Exception\InvalidArgumentException
* @return AbstractOptions Provides fluent interface
*/
public function setFromArray($options)
{
if ($options instanceof self) {
$options = $options->toArray();
}
if (!is_array($options) && !$options instanceof Traversable) {
throw new Exception\InvalidArgumentException(
sprintf(
'Parameter provided to %s must be an %s, %s or %s',
__METHOD__,
'array',
'Traversable',
'Laminas\Stdlib\AbstractOptions'
)
);
}
foreach ($options as $key => $value) {
$this->__set($key, $value);
}
return $this;
}
/**
* Cast to array
*
* @return array
*/
public function toArray()
{
$array = [];
$transform = function ($letters) {
$letter = array_shift($letters);
return '_' . strtolower($letter);
};
foreach ($this as $key => $value) {
if ($key === '__strictMode__') {
continue;
}
$normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
$array[$normalizedKey] = $value;
}
return $array;
}
/**
* Set a configuration property
*
* @see ParameterObject::__set()
* @param string $key
* @param mixed $value
* @throws Exception\BadMethodCallException
* @return void
*/
public function __set($key, $value)
{
$setter = 'set' . str_replace('_', '', $key);
if (is_callable([$this, $setter])) {
$this->{$setter}($value);
return;
}
if ($this->__strictMode__) {
throw new Exception\BadMethodCallException(sprintf(
'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined',
$key,
'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))),
$setter
));
}
}
/**
* Get a configuration property
*
* @see ParameterObject::__get()
* @param string $key
* @throws Exception\BadMethodCallException
* @return mixed
*/
public function __get($key)
{
$getter = 'get' . str_replace('_', '', $key);
if (is_callable([$this, $getter])) {
return $this->{$getter}();
}
throw new Exception\BadMethodCallException(sprintf(
'The option "%s" does not have a callable "%s" getter method which must be defined',
$key,
'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))
));
}
/**
* Test if a configuration property is null
* @see ParameterObject::__isset()
* @param string $key
* @return bool
*/
public function __isset($key)
{
$getter = 'get' . str_replace('_', '', $key);
return method_exists($this, $getter) && null !== $this->__get($key);
}
/**
* Set a configuration property to NULL
*
* @see ParameterObject::__unset()
* @param string $key
* @throws Exception\InvalidArgumentException
* @return void
*/
public function __unset($key)
{
try {
$this->__set($key, null);
} catch (Exception\BadMethodCallException $e) {
throw new Exception\InvalidArgumentException(
'The class property $' . $key . ' cannot be unset as'
. ' NULL is an invalid value for it',
0,
$e
);
}
}
}
src/ArrayObject.php 0000666 00000022611 13605043111 0010251 0 ustar 00 setFlags($flags);
$this->storage = $input;
$this->setIteratorClass($iteratorClass);
$this->protectedProperties = array_keys(get_object_vars($this));
}
/**
* Returns whether the requested key exists
*
* @param mixed $key
* @return bool
*/
public function __isset($key)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
return $this->offsetExists($key);
}
if (in_array($key, $this->protectedProperties)) {
throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
}
return isset($this->$key);
}
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
return $this->offsetSet($key, $value);
}
if (in_array($key, $this->protectedProperties)) {
throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
}
$this->$key = $value;
}
/**
* Unsets the value at the specified key
*
* @param mixed $key
* @return void
*/
public function __unset($key)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
return $this->offsetUnset($key);
}
if (in_array($key, $this->protectedProperties)) {
throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
}
unset($this->$key);
}
/**
* Returns the value at the specified key by reference
*
* @param mixed $key
* @return mixed
*/
public function &__get($key)
{
$ret = null;
if ($this->flag == self::ARRAY_AS_PROPS) {
$ret =& $this->offsetGet($key);
return $ret;
}
if (in_array($key, $this->protectedProperties)) {
throw new Exception\InvalidArgumentException('$key is a protected property, use a different key');
}
return $this->$key;
}
/**
* Appends the value
*
* @param mixed $value
* @return void
*/
public function append($value)
{
$this->storage[] = $value;
}
/**
* Sort the entries by value
*
* @return void
*/
public function asort()
{
asort($this->storage);
}
/**
* Get the number of public properties in the ArrayObject
*
* @return int
*/
public function count()
{
return count($this->storage);
}
/**
* Exchange the array for another one.
*
* @param array|ArrayObject $data
* @return array
*/
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
}
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
/**
* Creates a copy of the ArrayObject.
*
* @return array
*/
public function getArrayCopy()
{
return $this->storage;
}
/**
* Gets the behavior flags.
*
* @return int
*/
public function getFlags()
{
return $this->flag;
}
/**
* Create a new iterator from an ArrayObject instance
*
* @return \Iterator
*/
public function getIterator()
{
$class = $this->iteratorClass;
return new $class($this->storage);
}
/**
* Gets the iterator classname for the ArrayObject.
*
* @return string
*/
public function getIteratorClass()
{
return $this->iteratorClass;
}
/**
* Sort the entries by key
*
* @return void
*/
public function ksort()
{
ksort($this->storage);
}
/**
* Sort an array using a case insensitive "natural order" algorithm
*
* @return void
*/
public function natcasesort()
{
natcasesort($this->storage);
}
/**
* Sort entries using a "natural order" algorithm
*
* @return void
*/
public function natsort()
{
natsort($this->storage);
}
/**
* Returns whether the requested key exists
*
* @param mixed $key
* @return bool
*/
public function offsetExists($key)
{
return isset($this->storage[$key]);
}
/**
* Returns the value at the specified key
*
* @param mixed $key
* @return mixed
*/
public function &offsetGet($key)
{
$ret = null;
if (!$this->offsetExists($key)) {
return $ret;
}
$ret =& $this->storage[$key];
return $ret;
}
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
$this->storage[$key] = $value;
}
/**
* Unsets the value at the specified key
*
* @param mixed $key
* @return void
*/
public function offsetUnset($key)
{
if ($this->offsetExists($key)) {
unset($this->storage[$key]);
}
}
/**
* Serialize an ArrayObject
*
* @return string
*/
public function serialize()
{
return serialize(get_object_vars($this));
}
/**
* Sets the behavior flags
*
* @param int $flags
* @return void
*/
public function setFlags($flags)
{
$this->flag = $flags;
}
/**
* Sets the iterator classname for the ArrayObject
*
* @param string $class
* @return void
*/
public function setIteratorClass($class)
{
if (class_exists($class)) {
$this->iteratorClass = $class;
return ;
}
if (strpos($class, '\\') === 0) {
$class = '\\' . $class;
if (class_exists($class)) {
$this->iteratorClass = $class;
return ;
}
}
throw new Exception\InvalidArgumentException('The iterator class does not exist');
}
/**
* Sort the entries with a user-defined comparison function and maintain key association
*
* @param callable $function
* @return void
*/
public function uasort($function)
{
if (is_callable($function)) {
uasort($this->storage, $function);
}
}
/**
* Sort the entries by keys using a user-defined comparison function
*
* @param callable $function
* @return void
*/
public function uksort($function)
{
if (is_callable($function)) {
uksort($this->storage, $function);
}
}
/**
* Unserialize an ArrayObject
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
$ar = unserialize($data);
$this->protectedProperties = array_keys(get_object_vars($this));
$this->setFlags($ar['flag']);
$this->exchangeArray($ar['storage']);
$this->setIteratorClass($ar['iteratorClass']);
foreach ($ar as $k => $v) {
switch ($k) {
case 'flag':
$this->setFlags($v);
break;
case 'storage':
$this->exchangeArray($v);
break;
case 'iteratorClass':
$this->setIteratorClass($v);
break;
case 'protectedProperties':
continue;
default:
$this->__set($k, $v);
}
}
}
}
src/ArraySerializableInterface.php 0000666 00000001221 13605043111 0013264 0 ustar 00 getArrayCopy();
return new ArrayIterator(array_reverse($array));
}
}
src/ArrayUtils.php 0000666 00000022427 13605043111 0010150 0 ustar 00 0;
}
/**
* Test whether an array contains one or more integer keys
*
* @param mixed $value
* @param bool $allowEmpty Should an empty array() return true
* @return bool
*/
public static function hasIntegerKeys($value, $allowEmpty = false)
{
if (!is_array($value)) {
return false;
}
if (!$value) {
return $allowEmpty;
}
return count(array_filter(array_keys($value), 'is_int')) > 0;
}
/**
* Test whether an array contains one or more numeric keys.
*
* A numeric key can be one of the following:
* - an integer 1,
* - a string with a number '20'
* - a string with negative number: '-1000'
* - a float: 2.2120, -78.150999
* - a string with float: '4000.99999', '-10.10'
*
* @param mixed $value
* @param bool $allowEmpty Should an empty array() return true
* @return bool
*/
public static function hasNumericKeys($value, $allowEmpty = false)
{
if (!is_array($value)) {
return false;
}
if (!$value) {
return $allowEmpty;
}
return count(array_filter(array_keys($value), 'is_numeric')) > 0;
}
/**
* Test whether an array is a list
*
* A list is a collection of values assigned to continuous integer keys
* starting at 0 and ending at count() - 1.
*
* For example:
*
* $list = array('a', 'b', 'c', 'd');
* $list = array(
* 0 => 'foo',
* 1 => 'bar',
* 2 => array('foo' => 'baz'),
* );
*
*
* @param mixed $value
* @param bool $allowEmpty Is an empty list a valid list?
* @return bool
*/
public static function isList($value, $allowEmpty = false)
{
if (!is_array($value)) {
return false;
}
if (!$value) {
return $allowEmpty;
}
return (array_values($value) === $value);
}
/**
* Test whether an array is a hash table.
*
* An array is a hash table if:
*
* 1. Contains one or more non-integer keys, or
* 2. Integer keys are non-continuous or misaligned (not starting with 0)
*
* For example:
*
* $hash = array(
* 'foo' => 15,
* 'bar' => false,
* );
* $hash = array(
* 1995 => 'Birth of PHP',
* 2009 => 'PHP 5.3.0',
* 2012 => 'PHP 5.4.0',
* );
* $hash = array(
* 'formElement,
* 'options' => array( 'debug' => true ),
* );
*
*
* @param mixed $value
* @param bool $allowEmpty Is an empty array() a valid hash table?
* @return bool
*/
public static function isHashTable($value, $allowEmpty = false)
{
if (!is_array($value)) {
return false;
}
if (!$value) {
return $allowEmpty;
}
return (array_values($value) !== $value);
}
/**
* Checks if a value exists in an array.
*
* Due to "foo" == 0 === TRUE with in_array when strict = false, an option
* has been added to prevent this. When $strict = 0/false, the most secure
* non-strict check is implemented. if $strict = -1, the default in_array
* non-strict behaviour is used.
*
* @param mixed $needle
* @param array $haystack
* @param int|bool $strict
* @return bool
*/
public static function inArray($needle, array $haystack, $strict = false)
{
if (!$strict) {
if (is_int($needle) || is_float($needle)) {
$needle = (string) $needle;
}
if (is_string($needle)) {
foreach ($haystack as &$h) {
if (is_int($h) || is_float($h)) {
$h = (string) $h;
}
}
}
}
return in_array($needle, $haystack, $strict);
}
/**
* Convert an iterator to an array.
*
* Converts an iterator to an array. The $recursive flag, on by default,
* hints whether or not you want to do so recursively.
*
* @param array|Traversable $iterator The array or Traversable object to convert
* @param bool $recursive Recursively check all nested structures
* @throws Exception\InvalidArgumentException if $iterator is not an array or a Traversable object
* @return array
*/
public static function iteratorToArray($iterator, $recursive = true)
{
if (!is_array($iterator) && !$iterator instanceof Traversable) {
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
}
if (!$recursive) {
if (is_array($iterator)) {
return $iterator;
}
return iterator_to_array($iterator);
}
if (method_exists($iterator, 'toArray')) {
return $iterator->toArray();
}
$array = [];
foreach ($iterator as $key => $value) {
if (is_scalar($value)) {
$array[$key] = $value;
continue;
}
if ($value instanceof Traversable) {
$array[$key] = static::iteratorToArray($value, $recursive);
continue;
}
if (is_array($value)) {
$array[$key] = static::iteratorToArray($value, $recursive);
continue;
}
$array[$key] = $value;
}
return $array;
}
/**
* Merge two arrays together.
*
* If an integer key exists in both arrays and preserveNumericKeys is false, the value
* from the second array will be appended to the first array. If both values are arrays, they
* are merged together, else the value of the second array overwrites the one of the first array.
*
* @param array $a
* @param array $b
* @param bool $preserveNumericKeys
* @return array
*/
public static function merge(array $a, array $b, $preserveNumericKeys = false)
{
foreach ($b as $key => $value) {
if ($value instanceof MergeReplaceKeyInterface) {
$a[$key] = $value->getData();
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
if ($value instanceof MergeRemoveKey) {
unset($a[$key]);
} elseif (!$preserveNumericKeys && is_int($key)) {
$a[] = $value;
} elseif (is_array($value) && is_array($a[$key])) {
$a[$key] = static::merge($a[$key], $value, $preserveNumericKeys);
} else {
$a[$key] = $value;
}
} else {
if (!$value instanceof MergeRemoveKey) {
$a[$key] = $value;
}
}
}
return $a;
}
/**
* Compatibility Method for array_filter on <5.6 systems
*
* @param array $data
* @param callable $callback
* @param null|int $flag
* @return array
*/
public static function filter(array $data, $callback, $flag = null)
{
if (! is_callable($callback)) {
throw new Exception\InvalidArgumentException(sprintf(
'Second parameter of %s must be callable',
__METHOD__
));
}
if (version_compare(PHP_VERSION, '5.6.0') >= 0) {
return array_filter($data, $callback, $flag);
}
$output = [];
foreach ($data as $key => $value) {
$params = [$value];
if ($flag === static::ARRAY_FILTER_USE_BOTH) {
$params[] = $key;
}
if ($flag === static::ARRAY_FILTER_USE_KEY) {
$params = [$key];
}
$response = call_user_func_array($callback, $params);
if ($response) {
$output[$key] = $value;
}
}
return $output;
}
}
src/ArrayUtils/MergeRemoveKey.php 0000666 00000000541 13605043111 0013027 0 ustar 00 data = $data;
}
/**
* {@inheritDoc}
*/
public function getData()
{
return $this->data;
}
}
src/ArrayUtils/MergeReplaceKeyInterface.php 0000666 00000001032 13605043111 0014762 0 ustar 00 metadata = $metadata;
$this->registerCallback($callback);
}
/**
* Registers the callback provided in the constructor
*
* @param callable $callback
* @throws Exception\InvalidCallbackException
* @return void
*/
protected function registerCallback($callback)
{
if (!is_callable($callback)) {
throw new Exception\InvalidCallbackException('Invalid callback provided; not callable');
}
$this->callback = $callback;
}
/**
* Retrieve registered callback
*
* @return callable
*/
public function getCallback()
{
return $this->callback;
}
/**
* Invoke handler
*
* @param array $args Arguments to pass to callback
* @return mixed
*/
public function call(array $args = [])
{
$callback = $this->getCallback();
$argCount = count($args);
if (is_string($callback)) {
$result = $this->validateStringCallbackFor54($callback);
if ($result !== true && $argCount <= 3) {
$callback = $result;
// Minor performance tweak, if the callback gets called more
// than once
$this->callback = $result;
}
}
// Minor performance tweak; use call_user_func() until > 3 arguments
// reached
switch ($argCount) {
case 0:
return $callback();
case 1:
return $callback(array_shift($args));
case 2:
$arg1 = array_shift($args);
$arg2 = array_shift($args);
return $callback($arg1, $arg2);
case 3:
$arg1 = array_shift($args);
$arg2 = array_shift($args);
$arg3 = array_shift($args);
return $callback($arg1, $arg2, $arg3);
default:
return call_user_func_array($callback, $args);
}
}
/**
* Invoke as functor
*
* @return mixed
*/
public function __invoke()
{
return $this->call(func_get_args());
}
/**
* Get all callback metadata
*
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* Retrieve a single metadatum
*
* @param string $name
* @return mixed
*/
public function getMetadatum($name)
{
if (array_key_exists($name, $this->metadata)) {
return $this->metadata[$name];
}
return;
}
/**
* Validate a static method call
*
*
* @param string $callback
* @return true|array
* @throws Exception\InvalidCallbackException if invalid
*/
protected function validateStringCallbackFor54($callback)
{
if (!strstr($callback, '::')) {
return true;
}
list($class, $method) = explode('::', $callback, 2);
if (!class_exists($class)) {
throw new Exception\InvalidCallbackException(sprintf(
'Static method call "%s" refers to a class that does not exist',
$callback
));
}
$r = new ReflectionClass($class);
if (!$r->hasMethod($method)) {
throw new Exception\InvalidCallbackException(sprintf(
'Static method call "%s" refers to a method that does not exist',
$callback
));
}
$m = $r->getMethod($method);
if (!$m->isStatic()) {
throw new Exception\InvalidCallbackException(sprintf(
'Static method call "%s" refers to a method that is not static',
$callback
));
}
// returning a non boolean value may not be nice for a validate method,
// but that allows the usage of a static string callback without using
// the call_user_func function.
return [$class, $method];
}
}
src/DateTime.php 0000666 00000002447 13605043111 0007545 0 ustar 00 GLOB_MARK,
self::GLOB_NOSORT => GLOB_NOSORT,
self::GLOB_NOCHECK => GLOB_NOCHECK,
self::GLOB_NOESCAPE => GLOB_NOESCAPE,
self::GLOB_BRACE => GLOB_BRACE,
self::GLOB_ONLYDIR => GLOB_ONLYDIR,
self::GLOB_ERR => GLOB_ERR,
];
$globFlags = 0;
foreach ($flagMap as $internalFlag => $globFlag) {
if ($flags & $internalFlag) {
$globFlags |= $globFlag;
}
}
} else {
$globFlags = 0;
}
ErrorHandler::start();
$res = glob($pattern, $globFlags);
$err = ErrorHandler::stop();
if ($res === false) {
throw new Exception\RuntimeException("glob('{$pattern}', {$globFlags}) failed", 0, $err);
}
return $res;
}
/**
* Expand braces manually, then use the system glob.
*
* @param string $pattern
* @param int $flags
* @return array
* @throws Exception\RuntimeException
*/
protected static function fallbackGlob($pattern, $flags)
{
if (!$flags & self::GLOB_BRACE) {
return static::systemGlob($pattern, $flags);
}
$flags &= ~self::GLOB_BRACE;
$length = strlen($pattern);
$paths = [];
if ($flags & self::GLOB_NOESCAPE) {
$begin = strpos($pattern, '{');
} else {
$begin = 0;
while (true) {
if ($begin === $length) {
$begin = false;
break;
} elseif ($pattern[$begin] === '\\' && ($begin + 1) < $length) {
$begin++;
} elseif ($pattern[$begin] === '{') {
break;
}
$begin++;
}
}
if ($begin === false) {
return static::systemGlob($pattern, $flags);
}
$next = static::nextBraceSub($pattern, $begin + 1, $flags);
if ($next === null) {
return static::systemGlob($pattern, $flags);
}
$rest = $next;
while ($pattern[$rest] !== '}') {
$rest = static::nextBraceSub($pattern, $rest + 1, $flags);
if ($rest === null) {
return static::systemGlob($pattern, $flags);
}
}
$p = $begin + 1;
while (true) {
$subPattern = substr($pattern, 0, $begin)
. substr($pattern, $p, $next - $p)
. substr($pattern, $rest + 1);
$result = static::fallbackGlob($subPattern, $flags | self::GLOB_BRACE);
if ($result) {
$paths = array_merge($paths, $result);
}
if ($pattern[$next] === '}') {
break;
}
$p = $next + 1;
$next = static::nextBraceSub($pattern, $p, $flags);
}
return array_unique($paths);
}
/**
* Find the end of the sub-pattern in a brace expression.
*
* @param string $pattern
* @param int $begin
* @param int $flags
* @return int|null
*/
protected static function nextBraceSub($pattern, $begin, $flags)
{
$length = strlen($pattern);
$depth = 0;
$current = $begin;
while ($current < $length) {
if (!$flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') {
if (++$current === $length) {
break;
}
$current++;
} else {
if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) {
break;
} elseif ($pattern[$current++] === '{') {
$depth++;
}
}
}
return ($current < $length ? $current : null);
}
}
src/Guard/AllGuardsTrait.php 0000666 00000000732 13605043111 0011770 0 ustar 00 strategies = new ArrayObject();
$this->filterComposite = new FilterComposite();
}
/**
* Gets the strategy with the given name.
*
* @param string $name The name of the strategy to get.
*
* @throws \Laminas\Stdlib\Exception\InvalidArgumentException
* @return StrategyInterface
*/
public function getStrategy($name)
{
if (isset($this->strategies[$name])) {
return $this->strategies[$name];
}
if (!isset($this->strategies['*'])) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no strategy by name of "%s", and no wildcard strategy present',
__METHOD__,
$name
));
}
return $this->strategies['*'];
}
/**
* Checks if the strategy with the given name exists.
*
* @param string $name The name of the strategy to check for.
* @return bool
*/
public function hasStrategy($name)
{
return array_key_exists($name, $this->strategies)
|| array_key_exists('*', $this->strategies);
}
/**
* Adds the given strategy under the given name.
*
* @param string $name The name of the strategy to register.
* @param StrategyInterface $strategy The strategy to register.
* @return HydratorInterface
*/
public function addStrategy($name, StrategyInterface $strategy)
{
$this->strategies[$name] = $strategy;
return $this;
}
/**
* Removes the strategy with the given name.
*
* @param string $name The name of the strategy to remove.
* @return HydratorInterface
*/
public function removeStrategy($name)
{
unset($this->strategies[$name]);
return $this;
}
/**
* Converts a value for extraction. If no strategy exists the plain value is returned.
*
* @param string $name The name of the strategy to use.
* @param mixed $value The value that should be converted.
* @param mixed $object The object is optionally provided as context.
* @return mixed
*/
public function extractValue($name, $value, $object = null)
{
if ($this->hasStrategy($name)) {
$strategy = $this->getStrategy($name);
$value = $strategy->extract($value, $object);
}
return $value;
}
/**
* Converts a value for hydration. If no strategy exists the plain value is returned.
*
* @param string $name The name of the strategy to use.
* @param mixed $value The value that should be converted.
* @param array $data The whole data is optionally provided as context.
* @return mixed
*/
public function hydrateValue($name, $value, $data = null)
{
if ($this->hasStrategy($name)) {
$strategy = $this->getStrategy($name);
$value = $strategy->hydrate($value, $data);
}
return $value;
}
/**
* Convert a name for extraction. If no naming strategy exists, the plain value is returned.
*
* @param string $name The name to convert.
* @param null $object The object is optionally provided as context.
* @return mixed
*/
public function extractName($name, $object = null)
{
if ($this->hasNamingStrategy()) {
$name = $this->getNamingStrategy()->extract($name, $object);
}
return $name;
}
/**
* Converts a value for hydration. If no naming strategy exists, the plain value is returned.
*
* @param string $name The name to convert.
* @param array $data The whole data is optionally provided as context.
* @return mixed
*/
public function hydrateName($name, $data = null)
{
if ($this->hasNamingStrategy()) {
$name = $this->getNamingStrategy()->hydrate($name, $data);
}
return $name;
}
/**
* Get the filter instance
*
* @return Filter\FilterComposite
*/
public function getFilter()
{
return $this->filterComposite;
}
/**
* Add a new filter to take care of what needs to be hydrated.
* To exclude e.g. the method getServiceLocator:
*
*
* $composite->addFilter("servicelocator",
* function ($property) {
* list($class, $method) = explode('::', $property);
* if ($method === 'getServiceLocator') {
* return false;
* }
* return true;
* }, FilterComposite::CONDITION_AND
* );
*
*
* @param string $name Index in the composite
* @param callable|Filter\FilterInterface $filter
* @param int $condition
* @return Filter\FilterComposite
*/
public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
{
return $this->filterComposite->addFilter($name, $filter, $condition);
}
/**
* Check whether a specific filter exists at key $name or not
*
* @param string $name Index in the composite
* @return bool
*/
public function hasFilter($name)
{
return $this->filterComposite->hasFilter($name);
}
/**
* Remove a filter from the composition.
* To not extract "has" methods, you simply need to unregister it
*
*
* $filterComposite->removeFilter('has');
*
*
* @param $name
* @return Filter\FilterComposite
*/
public function removeFilter($name)
{
return $this->filterComposite->removeFilter($name);
}
/**
* Adds the given naming strategy
*
* @param NamingStrategyInterface $strategy The naming to register.
* @return self
*/
public function setNamingStrategy(NamingStrategyInterface $strategy)
{
$this->namingStrategy = $strategy;
return $this;
}
/**
* Gets the naming strategy.
*
* @return NamingStrategyInterface
*/
public function getNamingStrategy()
{
return $this->namingStrategy;
}
/**
* Checks if a naming strategy exists.
*
* @return bool
*/
public function hasNamingStrategy()
{
return isset($this->namingStrategy);
}
/**
* Removes the naming strategy
*
* @return self
*/
public function removeNamingStrategy()
{
$this->namingStrategy = null;
return $this;
}
}
src/Hydrator/Aggregate/AggregateHydrator.php 0000666 00000004274 13605043111 0015136 0 ustar 00 getEventManager()->attachAggregate(new HydratorListener($hydrator), $priority);
}
/**
* {@inheritDoc}
*/
public function extract($object)
{
$event = new ExtractEvent($this, $object);
$this->getEventManager()->trigger($event);
return $event->getExtractedData();
}
/**
* {@inheritDoc}
*/
public function hydrate(array $data, $object)
{
$event = new HydrateEvent($this, $object, $data);
$this->getEventManager()->trigger($event);
return $event->getHydratedObject();
}
/**
* {@inheritDoc}
*/
public function setEventManager(EventManagerInterface $eventManager)
{
$eventManager->setIdentifiers([__CLASS__, get_class($this)]);
$this->eventManager = $eventManager;
}
/**
* {@inheritDoc}
*/
public function getEventManager()
{
if (null === $this->eventManager) {
$this->setEventManager(new EventManager());
}
return $this->eventManager;
}
}
src/Hydrator/Aggregate/ExtractEvent.php 0000666 00000004105 13605043111 0014140 0 ustar 00 target = $target;
$this->extractionObject = $extractionObject;
}
/**
* Retrieves the object from which data is extracted
*
* @return object
*/
public function getExtractionObject()
{
return $this->extractionObject;
}
/**
* @param object $extractionObject
*
* @return void
*/
public function setExtractionObject($extractionObject)
{
$this->extractionObject = $extractionObject;
}
/**
* Retrieves the data that has been extracted
*
* @return array
*/
public function getExtractedData()
{
return $this->extractedData;
}
/**
* @param array $extractedData
*
* @return void
*/
public function setExtractedData(array $extractedData)
{
$this->extractedData = $extractedData;
}
/**
* Merge provided data with the extracted data
*
* @param array $additionalData
*
* @return void
*/
public function mergeExtractedData(array $additionalData)
{
$this->extractedData = array_merge($this->extractedData, $additionalData);
}
}
src/Hydrator/Aggregate/HydrateEvent.php 0000666 00000003466 13605043111 0014137 0 ustar 00 target = $target;
$this->hydratedObject = $hydratedObject;
$this->hydrationData = $hydrationData;
}
/**
* Retrieves the object that is being hydrated
*
* @return object
*/
public function getHydratedObject()
{
return $this->hydratedObject;
}
/**
* @param object $hydratedObject
*/
public function setHydratedObject($hydratedObject)
{
$this->hydratedObject = $hydratedObject;
}
/**
* Retrieves the data that is being used for hydration
*
* @return array
*/
public function getHydrationData()
{
return $this->hydrationData;
}
/**
* @param array $hydrationData
*/
public function setHydrationData(array $hydrationData)
{
$this->hydrationData = $hydrationData;
}
}
src/Hydrator/Aggregate/HydratorListener.php 0000666 00000004417 13605043111 0015034 0 ustar 00 hydrator = $hydrator;
}
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(HydrateEvent::EVENT_HYDRATE, [$this, 'onHydrate'], $priority);
$this->listeners[] = $events->attach(ExtractEvent::EVENT_EXTRACT, [$this, 'onExtract'], $priority);
}
/**
* Callback to be used when {@see \Laminas\Stdlib\Hydrator\Aggregate\HydrateEvent::EVENT_HYDRATE} is triggered
*
* @param \Laminas\Stdlib\Hydrator\Aggregate\HydrateEvent $event
*
* @return object
*
* @internal
*/
public function onHydrate(HydrateEvent $event)
{
$object = $this->hydrator->hydrate($event->getHydrationData(), $event->getHydratedObject());
$event->setHydratedObject($object);
return $object;
}
/**
* Callback to be used when {@see \Laminas\Stdlib\Hydrator\Aggregate\ExtractEvent::EVENT_EXTRACT} is triggered
*
* @param \Laminas\Stdlib\Hydrator\Aggregate\ExtractEvent $event
*
* @return array
*
* @internal
*/
public function onExtract(ExtractEvent $event)
{
$data = $this->hydrator->extract($event->getExtractionObject());
$event->mergeExtractedData($data);
return $data;
}
}
src/Hydrator/ArraySerializable.php 0000666 00000005134 13605043111 0013246 0 ustar 00 getArrayCopy();
$filter = $this->getFilter();
foreach ($data as $name => $value) {
if (!$filter->filter($name)) {
unset($data[$name]);
continue;
}
$extractedName = $this->extractName($name, $object);
// replace the original key with extracted, if differ
if ($extractedName !== $name) {
unset($data[$name]);
$name = $extractedName;
}
$data[$name] = $this->extractValue($name, $value, $object);
}
return $data;
}
/**
* Hydrate an object
*
* Hydrates an object by passing $data to either its exchangeArray() or
* populate() method.
*
* @param array $data
* @param object $object
* @return object
* @throws Exception\BadMethodCallException for an $object not implementing exchangeArray() or populate()
*/
public function hydrate(array $data, $object)
{
$replacement = [];
foreach ($data as $key => $value) {
$name = $this->hydrateName($key, $data);
$replacement[$name] = $this->hydrateValue($name, $value, $data);
}
if (is_callable([$object, 'exchangeArray'])) {
$object->exchangeArray($replacement);
} elseif (is_callable([$object, 'populate'])) {
$object->populate($replacement);
} else {
throw new Exception\BadMethodCallException(
sprintf('%s expects the provided object to implement exchangeArray() or populate()', __METHOD__)
);
}
return $object;
}
}
src/Hydrator/ClassMethods.php 0000666 00000021043 13605043111 0012227 0 ustar 00 setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
$this->callableMethodFilter = new OptionalParametersFilter();
$this->filterComposite->addFilter('is', new IsFilter());
$this->filterComposite->addFilter('has', new HasFilter());
$this->filterComposite->addFilter('get', new GetFilter());
$this->filterComposite->addFilter('parameter', new OptionalParametersFilter(), FilterComposite::CONDITION_AND);
}
/**
* @param array|Traversable $options
* @return ClassMethods
* @throws Exception\InvalidArgumentException
*/
public function setOptions($options)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException(
'The options parameter must be an array or a Traversable'
);
}
if (isset($options['underscoreSeparatedKeys'])) {
$this->setUnderscoreSeparatedKeys($options['underscoreSeparatedKeys']);
}
return $this;
}
/**
* @param bool $underscoreSeparatedKeys
* @return ClassMethods
*/
public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
{
$this->underscoreSeparatedKeys = (bool) $underscoreSeparatedKeys;
if ($this->underscoreSeparatedKeys) {
$this->setNamingStrategy(new UnderscoreNamingStrategy);
} elseif ($this->getNamingStrategy() instanceof UnderscoreNamingStrategy) {
$this->removeNamingStrategy();
}
return $this;
}
/**
* @return bool
*/
public function getUnderscoreSeparatedKeys()
{
return $this->underscoreSeparatedKeys;
}
/**
* Extract values from an object with class methods
*
* Extracts the getter/setter of the given $object.
*
* @param object $object
* @return array
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function extract($object)
{
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided $object to be a PHP object)',
__METHOD__
));
}
$objectClass = get_class($object);
// reset the hydrator's hydrator's cache for this object, as the filter may be per-instance
if ($object instanceof FilterProviderInterface) {
$this->extractionMethodsCache[$objectClass] = null;
}
// pass 1 - finding out which properties can be extracted, with which methods (populate hydration cache)
if (! isset($this->extractionMethodsCache[$objectClass])) {
$this->extractionMethodsCache[$objectClass] = [];
$filter = $this->filterComposite;
$methods = get_class_methods($object);
if ($object instanceof FilterProviderInterface) {
$filter = new FilterComposite(
[$object->getFilter()],
[new MethodMatchFilter('getFilter')]
);
}
foreach ($methods as $method) {
$methodFqn = $objectClass . '::' . $method;
if (! ($filter->filter($methodFqn) && $this->callableMethodFilter->filter($methodFqn))) {
continue;
}
$attribute = $method;
if (strpos($method, 'get') === 0) {
$attribute = substr($method, 3);
if (!property_exists($object, $attribute)) {
$attribute = lcfirst($attribute);
}
}
$this->extractionMethodsCache[$objectClass][$method] = $attribute;
}
}
$values = [];
// pass 2 - actually extract data
foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) {
$realAttributeName = $this->extractName($attributeName, $object);
$values[$realAttributeName] = $this->extractValue($realAttributeName, $object->$methodName(), $object);
}
return $values;
}
/**
* Hydrate an object by populating getter/setter methods
*
* Hydrates an object by getter/setter methods of the object.
*
* @param array $data
* @param object $object
* @return object
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function hydrate(array $data, $object)
{
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided $object to be a PHP object)',
__METHOD__
));
}
$objectClass = get_class($object);
foreach ($data as $property => $value) {
$propertyFqn = $objectClass . '::$' . $property;
if (! isset($this->hydrationMethodsCache[$propertyFqn])) {
$setterName = 'set' . ucfirst($this->hydrateName($property, $data));
$this->hydrationMethodsCache[$propertyFqn] = is_callable([$object, $setterName])
? $setterName
: false;
}
if ($this->hydrationMethodsCache[$propertyFqn]) {
$object->{$this->hydrationMethodsCache[$propertyFqn]}($this->hydrateValue($property, $value, $data));
}
}
return $object;
}
/**
* {@inheritDoc}
*/
public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
{
$this->resetCaches();
return parent::addFilter($name, $filter, $condition);
}
/**
* {@inheritDoc}
*/
public function removeFilter($name)
{
$this->resetCaches();
return parent::removeFilter($name);
}
/**
* {@inheritDoc}
*/
public function setNamingStrategy(NamingStrategyInterface $strategy)
{
$this->resetCaches();
return parent::setNamingStrategy($strategy);
}
/**
* {@inheritDoc}
*/
public function removeNamingStrategy()
{
$this->resetCaches();
return parent::removeNamingStrategy();
}
/**
* Reset all local hydration/extraction caches
*/
private function resetCaches()
{
$this->hydrationMethodsCache = $this->extractionMethodsCache = [];
}
}
src/Hydrator/DelegatingHydrator.php 0000666 00000002371 13605043111 0013421 0 ustar 00 hydrators = $hydrators;
}
/**
* {@inheritdoc}
*/
public function hydrate(array $data, $object)
{
return $this->getHydrator($object)->hydrate($data, $object);
}
/**
* {@inheritdoc}
*/
public function extract($object)
{
return $this->getHydrator($object)->extract($object);
}
/**
* Gets hydrator of an object
*
* @param object $object
* @return HydratorInterface
*/
protected function getHydrator($object)
{
return $this->hydrators->get(get_class($object));
}
}
src/Hydrator/DelegatingHydratorFactory.php 0000666 00000001570 13605043111 0014751 0 ustar 00 orFilter = new ArrayObject($orFilter);
$this->andFilter = new ArrayObject($andFilter);
}
/**
* Add a filter to the composite. Has to be indexed with $name in
* order to identify a specific filter.
*
* This example will exclude all methods from the hydration, that starts with 'getService'
*
* $composite->addFilter('exclude',
* function ($method) {
* if (preg_match('/^getService/', $method) {
* return false;
* }
* return true;
* }, FilterComposite::CONDITION_AND
* );
*
*
* @param string $name
* @param callable|FilterInterface $filter
* @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND
* @throws InvalidArgumentException
* @return FilterComposite
*/
public function addFilter($name, $filter, $condition = self::CONDITION_OR)
{
if (!is_callable($filter) && !($filter instanceof FilterInterface)) {
throw new InvalidArgumentException(
'The value of ' . $name . ' should be either a callable or ' .
'an instance of Laminas\Stdlib\Hydrator\Filter\FilterInterface'
);
}
if ($condition === self::CONDITION_OR) {
$this->orFilter[$name] = $filter;
} elseif ($condition === self::CONDITION_AND) {
$this->andFilter[$name] = $filter;
}
return $this;
}
/**
* Remove a filter from the composition
*
* @param $name string Identifier for the filter
* @return FilterComposite
*/
public function removeFilter($name)
{
if (isset($this->orFilter[$name])) {
unset($this->orFilter[$name]);
}
if (isset($this->andFilter[$name])) {
unset($this->andFilter[$name]);
}
return $this;
}
/**
* Check if $name has a filter registered
*
* @param $name string Identifier for the filter
* @return bool
*/
public function hasFilter($name)
{
return isset($this->orFilter[$name]) || isset($this->andFilter[$name]);
}
/**
* Filter the composite based on the AND and OR condition
* Will return true if one from the "or conditions" and all from
* the "and condition" returns true. Otherwise false
*
* @param $property string Parameter will be e.g. Parent\Namespace\Class::method
* @return bool
*/
public function filter($property)
{
$andCount = count($this->andFilter);
$orCount = count($this->orFilter);
// return true if no filters are registered
if ($orCount === 0 && $andCount === 0) {
return true;
} elseif ($orCount === 0 && $andCount !== 0) {
$returnValue = true;
} else {
$returnValue = false;
}
// Check if 1 from the or filters return true
foreach ($this->orFilter as $filter) {
if (is_callable($filter)) {
if ($filter($property) === true) {
$returnValue = true;
break;
}
continue;
} else {
if ($filter->filter($property) === true) {
$returnValue = true;
break;
}
}
}
// Check if all of the and condition return true
foreach ($this->andFilter as $filter) {
if (is_callable($filter)) {
if ($filter($property) === false) {
return false;
}
continue;
} else {
if ($filter->filter($property) === false) {
return false;
}
}
}
return $returnValue;
}
}
src/Hydrator/Filter/FilterInterface.php 0000666 00000001063 13605043111 0014131 0 ustar 00 method = $method;
$this->exclude = $exclude;
}
public function filter($property)
{
$pos = strpos($property, '::');
if ($pos !== false) {
$pos += 2;
} else {
$pos = 0;
}
if (substr($property, $pos) === $this->method) {
return !$this->exclude;
}
return $this->exclude;
}
}
src/Hydrator/Filter/NumberOfParameterFilter.php 0000666 00000002513 13605043111 0015610 0 ustar 00 numberOfParameters = (int) $numberOfParameters;
}
/**
* @param string $property the name of the property
* @return bool
* @throws InvalidArgumentException
*/
public function filter($property)
{
try {
$reflectionMethod = new ReflectionMethod($property);
} catch (ReflectionException $exception) {
throw new InvalidArgumentException(
"Method $property doesn't exist"
);
}
return $reflectionMethod->getNumberOfParameters() === $this->numberOfParameters;
}
}
src/Hydrator/Filter/OptionalParametersFilter.php 0000666 00000003066 13605043111 0016047 0 ustar 00 getParameters(),
function (ReflectionParameter $parameter) {
return ! $parameter->isOptional();
}
);
return static::$propertiesCache[$property] = empty($mandatoryParameters);
}
}
src/Hydrator/FilterEnabledInterface.php 0000666 00000003470 13605043111 0014163 0 ustar 00
* $composite->addFilter(
* "servicelocator",
* function ($property) {
* list($class, $method) = explode('::', $property);
* if ($method === 'getServiceLocator') {
* return false;
* }
* return true;
* },
* FilterComposite::CONDITION_AND
* );
*
*
* @param string $name Index in the composite
* @param callable|FilterInterface $filter
* @param int $condition
* @return FilterComposite
*/
public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR);
/**
* Check whether a specific filter exists at key $name or not
*
* @param string $name Index in the composite
* @return bool
*/
public function hasFilter($name);
/**
* Remove a filter from the composition.
* To not extract "has" methods, you simply need to unregister it
*
*
* $filterComposite->removeFilter('has');
*
*
* @param $name
* @return FilterComposite
*/
public function removeFilter($name);
}
src/Hydrator/HydrationInterface.php 0000666 00000001052 13605043111 0013416 0 ustar 00 hydrator = $hydrator;
return $this;
}
/**
* Retrieve hydrator
*
* @param void
* @return null|HydratorInterface
* @access public
*/
public function getHydrator()
{
return $this->hydrator;
}
}
src/Hydrator/HydratorInterface.php 0000666 00000000703 13605043111 0013253 0 ustar 00 'Laminas\Stdlib\Hydrator\DelegatingHydrator',
// Legacy Zend Framework aliases
// v2 normalized FQCNs
];
/**
* Default set of adapters
*
* @var array
*/
protected $invokableClasses = [
'arrayserializable' => 'Laminas\Stdlib\Hydrator\ArraySerializable',
'classmethods' => 'Laminas\Stdlib\Hydrator\ClassMethods',
'objectproperty' => 'Laminas\Stdlib\Hydrator\ObjectProperty',
'reflection' => 'Laminas\Stdlib\Hydrator\Reflection'
];
/**
* Default factory-based adapters
*
* @var array
*/
protected $factories = [
'Laminas\Stdlib\Hydrator\DelegatingHydrator' => 'Laminas\Stdlib\Hydrator\DelegatingHydratorFactory',
];
/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof HydratorInterface) {
// we're okay
return;
}
throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement Laminas\Stdlib\Hydrator\HydratorInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
}
src/Hydrator/NamingStrategy/ArrayMapNamingStrategy.php 0000666 00000002374 13605043111 0017171 0 ustar 00 extractionMap = $extractionMap;
$this->hydrationMap = array_flip($extractionMap);
}
/**
* {@inheritDoc}
*/
public function hydrate($name)
{
return isset($this->hydrationMap[$name]) ? $this->hydrationMap[$name] : $name;
}
/**
* {@inheritDoc}
*/
public function extract($name)
{
return isset($this->extractionMap[$name]) ? $this->extractionMap[$name] : $name;
}
}
src/Hydrator/NamingStrategy/CompositeNamingStrategy.php 0000666 00000003355 13605043111 0017417 0 ustar 00 namingStrategies = array_map(
function (NamingStrategyInterface $strategy) {
// this callback is here only to ensure type-safety
return $strategy;
},
$strategies
);
$this->defaultNamingStrategy = $defaultNamingStrategy ?: new IdentityNamingStrategy();
}
/**
* {@inheritDoc}
*/
public function extract($name)
{
$strategy = isset($this->namingStrategies[$name])
? $this->namingStrategies[$name]
: $this->defaultNamingStrategy;
return $strategy->extract($name);
}
/**
* {@inheritDoc}
*/
public function hydrate($name)
{
$strategy = isset($this->namingStrategies[$name])
? $this->namingStrategies[$name]
: $this->defaultNamingStrategy;
return $strategy->hydrate($name);
}
}
src/Hydrator/NamingStrategy/IdentityNamingStrategy.php 0000666 00000001156 13605043111 0017243 0 ustar 00 mapping = $mapping;
$this->reverse = $reverse ?: $this->flipMapping($mapping);
}
/**
* Safelly flip mapping array.
*
* @param array $array Array to flip
* @return array Flipped array
* @throws InvalidArgumentException
*/
protected function flipMapping(array $array)
{
array_walk($array, function ($value) {
if (!is_string($value) && !is_int($value)) {
throw new InvalidArgumentException('Mapping array can\'t be flipped because of invalid value');
}
});
return array_flip($array);
}
/**
* Converts the given name so that it can be extracted by the hydrator.
*
* @param string $name The original name
* @return mixed The hydrated name
*/
public function hydrate($name)
{
if (array_key_exists($name, $this->mapping)) {
return $this->mapping[$name];
}
return $name;
}
/**
* Converts the given name so that it can be hydrated by the hydrator.
*
* @param string $name The original name
* @return mixed The extracted name
*/
public function extract($name)
{
if (array_key_exists($name, $this->reverse)) {
return $this->reverse[$name];
}
return $name;
}
}
src/Hydrator/NamingStrategy/NamingStrategyInterface.php 0000666 00000002137 13605043111 0017352 0 ustar 00 getUnderscoreToStudlyCaseFilter()->filter($name);
}
/**
* Remove capitalized letters and prepend underscores.
*
* @param string $name
* @return string
*/
public function extract($name)
{
return $this->getCamelCaseToUnderscoreFilter()->filter($name);
}
/**
* @return FilterChain
*/
protected function getUnderscoreToStudlyCaseFilter()
{
if (static::$underscoreToStudlyCaseFilter instanceof FilterChain) {
return static::$underscoreToStudlyCaseFilter;
}
$filter = new FilterChain();
$filter->attachByName('WordUnderscoreToStudlyCase');
return static::$underscoreToStudlyCaseFilter = $filter;
}
/**
* @return FilterChain
*/
protected function getCamelCaseToUnderscoreFilter()
{
if (static::$camelCaseToUnderscoreFilter instanceof FilterChain) {
return static::$camelCaseToUnderscoreFilter;
}
$filter = new FilterChain();
$filter->attachByName('WordCamelCaseToUnderscore');
$filter->attachByName('StringToLower');
return static::$camelCaseToUnderscoreFilter = $filter;
}
}
src/Hydrator/NamingStrategyEnabledInterface.php 0000666 00000002131 13605043111 0015663 0 ustar 00 getFilter();
foreach ($data as $name => $value) {
// Filter keys, removing any we don't want
if (! $filter->filter($name)) {
unset($data[$name]);
continue;
}
// Replace name if extracted differ
$extracted = $this->extractName($name, $object);
if ($extracted !== $name) {
unset($data[$name]);
$name = $extracted;
}
$data[$name] = $this->extractValue($name, $value, $object);
}
return $data;
}
/**
* {@inheritDoc}
*
* Hydrate an object by populating public properties
*
* Hydrates an object by setting public properties of the object.
*
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function hydrate(array $data, $object)
{
if (!is_object($object)) {
throw new Exception\BadMethodCallException(
sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
);
}
$properties = & self::$skippedPropertiesCache[get_class($object)];
if (! isset($properties)) {
$reflection = new ReflectionClass($object);
$properties = array_fill_keys(
array_map(
function (ReflectionProperty $property) {
return $property->getName();
},
$reflection->getProperties(
ReflectionProperty::IS_PRIVATE
+ ReflectionProperty::IS_PROTECTED
+ ReflectionProperty::IS_STATIC
)
),
true
);
}
foreach ($data as $name => $value) {
$property = $this->hydrateName($name, $data);
if (isset($properties[$property])) {
continue;
}
$object->$property = $this->hydrateValue($property, $value, $data);
}
return $object;
}
}
src/Hydrator/Reflection.php 0000666 00000005426 13605043111 0011737 0 ustar 00 extractName($property->getName(), $object);
if (!$this->filterComposite->filter($propertyName)) {
continue;
}
$value = $property->getValue($object);
$result[$propertyName] = $this->extractValue($propertyName, $value, $object);
}
return $result;
}
/**
* Hydrate $object with the provided $data.
*
* @param array $data
* @param object $object
* @return object
*/
public function hydrate(array $data, $object)
{
$reflProperties = self::getReflProperties($object);
foreach ($data as $key => $value) {
$name = $this->hydrateName($key, $data);
if (isset($reflProperties[$name])) {
$reflProperties[$name]->setValue($object, $this->hydrateValue($name, $value, $data));
}
}
return $object;
}
/**
* Get a reflection properties from in-memory cache and lazy-load if
* class has not been loaded.
*
* @param string|object $input
* @throws Exception\InvalidArgumentException
* @return \ReflectionProperty[]
*/
protected static function getReflProperties($input)
{
if (is_object($input)) {
$input = get_class($input);
} elseif (!is_string($input)) {
throw new Exception\InvalidArgumentException('Input must be a string or an object.');
}
if (isset(static::$reflProperties[$input])) {
return static::$reflProperties[$input];
}
static::$reflProperties[$input] = [];
$reflClass = new ReflectionClass($input);
$reflProperties = $reflClass->getProperties();
foreach ($reflProperties as $property) {
$property->setAccessible(true);
static::$reflProperties[$input][$property->getName()] = $property;
}
return static::$reflProperties[$input];
}
}
src/Hydrator/Strategy/BooleanStrategy.php 0000666 00000006311 13605043111 0014543 0 ustar 00 trueValue = $trueValue;
$this->falseValue = $falseValue;
}
/**
* Converts the given value so that it can be extracted by the hydrator.
*
* @param bool $value The original value.
* @throws InvalidArgumentException
* @return int|string Returns the value that should be extracted.
*/
public function extract($value)
{
if (!is_bool($value)) {
throw new InvalidArgumentException(sprintf(
'Unable to extract. Expected bool. %s was given.',
is_object($value) ? get_class($value) : gettype($value)
));
}
return $value === true ? $this->trueValue : $this->falseValue;
}
/**
* Converts the given value so that it can be hydrated by the hydrator.
*
* @param int|string $value The original value.
* @throws InvalidArgumentException
* @return bool Returns the value that should be hydrated.
*/
public function hydrate($value)
{
if (!is_string($value) && !is_int($value)) {
throw new InvalidArgumentException(sprintf(
'Unable to hydrate. Expected string or int. %s was given.',
is_object($value) ? get_class($value) : gettype($value)
));
}
if ($value === $this->trueValue) {
return true;
}
if ($value === $this->falseValue) {
return false;
}
throw new InvalidArgumentException(sprintf(
'Unexpected value %s can\'t be hydrated. Expect %s or %s as Value.',
$value,
$this->trueValue,
$this->falseValue
));
}
}
src/Hydrator/Strategy/ClosureStrategy.php 0000666 00000005567 13605043111 0014614 0 ustar 00 addStrategy('category', new ClosureStrategy(
* function (Category $value) {
* return (int) $value->id;
* },
* function ($value) {
* return new Category((int) $value);
* }
* ));
*
* @param callable $extractFunc - anonymous function, that extract values
* from object
* @param callable $hydrateFunc - anonymous function, that hydrate values
* into object
*/
public function __construct($extractFunc = null, $hydrateFunc = null)
{
if (isset($extractFunc)) {
if (!is_callable($extractFunc)) {
throw new \Exception('$extractFunc must be callable');
}
$this->extractFunc = $extractFunc;
} else {
$this->extractFunc = function ($value) {
return $value;
};
}
if (isset($hydrateFunc)) {
if (!is_callable($hydrateFunc)) {
throw new \Exception('$hydrateFunc must be callable');
}
$this->hydrateFunc = $hydrateFunc;
} else {
$this->hydrateFunc = function ($value) {
return $value;
};
}
}
/**
* Converts the given value so that it can be extracted by the hydrator.
*
* @param mixed $value The original value.
* @param array $object The object is optionally provided as context.
* @return mixed Returns the value that should be extracted.
*/
public function extract($value, $object = null)
{
$func = $this->extractFunc;
return $func($value, $object);
}
/**
* Converts the given value so that it can be hydrated by the hydrator.
*
* @param mixed $value The original value.
* @param array $data The whole data is optionally provided as context.
* @return mixed Returns the value that should be hydrated.
*/
public function hydrate($value, $data = null)
{
$func = $this->hydrateFunc;
return $func($value, $data);
}
}
src/Hydrator/Strategy/DateTimeFormatterStrategy.php 0000666 00000003400 13605043111 0016540 0 ustar 00 format = (string) $format;
$this->timezone = $timezone;
}
/**
* {@inheritDoc}
*
* Converts to date time string
*
* @param mixed|DateTime $value
*
* @return mixed|string
*/
public function extract($value)
{
if ($value instanceof DateTime) {
return $value->format($this->format);
}
return $value;
}
/**
* Converts date time string to DateTime instance for injecting to object
*
* {@inheritDoc}
*
* @param mixed|string $value
*
* @return mixed|DateTime
*/
public function hydrate($value)
{
if ($value === '' || $value === null) {
return;
}
if ($this->timezone) {
$hydrated = DateTime::createFromFormat($this->format, $value, $this->timezone);
} else {
$hydrated = DateTime::createFromFormat($this->format, $value);
}
return $hydrated ?: $value;
}
}
src/Hydrator/Strategy/DefaultStrategy.php 0000666 00000001665 13605043111 0014557 0 ustar 00 setValueDelimiter($delimiter);
$this->explodeLimit = ($explodeLimit === null) ? null : (int) $explodeLimit;
}
/**
* Sets the delimiter string that the values will be split upon
*
* @param string $delimiter
* @return self
*/
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;
}
/**
* {@inheritDoc}
*
* Split a string by delimiter
*
* @param string|null $value
*
* @return string[]
*
* @throws Exception\InvalidArgumentException
*/
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);
}
/**
* {@inheritDoc}
*
* Join array elements with delimiter
*
* @param string[] $value The original value.
*
* @return string|null
*/
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);
}
}
src/Hydrator/Strategy/SerializableStrategy.php 0000666 00000006657 13605043111 0015607 0 ustar 00 setSerializer($serializer);
if ($serializerOptions) {
$this->setSerializerOptions($serializerOptions);
}
}
/**
* Serialize the given value so that it can be extracted by the hydrator.
*
* @param mixed $value The original value.
* @return mixed Returns the value that should be extracted.
*/
public function extract($value)
{
$serializer = $this->getSerializer();
return $serializer->serialize($value);
}
/**
* Unserialize the given value so that it can be hydrated by the hydrator.
*
* @param mixed $value The original value.
* @return mixed Returns the value that should be hydrated.
*/
public function hydrate($value)
{
$serializer = $this->getSerializer();
return $serializer->unserialize($value);
}
/**
* Set serializer
*
* @param string|SerializerAdapter $serializer
* @return SerializableStrategy
*/
public function setSerializer($serializer)
{
if (!is_string($serializer) && !$serializer instanceof SerializerAdapter) {
throw new InvalidArgumentException(sprintf(
'%s expects either a string serializer name or Laminas\Serializer\Adapter\AdapterInterface instance; '
. 'received "%s"',
__METHOD__,
(is_object($serializer) ? get_class($serializer) : gettype($serializer))
));
}
$this->serializer = $serializer;
return $this;
}
/**
* Get serializer
*
* @return SerializerAdapter
*/
public function getSerializer()
{
if (is_string($this->serializer)) {
$options = $this->getSerializerOptions();
$this->setSerializer(SerializerFactory::factory($this->serializer, $options));
} elseif (null === $this->serializer) {
$this->setSerializer(SerializerFactory::getDefaultAdapter());
}
return $this->serializer;
}
/**
* Set configuration options for instantiating a serializer adapter
*
* @param mixed $serializerOptions
* @return SerializableStrategy
*/
public function setSerializerOptions($serializerOptions)
{
$this->serializerOptions = $serializerOptions;
return $this;
}
/**
* Get configuration options for instantiating a serializer adapter
*
* @return mixed
*/
public function getSerializerOptions()
{
return $this->serializerOptions;
}
}
src/Hydrator/Strategy/StrategyChain.php 0000666 00000003311 13605043111 0014203 0 ustar 00 extractionStrategies = array_map(
function (StrategyInterface $strategy) {
// this callback is here only to ensure type-safety
return $strategy;
},
$extractionStrategies
);
$this->hydrationStrategies = array_reverse($extractionStrategies);
}
/**
* {@inheritDoc}
*/
public function extract($value)
{
foreach ($this->extractionStrategies as $strategy) {
$value = $strategy->extract($value);
}
return $value;
}
/**
* {@inheritDoc}
*/
public function hydrate($value)
{
foreach ($this->hydrationStrategies as $strategy) {
$value = $strategy->hydrate($value);
}
return $value;
}
}
src/Hydrator/Strategy/StrategyInterface.php 0000666 00000002056 13605043111 0015066 0 ustar 00 metadata[$spec] = $value;
return $this;
}
if (!is_array($spec) && !$spec instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected a string, array, or Traversable argument in first position; received "%s"',
(is_object($spec) ? get_class($spec) : gettype($spec))
));
}
foreach ($spec as $key => $value) {
$this->metadata[$key] = $value;
}
return $this;
}
/**
* Retrieve all metadata or a single metadatum as specified by key
*
* @param null|string|int $key
* @param null|mixed $default
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function getMetadata($key = null, $default = null)
{
if (null === $key) {
return $this->metadata;
}
if (!is_scalar($key)) {
throw new Exception\InvalidArgumentException('Non-scalar argument provided for key');
}
if (array_key_exists($key, $this->metadata)) {
return $this->metadata[$key];
}
return $default;
}
/**
* Set message content
*
* @param mixed $value
* @return Message
*/
public function setContent($value)
{
$this->content = $value;
return $this;
}
/**
* Get message content
*
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @return string
*/
public function toString()
{
$request = '';
foreach ($this->getMetadata() as $key => $value) {
$request .= sprintf(
"%s: %s\r\n",
(string) $key,
(string) $value
);
}
$request .= "\r\n" . $this->getContent();
return $request;
}
}
src/MessageInterface.php 0000666 00000001604 13605043111 0011250 0 ustar 00 exchangeArray($values);
}
/**
* Populate from query string
*
* @param string $string
* @return void
*/
public function fromString($string)
{
$array = [];
parse_str($string, $array);
$this->fromArray($array);
}
/**
* Serialize to native PHP array
*
* @return array
*/
public function toArray()
{
return $this->getArrayCopy();
}
/**
* Serialize to query string
*
* @return string
*/
public function toString()
{
return http_build_query($this);
}
/**
* Retrieve by key
*
* Returns null if the key does not exist.
*
* @param string $name
* @return mixed
*/
public function offsetGet($name)
{
if ($this->offsetExists($name)) {
return parent::offsetGet($name);
}
return;
}
/**
* @param string $name
* @param mixed $default optional default value
* @return mixed
*/
public function get($name, $default = null)
{
if ($this->offsetExists($name)) {
return parent::offsetGet($name);
}
return $default;
}
/**
* @param string $name
* @param mixed $value
* @return Parameters
*/
public function set($name, $value)
{
$this[$name] = $value;
return $this;
}
}
src/ParametersInterface.php 0000666 00000003445 13605043111 0011774 0 ustar 00 items[$name])) {
$this->count++;
}
$this->sorted = false;
$this->items[$name] = [
'data' => $value,
'priority' => (int) $priority,
'serial' => $this->serial++,
];
}
/**
* @param string $name
* @param int $priority
*
* @return $this
*
* @throws \Exception
*/
public function setPriority($name, $priority)
{
if (!isset($this->items[$name])) {
throw new \Exception("item $name not found");
}
$this->items[$name]['priority'] = (int) $priority;
$this->sorted = false;
return $this;
}
/**
* Remove a item.
*
* @param string $name
* @return void
*/
public function remove($name)
{
if (isset($this->items[$name])) {
$this->count--;
}
unset($this->items[$name]);
}
/**
* Remove all items.
*
* @return void
*/
public function clear()
{
$this->items = [];
$this->serial = 0;
$this->count = 0;
$this->sorted = false;
}
/**
* Get a item.
*
* @param string $name
* @return mixed
*/
public function get($name)
{
if (!isset($this->items[$name])) {
return;
}
return $this->items[$name]['data'];
}
/**
* Sort all items.
*
* @return void
*/
protected function sort()
{
if (!$this->sorted) {
uasort($this->items, [$this, 'compare']);
$this->sorted = true;
}
}
/**
* Compare the priority of two items.
*
* @param array $item1,
* @param array $item2
* @return int
*/
protected function compare(array $item1, array $item2)
{
return ($item1['priority'] === $item2['priority'])
? ($item1['serial'] > $item2['serial'] ? -1 : 1) * $this->isLIFO
: ($item1['priority'] > $item2['priority'] ? -1 : 1);
}
/**
* Get/Set serial order mode
*
* @param bool|null $flag
*
* @return bool
*/
public function isLIFO($flag = null)
{
if ($flag !== null) {
$isLifo = $flag === true ? 1 : -1;
if ($isLifo !== $this->isLIFO) {
$this->isLIFO = $isLifo;
$this->sorted = false;
}
}
return 1 === $this->isLIFO;
}
/**
* {@inheritDoc}
*/
public function rewind()
{
$this->sort();
reset($this->items);
}
/**
* {@inheritDoc}
*/
public function current()
{
$this->sorted || $this->sort();
$node = current($this->items);
return $node ? $node['data'] : false;
}
/**
* {@inheritDoc}
*/
public function key()
{
$this->sorted || $this->sort();
return key($this->items);
}
/**
* {@inheritDoc}
*/
public function next()
{
$node = next($this->items);
return $node ? $node['data'] : false;
}
/**
* {@inheritDoc}
*/
public function valid()
{
return current($this->items) !== false;
}
/**
* @return self
*/
public function getIterator()
{
return clone $this;
}
/**
* {@inheritDoc}
*/
public function count()
{
return $this->count;
}
/**
* Return list as array
*
* @param int $flag
*
* @return array
*/
public function toArray($flag = self::EXTR_DATA)
{
$this->sort();
if ($flag == self::EXTR_BOTH) {
return $this->items;
}
return array_map(
function ($item) use ($flag) {
return ($flag == PriorityList::EXTR_PRIORITY) ? $item['priority'] : $item['data'];
},
$this->items
);
}
}
src/PriorityQueue.php 0000666 00000017272 13605043111 0010701 0 ustar 00 items[] = [
'data' => $data,
'priority' => $priority,
];
$this->getQueue()->insert($data, $priority);
return $this;
}
/**
* Remove an item from the queue
*
* This is different than {@link extract()}; its purpose is to dequeue an
* item.
*
* This operation is potentially expensive, as it requires
* re-initialization and re-population of the inner queue.
*
* Note: this removes the first item matching the provided item found. If
* the same item has been added multiple times, it will not remove other
* instances.
*
* @param mixed $datum
* @return bool False if the item was not found, true otherwise.
*/
public function remove($datum)
{
$found = false;
foreach ($this->items as $key => $item) {
if ($item['data'] === $datum) {
$found = true;
break;
}
}
if ($found) {
unset($this->items[$key]);
$this->queue = null;
if (!$this->isEmpty()) {
$queue = $this->getQueue();
foreach ($this->items as $item) {
$queue->insert($item['data'], $item['priority']);
}
}
return true;
}
return false;
}
/**
* Is the queue empty?
*
* @return bool
*/
public function isEmpty()
{
return (0 === $this->count());
}
/**
* How many items are in the queue?
*
* @return int
*/
public function count()
{
return count($this->items);
}
/**
* Peek at the top node in the queue, based on priority.
*
* @return mixed
*/
public function top()
{
return $this->getIterator()->top();
}
/**
* Extract a node from the inner queue and sift up
*
* @return mixed
*/
public function extract()
{
return $this->getQueue()->extract();
}
/**
* Retrieve the inner iterator
*
* SplPriorityQueue acts as a heap, which typically implies that as items
* are iterated, they are also removed. This does not work for situations
* where the queue may be iterated multiple times. As such, this class
* aggregates the values, and also injects an SplPriorityQueue. This method
* retrieves the inner queue object, and clones it for purposes of
* iteration.
*
* @return SplPriorityQueue
*/
public function getIterator()
{
$queue = $this->getQueue();
return clone $queue;
}
/**
* Serialize the data structure
*
* @return string
*/
public function serialize()
{
return serialize($this->items);
}
/**
* Unserialize a string into a PriorityQueue object
*
* Serialization format is compatible with {@link Laminas\Stdlib\SplPriorityQueue}
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
foreach (unserialize($data) as $item) {
$this->insert($item['data'], $item['priority']);
}
}
/**
* Serialize to an array
*
* By default, returns only the item data, and in the order registered (not
* sorted). You may provide one of the EXTR_* flags as an argument, allowing
* the ability to return priorities or both data and priority.
*
* @param int $flag
* @return array
*/
public function toArray($flag = self::EXTR_DATA)
{
switch ($flag) {
case self::EXTR_BOTH:
return $this->items;
case self::EXTR_PRIORITY:
return array_map(function ($item) {
return $item['priority'];
}, $this->items);
case self::EXTR_DATA:
default:
return array_map(function ($item) {
return $item['data'];
}, $this->items);
}
}
/**
* Specify the internal queue class
*
* Please see {@link getIterator()} for details on the necessity of an
* internal queue class. The class provided should extend SplPriorityQueue.
*
* @param string $class
* @return PriorityQueue
*/
public function setInternalQueueClass($class)
{
$this->queueClass = (string) $class;
return $this;
}
/**
* Does the queue contain the given datum?
*
* @param mixed $datum
* @return bool
*/
public function contains($datum)
{
foreach ($this->items as $item) {
if ($item['data'] === $datum) {
return true;
}
}
return false;
}
/**
* Does the queue have an item with the given priority?
*
* @param int $priority
* @return bool
*/
public function hasPriority($priority)
{
foreach ($this->items as $item) {
if ($item['priority'] === $priority) {
return true;
}
}
return false;
}
/**
* Get the inner priority queue instance
*
* @throws Exception\DomainException
* @return SplPriorityQueue
*/
protected function getQueue()
{
if (null === $this->queue) {
$this->queue = new $this->queueClass();
if (!$this->queue instanceof \SplPriorityQueue) {
throw new Exception\DomainException(sprintf(
'PriorityQueue expects an internal queue of type SplPriorityQueue; received "%s"',
get_class($this->queue)
));
}
}
return $this->queue;
}
/**
* Add support for deep cloning
*
* @return void
*/
public function __clone()
{
if (null !== $this->queue) {
$this->queue = clone $this->queue;
}
}
}
src/Request.php 0000666 00000000633 13605043111 0007474 0 ustar 00 serial--];
}
parent::insert($datum, $priority);
}
/**
* Serialize to an array
*
* Array will be priority => data pairs
*
* @return array
*/
public function toArray()
{
$array = [];
foreach (clone $this as $item) {
$array[] = $item;
}
return $array;
}
/**
* Serialize
*
* @return string
*/
public function serialize()
{
$clone = clone $this;
$clone->setExtractFlags(self::EXTR_BOTH);
$data = [];
foreach ($clone as $item) {
$data[] = $item;
}
return serialize($data);
}
/**
* Deserialize
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
foreach (unserialize($data) as $item) {
$this->insert($item['data'], $item['priority']);
}
}
}
src/SplQueue.php 0000666 00000002055 13605043111 0007607 0 ustar 00 toArray());
}
/**
* Unserialize
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
foreach (unserialize($data) as $item) {
$this->push($item);
}
}
}
src/SplStack.php 0000666 00000002066 13605043111 0007572 0 ustar 00 toArray());
}
/**
* Unserialize
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
foreach (unserialize($data) as $item) {
$this->unshift($item);
}
}
}
src/StringUtils.php 0000666 00000012471 13605043111 0010336 0 ustar 00 setEncoding($encoding, $convertEncoding);
return $wrapper;
}
}
throw new Exception\RuntimeException(
'No wrapper found supporting "' . $encoding . '"'
. (($convertEncoding !== null) ? ' and "' . $convertEncoding . '"' : '')
);
}
/**
* Get a list of all known single-byte character encodings
*
* @return string[]
*/
public static function getSingleByteEncodings()
{
return static::$singleByteEncodings;
}
/**
* Check if a given encoding is a known single-byte character encoding
*
* @param string $encoding
* @return bool
*/
public static function isSingleByteEncoding($encoding)
{
return in_array(strtoupper($encoding), static::$singleByteEncodings);
}
/**
* Check if a given string is valid UTF-8 encoded
*
* @param string $str
* @return bool
*/
public static function isValidUtf8($str)
{
return is_string($str) && ($str === '' || preg_match('/^./su', $str) == 1);
}
/**
* Is PCRE compiled with Unicode support?
*
* @return bool
*/
public static function hasPcreUnicodeSupport()
{
if (static::$hasPcreUnicodeSupport === null) {
ErrorHandler::start();
static::$hasPcreUnicodeSupport = defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
ErrorHandler::stop();
}
return static::$hasPcreUnicodeSupport;
}
}
src/StringWrapper/AbstractStringWrapper.php 0000666 00000020566 13605043111 0015155 0 ustar 00 convertEncoding = $convertEncodingUpper;
} else {
$this->convertEncoding = null;
}
$this->encoding = $encodingUpper;
return $this;
}
/**
* Get the defined character encoding to work with
*
* @return string
* @throws Exception\LogicException If no encoding was defined
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Get the defined character encoding to convert to
*
* @return string|null
*/
public function getConvertEncoding()
{
return $this->convertEncoding;
}
/**
* Convert a string from defined character encoding to the defined convert encoding
*
* @param string $str
* @param bool $reverse
* @return string|false
*/
public function convert($str, $reverse = false)
{
$encoding = $this->getEncoding();
$convertEncoding = $this->getConvertEncoding();
if ($convertEncoding === null) {
throw new Exception\LogicException(
'No convert encoding defined'
);
}
if ($encoding === $convertEncoding) {
return $str;
}
$from = $reverse ? $convertEncoding : $encoding;
$to = $reverse ? $encoding : $convertEncoding;
throw new Exception\RuntimeException(sprintf(
'Converting from "%s" to "%s" isn\'t supported by this string wrapper',
$from,
$to
));
}
/**
* Wraps a string to a given number of characters
*
* @param string $string
* @param int $width
* @param string $break
* @param bool $cut
* @return string|false
*/
public function wordWrap($string, $width = 75, $break = "\n", $cut = false)
{
$string = (string) $string;
if ($string === '') {
return '';
}
$break = (string) $break;
if ($break === '') {
throw new Exception\InvalidArgumentException('Break string cannot be empty');
}
$width = (int) $width;
if ($width === 0 && $cut) {
throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
}
if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
return wordwrap($string, $width, $break, $cut);
}
$stringWidth = $this->strlen($string);
$breakWidth = $this->strlen($break);
$result = '';
$lastStart = $lastSpace = 0;
for ($current = 0; $current < $stringWidth; $current++) {
$char = $this->substr($string, $current, 1);
$possibleBreak = $char;
if ($breakWidth !== 1) {
$possibleBreak = $this->substr($string, $current, $breakWidth);
}
if ($possibleBreak === $break) {
$result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth);
$current += $breakWidth - 1;
$lastStart = $lastSpace = $current + 1;
continue;
}
if ($char === ' ') {
if ($current - $lastStart >= $width) {
$result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
$lastStart = $current + 1;
}
$lastSpace = $current;
continue;
}
if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
$result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
$lastStart = $lastSpace = $current;
continue;
}
if ($current - $lastStart >= $width && $lastStart < $lastSpace) {
$result .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break;
$lastStart = $lastSpace = $lastSpace + 1;
continue;
}
}
if ($lastStart !== $current) {
$result .= $this->substr($string, $lastStart, $current - $lastStart);
}
return $result;
}
/**
* Pad a string to a certain length with another string
*
* @param string $input
* @param int $padLength
* @param string $padString
* @param int $padType
* @return string
*/
public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT)
{
if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
return str_pad($input, $padLength, $padString, $padType);
}
$lengthOfPadding = $padLength - $this->strlen($input);
if ($lengthOfPadding <= 0) {
return $input;
}
$padStringLength = $this->strlen($padString);
if ($padStringLength === 0) {
return $input;
}
$repeatCount = floor($lengthOfPadding / $padStringLength);
if ($padType === STR_PAD_BOTH) {
$repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
$lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
$lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
$lastStringRightLength += $lastStringLength % 2;
$lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength);
$lastStringRight = $this->substr($padString, 0, $lastStringRightLength);
return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
. $input
. str_repeat($padString, $repeatCountRight) . $lastStringRight;
}
$lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength);
if ($padType === STR_PAD_LEFT) {
return str_repeat($padString, $repeatCount) . $lastString . $input;
}
return $input . str_repeat($padString, $repeatCount) . $lastString;
}
}
src/StringWrapper/Iconv.php 0000666 00000014731 13605043111 0011735 0 ustar 00 getEncoding());
}
/**
* Returns the portion of string specified by the start and length parameters
*
* @param string $str
* @param int $offset
* @param int|null $length
* @return string|false
*/
public function substr($str, $offset = 0, $length = null)
{
return iconv_substr($str, $offset, $length, $this->getEncoding());
}
/**
* Find the position of the first occurrence of a substring in a string
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @return int|false
*/
public function strpos($haystack, $needle, $offset = 0)
{
return iconv_strpos($haystack, $needle, $offset, $this->getEncoding());
}
/**
* Convert a string from defined encoding to the defined convert encoding
*
* @param string $str
* @param bool $reverse
* @return string|false
*/
public function convert($str, $reverse = false)
{
$encoding = $this->getEncoding();
$convertEncoding = $this->getConvertEncoding();
if ($convertEncoding === null) {
throw new Exception\LogicException(
'No convert encoding defined'
);
}
if ($encoding === $convertEncoding) {
return $str;
}
$fromEncoding = $reverse ? $convertEncoding : $encoding;
$toEncoding = $reverse ? $encoding : $convertEncoding;
// automatically add "//IGNORE" to not stop converting on invalid characters
// invalid characters triggers a notice anyway
return iconv($fromEncoding, $toEncoding . '//IGNORE', $str);
}
}
src/StringWrapper/Intl.php 0000666 00000004171 13605043111 0011562 0 ustar 00 getEncoding());
}
/**
* Returns the portion of string specified by the start and length parameters
*
* @param string $str
* @param int $offset
* @param int|null $length
* @return string|false
*/
public function substr($str, $offset = 0, $length = null)
{
return mb_substr($str, $offset, $length, $this->getEncoding());
}
/**
* Find the position of the first occurrence of a substring in a string
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @return int|false
*/
public function strpos($haystack, $needle, $offset = 0)
{
return mb_strpos($haystack, $needle, $offset, $this->getEncoding());
}
/**
* Convert a string from defined encoding to the defined convert encoding
*
* @param string $str
* @param bool $reverse
* @return string|false
*/
public function convert($str, $reverse = false)
{
$encoding = $this->getEncoding();
$convertEncoding = $this->getConvertEncoding();
if ($convertEncoding === null) {
throw new Exception\LogicException(
'No convert encoding defined'
);
}
if ($encoding === $convertEncoding) {
return $str;
}
$fromEncoding = $reverse ? $convertEncoding : $encoding;
$toEncoding = $reverse ? $encoding : $convertEncoding;
return mb_convert_encoding($str, $toEncoding, $fromEncoding);
}
}
src/StringWrapper/Native.php 0000666 00000007314 13605043111 0012104 0 ustar 00 convertEncoding = $encodingUpper;
}
if ($convertEncoding !== null) {
if ($encodingUpper !== strtoupper($convertEncoding)) {
throw new Exception\InvalidArgumentException(
'Wrapper doesn\'t support to convert between character encodings'
);
}
$this->convertEncoding = $encodingUpper;
} else {
$this->convertEncoding = null;
}
$this->encoding = $encodingUpper;
return $this;
}
/**
* Returns the length of the given string
*
* @param string $str
* @return int|false
*/
public function strlen($str)
{
return strlen($str);
}
/**
* Returns the portion of string specified by the start and length parameters
*
* @param string $str
* @param int $offset
* @param int|null $length
* @return string|false
*/
public function substr($str, $offset = 0, $length = null)
{
return substr($str, $offset, $length);
}
/**
* Find the position of the first occurrence of a substring in a string
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @return int|false
*/
public function strpos($haystack, $needle, $offset = 0)
{
return strpos($haystack, $needle, $offset);
}
}
src/StringWrapper/StringWrapperInterface.php 0000666 00000005757 13605043111 0015317 0 ustar 00