Finished add non array_* functions

This commit is contained in:
R. Eric Wheeler 2016-07-21 14:56:56 -07:00
parent 57310f3d26
commit 150ed44033
7 changed files with 1394 additions and 405 deletions

View File

@ -15,4 +15,9 @@ matrix:
before_script: before_script:
- composer install --dev - composer install --dev
script: phpunit --coverage-text script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c phpunit.xml.dist
after_success:
- travis_retry php vendor/bin/coveralls -v

View File

@ -1,4 +1,5 @@
# ArrayObjectArray [![Build Status](https://travis-ci.org/sikofitt/array-object-array.svg?branch=master)](https://travis-ci.org/sikofitt/array-object-array) # ArrayObjectArray [![Build Status](https://travis-ci.org/sikofitt/array-object-array.svg?branch=master)](https://travis-ci.org/sikofitt/array-object-array) [![Coverage Status](https://coveralls.io/repos/github/sikofitt/array-object-array/badge.svg?branch=master)](https://coveralls.io/github/sikofitt/array-object-array?branch=master)
This is an incredibly simple class extension of `\ArrayObject` to provide This is an incredibly simple class extension of `\ArrayObject` to provide
array functions use the magic `__call` method. I no longer like writing this array functions use the magic `__call` method. I no longer like writing this

View File

@ -1,22 +1,26 @@
{ {
"name": "sikofitt/array-object-array", "name": "sikofitt/array-object-array",
"description": "ArrayObject class that implements array functions through __call", "description": "ArrayObject class that implements array functions through __call",
"keywords": ["array", "object", "php", "magic", "call"],
"type": "library", "type": "library",
"require": { "require": {
"php": ">=5.3.29", "php": ">=5.3",
"symfony/polyfill-php54": "^1.2", "symfony/polyfill-php54": "^1.2",
"symfony/polyfill-php55": "^1.2", "symfony/polyfill-php55": "^1.2",
"symfony/polyfill-php56": "^1.2" "symfony/polyfill-php56": "^1.2"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "*", "phpunit/phpunit": "~4.8",
"phpunit/php-code-coverage": "*" "phpunit/php-code-coverage": "~2.2",
"satooshi/php-coveralls": "^1.0"
}, },
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
"name": "sikofitt", "name": "sikofitt",
"email": "sikofitt@gmail.com" "email": "sikofitt@gmail.com",
"homepage": "http://rewiv.com",
"role": "developer"
} }
], ],
"autoload": { "autoload": {

536
composer.lock generated
View File

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "5da3f055fabd5e44bb25a547d0aea8de", "hash": "e783c69d217d2ee4cd8d84375554030d",
"content-hash": "623968123c52d5ef08b66b52671f3c04", "content-hash": "a03a3e83bc124fad42d5a75c770dc38a",
"packages": [ "packages": [
{ {
"name": "ircmaxell/password-compat", "name": "ircmaxell/password-compat",
@ -327,6 +327,102 @@
], ],
"time": "2015-06-14 21:17:01" "time": "2015-06-14 21:17:01"
}, },
{
"name": "guzzle/guzzle",
"version": "v3.9.3",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle3.git",
"reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle3/zipball/0645b70d953bc1c067bbc8d5bc53194706b628d9",
"reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9",
"shasum": ""
},
"require": {
"ext-curl": "*",
"php": ">=5.3.3",
"symfony/event-dispatcher": "~2.1"
},
"replace": {
"guzzle/batch": "self.version",
"guzzle/cache": "self.version",
"guzzle/common": "self.version",
"guzzle/http": "self.version",
"guzzle/inflection": "self.version",
"guzzle/iterator": "self.version",
"guzzle/log": "self.version",
"guzzle/parser": "self.version",
"guzzle/plugin": "self.version",
"guzzle/plugin-async": "self.version",
"guzzle/plugin-backoff": "self.version",
"guzzle/plugin-cache": "self.version",
"guzzle/plugin-cookie": "self.version",
"guzzle/plugin-curlauth": "self.version",
"guzzle/plugin-error-response": "self.version",
"guzzle/plugin-history": "self.version",
"guzzle/plugin-log": "self.version",
"guzzle/plugin-md5": "self.version",
"guzzle/plugin-mock": "self.version",
"guzzle/plugin-oauth": "self.version",
"guzzle/service": "self.version",
"guzzle/stream": "self.version"
},
"require-dev": {
"doctrine/cache": "~1.3",
"monolog/monolog": "~1.0",
"phpunit/phpunit": "3.7.*",
"psr/log": "~1.0",
"symfony/class-loader": "~2.1",
"zendframework/zend-cache": "2.*,<2.3",
"zendframework/zend-log": "2.*,<2.3"
},
"suggest": {
"guzzlehttp/guzzle": "Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.9-dev"
}
},
"autoload": {
"psr-0": {
"Guzzle": "src/",
"Guzzle\\Tests": "tests/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Guzzle Community",
"homepage": "https://github.com/guzzle/guzzle/contributors"
}
],
"description": "PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"rest",
"web service"
],
"abandoned": "guzzlehttp/guzzle",
"time": "2015-03-18 18:23:50"
},
{ {
"name": "phpdocumentor/reflection-docblock", "name": "phpdocumentor/reflection-docblock",
"version": "2.0.4", "version": "2.0.4",
@ -683,16 +779,16 @@
}, },
{ {
"name": "phpunit/phpunit", "name": "phpunit/phpunit",
"version": "4.8.26", "version": "4.8.27",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "fc1d8cd5b5de11625979125c5639347896ac2c74" "reference": "c062dddcb68e44b563f66ee319ddae2b5a322a90"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/fc1d8cd5b5de11625979125c5639347896ac2c74", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c062dddcb68e44b563f66ee319ddae2b5a322a90",
"reference": "fc1d8cd5b5de11625979125c5639347896ac2c74", "reference": "c062dddcb68e44b563f66ee319ddae2b5a322a90",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -751,7 +847,7 @@
"testing", "testing",
"xunit" "xunit"
], ],
"time": "2016-05-17 03:09:28" "time": "2016-07-21 06:48:14"
}, },
{ {
"name": "phpunit/phpunit-mock-objects", "name": "phpunit/phpunit-mock-objects",
@ -809,6 +905,102 @@
], ],
"time": "2015-10-02 06:51:40" "time": "2015-10-02 06:51:40"
}, },
{
"name": "psr/log",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "fe0936ee26643249e916849d48e3a51d5f5e278b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b",
"reference": "fe0936ee26643249e916849d48e3a51d5f5e278b",
"shasum": ""
},
"type": "library",
"autoload": {
"psr-0": {
"Psr\\Log\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"keywords": [
"log",
"psr",
"psr-3"
],
"time": "2012-12-21 11:40:51"
},
{
"name": "satooshi/php-coveralls",
"version": "v1.0.1",
"source": {
"type": "git",
"url": "https://github.com/satooshi/php-coveralls.git",
"reference": "da51d304fe8622bf9a6da39a8446e7afd432115c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/satooshi/php-coveralls/zipball/da51d304fe8622bf9a6da39a8446e7afd432115c",
"reference": "da51d304fe8622bf9a6da39a8446e7afd432115c",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-simplexml": "*",
"guzzle/guzzle": "^2.8|^3.0",
"php": ">=5.3.3",
"psr/log": "^1.0",
"symfony/config": "^2.1|^3.0",
"symfony/console": "^2.1|^3.0",
"symfony/stopwatch": "^2.0|^3.0",
"symfony/yaml": "^2.0|^3.0"
},
"suggest": {
"symfony/http-kernel": "Allows Symfony integration"
},
"bin": [
"bin/coveralls"
],
"type": "library",
"autoload": {
"psr-4": {
"Satooshi\\": "src/Satooshi/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Kitamura Satoshi",
"email": "with.no.parachute@gmail.com",
"homepage": "https://www.facebook.com/satooshi.jp"
}
],
"description": "PHP client library for Coveralls API",
"homepage": "https://github.com/satooshi/php-coveralls",
"keywords": [
"ci",
"coverage",
"github",
"test"
],
"time": "2016-01-20 17:35:46"
},
{ {
"name": "sebastian/comparator", "name": "sebastian/comparator",
"version": "1.2.0", "version": "1.2.0",
@ -1181,6 +1373,336 @@
"homepage": "https://github.com/sebastianbergmann/version", "homepage": "https://github.com/sebastianbergmann/version",
"time": "2015-06-21 13:59:46" "time": "2015-06-21 13:59:46"
}, },
{
"name": "symfony/config",
"version": "v2.8.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/config.git",
"reference": "0926e69411eba491803dbafb9f1f233e2ced58d0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/config/zipball/0926e69411eba491803dbafb9f1f233e2ced58d0",
"reference": "0926e69411eba491803dbafb9f1f233e2ced58d0",
"shasum": ""
},
"require": {
"php": ">=5.3.9",
"symfony/filesystem": "~2.3|~3.0.0"
},
"suggest": {
"symfony/yaml": "To use the yaml reference dumper"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Config\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Config Component",
"homepage": "https://symfony.com",
"time": "2016-06-29 05:31:50"
},
{
"name": "symfony/console",
"version": "v2.8.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "c392a6ec72f2122748032c2ad6870420561ffcfa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/c392a6ec72f2122748032c2ad6870420561ffcfa",
"reference": "c392a6ec72f2122748032c2ad6870420561ffcfa",
"shasum": ""
},
"require": {
"php": ">=5.3.9",
"symfony/polyfill-mbstring": "~1.0"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/event-dispatcher": "~2.1|~3.0.0",
"symfony/process": "~2.1|~3.0.0"
},
"suggest": {
"psr/log": "For using the console logger",
"symfony/event-dispatcher": "",
"symfony/process": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Console\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
"time": "2016-06-29 07:02:14"
},
{
"name": "symfony/event-dispatcher",
"version": "v2.8.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/event-dispatcher.git",
"reference": "b180b70439dca70049b6b9b7e21d75e6e5d7aca9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/b180b70439dca70049b6b9b7e21d75e6e5d7aca9",
"reference": "b180b70439dca70049b6b9b7e21d75e6e5d7aca9",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/config": "~2.0,>=2.0.5|~3.0.0",
"symfony/dependency-injection": "~2.6|~3.0.0",
"symfony/expression-language": "~2.6|~3.0.0",
"symfony/stopwatch": "~2.3|~3.0.0"
},
"suggest": {
"symfony/dependency-injection": "",
"symfony/http-kernel": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\EventDispatcher\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony EventDispatcher Component",
"homepage": "https://symfony.com",
"time": "2016-06-29 05:29:29"
},
{
"name": "symfony/filesystem",
"version": "v2.8.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/filesystem.git",
"reference": "7258ddd6f987053f21fa43d03430580ba54e6096"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/filesystem/zipball/7258ddd6f987053f21fa43d03430580ba54e6096",
"reference": "7258ddd6f987053f21fa43d03430580ba54e6096",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Filesystem\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Filesystem Component",
"homepage": "https://symfony.com",
"time": "2016-06-29 05:31:50"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.2.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "dff51f72b0706335131b00a7f49606168c582594"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594",
"reference": "dff51f72b0706335131b00a7f49606168c582594",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"time": "2016-05-18 14:26:46"
},
{
"name": "symfony/stopwatch",
"version": "v2.8.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/stopwatch.git",
"reference": "35bae476693150728b0eb51647faac82faf9aaca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/stopwatch/zipball/35bae476693150728b0eb51647faac82faf9aaca",
"reference": "35bae476693150728b0eb51647faac82faf9aaca",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Stopwatch\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Stopwatch Component",
"homepage": "https://symfony.com",
"time": "2016-06-29 05:29:29"
},
{ {
"name": "symfony/yaml", "name": "symfony/yaml",
"version": "v2.8.8", "version": "v2.8.8",

View File

@ -14,6 +14,9 @@
verbose="true" verbose="true"
debug="true" debug="true"
> >
<logging>
<log type="coverage-clover" target="build/logs/clover.xml" />
</logging>
<testsuites> <testsuites>
<testsuite name="ArrayObjectArray tests"> <testsuite name="ArrayObjectArray tests">
<directory>./tests/Sikofitt</directory> <directory>./tests/Sikofitt</directory>

View File

@ -19,8 +19,133 @@ namespace Sikofitt\Utility;
* *
* @package Sikofitt\Utility * @package Sikofitt\Utility
*/ */
class ArrayObjectArray extends \ArrayObject use Guzzle\Common\Exception\BadMethodCallException;
/**
* Class ArrayObjectArray
*
* @package Sikofitt\Utility
*/
class ArrayObjectArray extends \ArrayObject implements \IteratorAggregate
{ {
/**
* @var \ArrayIterator
*/
private $iterator;
/**
* ArrayObjectArray constructor.
*
* @param null $input
* @param int $flags
* @param string $iterator_class
*/
public function __construct(
$input = null,
$flags = 0,
$iterator_class = 'ArrayIterator'
) {
// SORT_FLAG_CASE and SORT_NATURAL were introduced in php 5.4
if (!defined('SORT_FLAG_CASE')) { define('SORT_FLAG_CASE', 8); } elseif(!defined('SORT_NATURAL')) { define('SORT_NATURAL', 6); }
// ARRAY_FILTER_USE_* flags were added in php 5.6
// along with a 3rd parameter to use them.
if (!defined('ARRAY_FILTER_USE_BOTH')) { define('ARRAY_FILTER_USE_BOTH', 1); } elseif (!defined('ARRAY_FILTER_USE_KEY')) { define('ARRAY_FILTER_USE_KEY', 2); }
parent::__construct($input, $flags, $iterator_class);
$this->iterator = $this->getIterator();
}
/**
* @param string $iterator_class
*/
public function setIteratorClass($iterator_class)
{
parent::setIteratorClass($iterator_class);
$this->iterator = $this->getIterator();
}
/**
*
*/
public function pos()
{
return pos($this->iterator);
}
/**
* @return mixed
*/
public function key()
{
return key($this->iterator);
}
/**
* @return mixed
*/
public function current()
{
return current($this->iterator);
}
/**
*
*/
public function next()
{
next($this->iterator);
}
/**
*
*/
public function reset()
{
reset($this->iterator);
}
/**
* @return bool
*/
public function valid()
{
return $this->iterator->valid();
}
/**
*
*/
public function prev()
{
prev($this->iterator);
}
/**
*
*/
public function end()
{
end($this->iterator);
}
public function each()
{
static $arrayCopy;
if(!$arrayCopy) {
$arrayCopy = $this->getArrayCopy();
}
return each($arrayCopy);
}
/**
* @param $needle
* @param bool $strict
*
* @return bool
*/
public function in_array($needle, $strict = false)
{
return in_array($needle, $this->getArrayCopy(), $strict);
}
/** /**
* @param $function * @param $function
* @param $argv * @param $argv
@ -29,11 +154,14 @@ class ArrayObjectArray extends \ArrayObject
*/ */
public function __call($function, $argv) public function __call($function, $argv)
{ {
if (0 !== strcasecmp($function, 'key_exists')) {
if (!is_callable($function) || substr($function, 0, 6) !== 'array_') { if (!is_callable($function) || substr($function, 0, 6) !== 'array_') {
throw new \BadMethodCallException(__CLASS__ . '->' . $function); throw new \BadMethodCallException(__CLASS__ . '->' . $function);
} }
}
switch ($function) { switch ($function) {
case 'array_key_exists': case 'array_key_exists':
case 'key_exists':
return call_user_func( return call_user_func(
$function, $function,
$argv[0], $argv[0],
@ -74,4 +202,108 @@ class ArrayObjectArray extends \ArrayObject
{ {
return \array_fill($startIndex, $num, $value); return \array_fill($startIndex, $num, $value);
} }
/**
* Alias of count
*
* @return int
*/
public function sizeof()
{
return $this->count();
}
/**
* @param null|int $sort_flags
*
* @return bool
*/
public function krsort($sort_flags = null)
{
$arrayCopy = $this->getArrayCopy();
if (@\krsort($arrayCopy, $sort_flags)) {
$this->exchangeArray($arrayCopy);
return true;
} else {
return false;
}
}
/**
* @param null|int $sort_flags
*
* @return bool
*/
public function rsort($sort_flags = null)
{
$arrayCopy = $this->getArrayCopy();
if(@\rsort($arrayCopy, $sort_flags))
{
$this->exchangeArray($arrayCopy);
return true;
} else {
return false;
}
}
/**
* @param null|int $sort_flags
*
* @return bool
*/
public function sort($sort_flags = null)
{
$arrayCopy = $this->getArrayCopy();
if(@\sort($arrayCopy, $sort_flags))
{
$this->exchangeArray($arrayCopy);
return true;
} else {
return false;
}
}
public function usort($callback)
{
if(!is_callable($callback))
{
throw new \BadFunctionCallException($callback);
}
$arrayCopy = $this->getArrayCopy();
if(true === $returnVal = @\usort($arrayCopy, $callback))
{
$this->exchangeArray($arrayCopy);
}
return $returnVal;
}
/**
* @param int $sort_flags
*
* @return bool
*/
public function arsort($sort_flags = SORT_REGULAR)
{
$arrayCopy = $this->getArrayCopy();
if (@\arsort($arrayCopy, $sort_flags)) {
$this->exchangeArray($arrayCopy);
return true;
} else {
return false;
}
}
/**
* @return bool
*/
public function shuffle()
{
$arrayCopy = $this->getArrayCopy();
if (true === $returnVal = shuffle($arrayCopy)) {
$this->exchangeArray($arrayCopy);
}
return $returnVal;
}
} }

View File

@ -1,6 +1,6 @@
<?php <?php
/* /*
* This file is part of ArrayObjectArray. * This file is part of ArrayObjectArray.
* *
* (copyleft) R. Eric Wheeler <sikofitt@gmail.com> * (copyleft) R. Eric Wheeler <sikofitt@gmail.com>
@ -9,15 +9,14 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Sikofitt\Tests; namespace Sikofitt\Tests;
use SebastianBergmann\CodeCoverage\CodeCoverage; use Sikofitt\Utility\ArrayObjectArray;
use SebastianBergmann\CodeCoverage\Driver\Xdebug;
use Sikofitt\Utility\ArrayObjectArray;
/** /**
* Class ArrayObjectArrayTest * Class ArrayObjectArrayTest
* *
* @covers \Sikofitt\Utility\ArrayObjectArray
* @package Sikofitt\Tests * @package Sikofitt\Tests
* *
* @test * @test
@ -26,107 +25,85 @@ use Sikofitt\Utility\ArrayObjectArray;
* array_chunk Split an array into chunks * array_chunk Split an array into chunks
* array_column Return the values from a single column in the * array_column Return the values from a single column in the
* input array * input array
* array_combine Creates an array by using one array for keys and * array_combine Creates an array by using one array for keys
* another for its values * and another for its values array_count_values Counts all the
* array_count_values Counts all the values of an array * values of an array array_diff_assoc Computes the difference of
* array_diff_assoc Computes the difference of arrays with additional * arrays with additional index check array_diff_key Computes
* index check * the difference of arrays using keys for comparison array_diff_uassoc
* array_diff_key Computes the difference of arrays using keys for * Computes the difference of arrays with additional index check which
* comparison * is performed by a user supplied callback function. array_diff_ukey
* array_diff_uassoc Computes the difference of arrays with additional * Computes the difference of arrays using a callback function
* index check which is performed by a user supplied * on the keys for comparison array_diff Computes
* callback function. * the difference of arrays array_fill_keys Fill an
* array_diff_ukey Computes the difference of arrays using a * array with values, specifying keys array_fill Fill an array with values array_filter Filters elements of an array using a callback function array_flip Exchanges all keys with their associated values in an array array_intersect_assoc Computes the intersection of arrays with additional index check array_intersect_key Computes the intersection of arrays using keys for comparison array_intersect_uassoc Computes the intersection of arrays with additional index check, compares indexes by a callback function array_intersect_ukey Computes the intersection of arrays using a callback function on the keys for comparison array_intersect Computes the intersection of arrays array_key_exists Checks if the given key or index exists in the array array_keys Return all the keys or a subset of the keys of an array array_map Applies the callback to the elements of the given arrays array_merge_recursive Merge two or more arrays recursively array_merge Merge one or more arrays array_multisort Sort multiple or multi-dimensional arrays array_pad Pad array to the specified length with a value array_pop Pop the element off the end of array array_product Calculate the product of values in an array array_push Push one or more elements onto the end of array array_rand Pick one or more random entries out of an array array_reduce Iteratively reduce the array to a single value using a callback function array_replace_recursive Replaces elements from passed arrays into the first array recursively array_replace Replaces elements from passed arrays into the first array array_reverse Return an array with elements in reverse order array_search Searches the array for a given value and returns the corresponding key if successful array_shift Shift an element off the beginning of array array_slice Extract a slice of the array array_splice Remove a portion of the array and replace it with something else array_sum Calculate the sum of values in an array array_udiff_assoc Computes the difference of arrays with additional index check, compares data by a callback function array_udiff_uassoc Computes the difference of arrays with additional index check, compares data and indexes by a callback function array_udiff Computes the difference of arrays by using a callback function for data comparison a rray_uintersect_assoc Computes the intersection of arrays with additional index check, compares data by a callback function array_uintersect_uassoc Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions array_uintersect Computes the intersection of arrays, compares data by a callback function array_unique Removes duplicate values from an array array_unshift Prepend one or more elements to the beginning of an array array_values Return all the values of an array array_walk_recursive Apply a user function recursively to every member of an array array_walk Apply a user supplied function to every member of an array
* callback
* function on the keys for comparison
* array_diff Computes the difference of arrays
* array_fill_keys Fill an array with values, specifying keys
* array_fill Fill an array with values
* array_filter Filters elements of an array using a callback
* function
* array_flip Exchanges all keys with their associated values
* in
* an array
* array_intersect_assoc Computes the intersection of arrays with
* additional index check
* array_intersect_key Computes the intersection of arrays using keys
* for
* comparison
* array_intersect_uassoc Computes the intersection of arrays with
* additional index check, compares indexes by a
* callback function
* array_intersect_ukey Computes the intersection of arrays using a
* callback function on the keys for comparison
* array_intersect Computes the intersection of arrays
* array_key_exists Checks if the given key or index exists in the
* array
* array_keys Return all the keys or a subset of the keys of an
* array
* array_map Applies the callback to the elements of the given
* arrays
* array_merge_recursive Merge two or more arrays recursively
* array_merge Merge one or more arrays
* array_multisort Sort multiple or multi
* -
* dimensional
* arrays
* array_pad
*
* Pad
* array
* to
* the
* specified length with a value array_pop Pop the element off the end of array array_product Calculate the product of values in an array array_push Push one or more elements onto the end of array array_rand Pick one or more random entries out of an array array_reduce Iteratively reduce the array to a single value using a callback function array_replace_recursive Replaces elements from passed arrays into the first array recursively array_replace Replaces elements from passed arrays into the first array array_reverse Return an array with elements in reverse order array_search Searches the array for a given value and returns the corresponding key if successful array_shift Shift an element off the beginning of array array_slice Extract a slice of the array array_splice Remove a portion of the array and replace it with something else array_sum Calculate the sum of values in an array array_udiff_assoc Computes the difference of arrays with additional index check, compares data by a callback function array_udiff_uassoc Computes the difference of arrays with additional index check, compares data and indexes by a callback function array_udiff Computes the difference of arrays by using a callback function for data comparison array_uintersect_assoc Computes the intersection of arrays with additional index check, compares data by a callback function array_uintersect_uassoc Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions array_uintersect Computes the intersection of arrays, compares data by a callback function array_unique Removes duplicate values from an array array_unshift Prepend one or more elements to the beginning of an array array_values Return all the values of an array array_walk_recursive Apply a user function recursively to every member of an array array_walk Apply a user supplied function to every member of an array
* *
* Non array_* functions - * Non array_* prefixed functions
*
* arsort Sort an array in reverse order and maintain index
* association
* asort Sort an array and maintain index association
* compact Create array containing variables and their
* values
* count Count all elements in an array, or something in
* an
* object
* sizeof Alias of count
* current Return the current element in an array
* each Return the current key and value pair from an
* array and advance the array cursor end Set the
* internal pointer of an array to its last element extract
* Import variables into the current symbol table from an array in_array
* Checks if a value exists in an array key_exists
* Alias of array_key_exists key Fetch a key
* from an array krsort Sort an array by key in
* reverse order ksort Sort an array by key list
* Assign variables as if they were an array
* natcasesort Sort an array using a
* case insensitive "natural order" algorithm natsort Sort an array using a "natural order" algorithm next Advance the internal array pointer of an array pos Alias of current prev Rewind the internal array pointer range Create an array containing a range of elements reset Set the internal pointer of an array to its first element rsort Sort an array in reverse order shuffle Shuffle an array sort Sort an array uasort Sort an array with a user - defined comparison function and maintain index association uksort Sort an array by keys using a user - defined comparison function usort Sort an array by
* *
* arsort Sort an array in reverse order and maintain
* index association current Return the current element
* in an array
** each Return the current key and value pair from an array and advance the array cursor
* end Set the internal pointer of an array to its
* last element in_array Checks if a value exists in an
* array key_exists Alias of array_key_exists key
* Fetch a key from an array krsort Sort an
* array by key in reverse order next Advance
* the internal array pointer of an array pos
* Alias of current prev Rewind the
* internal array pointer reset Set the
* internal pointer of an array to its first element rsort
* Sort an array in reverse order shuffle
* Shuffle an array sizeof Alias of count sort Sort an array usort Sort an array by values using a user-defined comparison function
*/ */
class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
/** {
* Class ArrayObjectArrayTest /**
* @coversDefaultClass Sikofitt\Utility\ArrayObjectArray * @var ArrayObjectArray
* @package Sikofitt\Tests * default ArrayObjectArray class to work with
*/ */
class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
{
private $arrayObjectArray; private $arrayObjectArray;
/**
* @var array
* default Array to work with
*/
private $workingArray; private $workingArray;
/**
* @var array
* multi-dimensional array to work with
*/
private $workingMultiArray; private $workingMultiArray;
/**
* @var ArrayObjectArray
* multi-dimensional ArrayObjectArray to work with
*/
private $arrayObjectArrayMulti; private $arrayObjectArrayMulti;
/**
* @var array
* Single array to work with
*/
private $singleArrayOne; private $singleArrayOne;
/**
* @var array
* Second single array to work with
*/
private $singleArrayTwo; private $singleArrayTwo;
/**
* @var ArrayObjectArray
* Single ArrayObjectArray class to work with
*/
private $arrayObjectSingle; private $arrayObjectSingle;
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
public function setUp() public function setUp()
{ {
$this->workingArray = array( $this->workingArray = array(
'this' => 'THAT', 'this' => 'THAT',
'that' => 'this', 'that' => 'this',
@ -225,15 +202,20 @@ class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
* @covers Sikofitt\Utility\ArrayObjectArray::__call * @covers Sikofitt\Utility\ArrayObjectArray::__call
* @covers Sikofitt\Utility\ArrayObjectArray::array_fill * @covers Sikofitt\Utility\ArrayObjectArray::array_fill
*/ */
public function testArrayFunctions() { public function testArrayFunctions()
$key_comp_function = function ($a, $b) { {
if ($a === $b) { $key_comp_function = function ($a, $b)
{
if ($a === $b)
{
return 0; return 0;
} }
return ($a > $b) ? 1 : -1; return ($a > $b) ? 1 : -1;
}; };
$array_filter = function ($a) { $array_filter = function ($a)
{
return $a & 1; return $a & 1;
}; };
// array_change_key_case // array_change_key_case
@ -268,15 +250,11 @@ class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
// array_filter // array_filter
$this->assertSame(array_filter($this->workingMultiArray), $this->arrayObjectArrayMulti->array_filter()); $this->assertSame(array_filter($this->workingMultiArray), $this->arrayObjectArrayMulti->array_filter());
$this->assertSame(array_filter($this->workingMultiArray, $array_filter), $this->arrayObjectArrayMulti->array_filter($array_filter)); $this->assertSame(array_filter($this->workingMultiArray, $array_filter), $this->arrayObjectArrayMulti->array_filter($array_filter));
// The third parameter was added in php 5.6
if(!defined('ARRAY_FILTER_USE_BOTH')) {
define('ARRAY_FILTER_USE_BOTH', 1);
}
if(!defined('ARRAY_FILTER_USE_KEY')) {
define('ARRAY_FILTER_USE_KEY', 2);
}
if (PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION')) { // The third parameter for array_filter was added in php 5.6 and does
// not exist in hhvm
if (PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION'))
{
$this->assertSame(array_filter($this->workingMultiArray, $array_filter, ARRAY_FILTER_USE_BOTH), $this->arrayObjectArrayMulti->array_filter($array_filter, ARRAY_FILTER_USE_BOTH)); $this->assertSame(array_filter($this->workingMultiArray, $array_filter, ARRAY_FILTER_USE_BOTH), $this->arrayObjectArrayMulti->array_filter($array_filter, ARRAY_FILTER_USE_BOTH));
$this->assertSame(array_filter($this->workingMultiArray, $array_filter, ARRAY_FILTER_USE_KEY), $this->arrayObjectArrayMulti->array_filter($array_filter, ARRAY_FILTER_USE_KEY)); $this->assertSame(array_filter($this->workingMultiArray, $array_filter, ARRAY_FILTER_USE_KEY), $this->arrayObjectArrayMulti->array_filter($array_filter, ARRAY_FILTER_USE_KEY));
} }
@ -328,6 +306,8 @@ class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
) )
); );
// array_pad // array_pad
$this->assertSame(array_pad($this->workingArray, 6, 'hello'), $this->arrayObjectArray->array_pad(6, 'hello'));
// array_pop // array_pop
// array_product // array_product
// array_push // array_push
@ -355,31 +335,273 @@ class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @codeCoverageIgnore * @covers Sikofitt\Utility\ArrayObjectArray::arsort
*/
public function testArsort()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['numeric']);
arsort($data['numeric']);
$this->assertTrue($arrayObjectArray->arsort());
$this->assertSame($data['numeric'], $arrayObjectArray->getArrayCopy());
$data = null;
$data = $this->arrayObjectArrayData();
$arrayObjectArray->exchangeArray($data['numeric']);
arsort($data['numeric'], SORT_STRING);
$this->assertTrue($arrayObjectArray->arsort(SORT_STRING));
$this->assertSame($data['numeric'], $arrayObjectArray->getArrayCopy());
$data = null;
$data = $this->arrayObjectArrayData();
$arrayObjectArray->exchangeArray($data['alpha']);
arsort($data['alpha'], SORT_NUMERIC);
$this->assertTrue($arrayObjectArray->arsort(SORT_NUMERIC));
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$data = null;
$data = $this->arrayObjectArrayData();
$arrayObjectArray->exchangeArray($data['multi']);
arsort($data['multi'], SORT_FLAG_CASE | SORT_NATURAL);
$this->assertTrue($arrayObjectArray->arsort(SORT_FLAG_CASE | SORT_NATURAL));
$this->assertSame($data['multi'], $arrayObjectArray->getArrayCopy());
$arrayObjectArray->exchangeArray(array('string'));
$this->assertFalse($arrayObjectArray->arsort('string'));
}
public function arrayObjectArrayData()
{
return array(
'numeric' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0),
'alpha' => array(
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'zero',
),
'multi' => array(
'one' => 'two',
'two' => 'three',
'three' => 'four',
'four' => 'five',
'five' => 'six',
'six' => 'seven',
'seven' => 'eight',
'eight' => 'nine',
'nine' => 'zero',
),
);
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::setIteratorClass
*/
public function testSetIteratorClass()
{
$arrayObjectArray = new ArrayObjectArray(array());
$this->assertSame($arrayObjectArray->getIteratorClass(), 'ArrayIterator');
$arrayObjectArray->setIteratorClass('RecursiveArrayIterator');
$this->assertSame($arrayObjectArray->getIteratorClass(), 'RecursiveArrayIterator');
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::current
* @covers Sikofitt\Utility\ArrayObjectArray::reset
* @covers Sikofitt\Utility\ArrayObjectArray::next
* @covers Sikofitt\Utility\ArrayObjectArray::valid
* @covers Sikofitt\Utility\ArrayObjectArray::key
* @covers Sikofitt\Utility\ArrayObjectArray::prev
* @covers Sikofitt\Utility\ArrayObjectArray::pos
*/
public function testArrayIterator()
{
$data = null;
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['alpha']);
$arrayObjectArray->reset();
$this->assertSame('one', $arrayObjectArray->current());
$arrayObjectArray->next();
$this->assertSame('two', $arrayObjectArray->current());
$this->assertSame('two', $arrayObjectArray->pos());
$arrayObjectArray->prev();
$this->assertSame('one', $arrayObjectArray->pos());
$arrayObjectArray->next();
$this->assertSame(1, $arrayObjectArray->key());
$arrayObjectArray->prev();
$this->assertSame('one', $arrayObjectArray->current());
$this->assertSame('one', $arrayObjectArray->pos());
$this->assertSame(0, $arrayObjectArray->key());
$this->assertTrue($arrayObjectArray->valid());
$arrayObjectArray->end();
$this->assertSame('zero', $arrayObjectArray->pos());
}
/**
* @covers \Sikofitt\Utility\ArrayObjectArray::in_array
*/
public function testInArray()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['multi']);
$this->assertFalse($arrayObjectArray->in_array('one'));
$this->assertTrue($arrayObjectArray->in_array('zero'));
$this->assertSame(in_array('one', $data['multi'], true), $arrayObjectArray->in_array('one'));
$arrayObjectArray->exchangeArray($data['numeric']);
$this->assertFalse($arrayObjectArray->in_array('1', true, true));
$this->assertTrue($arrayObjectArray->in_array('1'));
$arrayObjectArray->sizeof();
}
/**
* @covers \Sikofitt\Utility\ArrayObjectArray::shuffle
*/
public function testShuffle()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['numeric']);
$this->assertSame($data['numeric'], $arrayObjectArray->getArrayCopy());
$arrayObjectArray->shuffle();
$this->assertNotSame($data['numeric'], $arrayObjectArray->getArrayCopy());
}
public function testKrsort()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$arrayObjectArray->krsort();
$this->assertNotSame($data['alpha'], $arrayObjectArray->getArrayCopy());
krsort($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$this->assertFalse($arrayObjectArray->krsort('string'));
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::rsort
*/
public function testRsort()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$arrayObjectArray->rsort();
$this->assertNotSame($data['alpha'], $arrayObjectArray->getArrayCopy());
rsort($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$this->assertFalse($arrayObjectArray->rsort('string'));
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::sort
*/
public function testSort()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$arrayObjectArray->sort();
$this->assertNotSame($data['alpha'], $arrayObjectArray->getArrayCopy());
sort($data['alpha']);
$this->assertSame($data['alpha'], $arrayObjectArray->getArrayCopy());
$this->assertFalse($arrayObjectArray->sort('string'));
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::usort
*/
public function testUsort()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['numeric']);
$this->assertSame($data['numeric'], $arrayObjectArray->getArrayCopy());
usort($data['numeric'], 'strcasecmp');
$this->assertTrue($arrayObjectArray->usort('in_array'));
$this->assertNotSame($data['numeric'], $arrayObjectArray->getArrayCopy());
$this->assertTrue($arrayObjectArray->usort('strcasecmp'));
$this->assertSame($data['numeric'], $arrayObjectArray->getArrayCopy());
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::usort
* @expectedException \BadFunctionCallException
*/
public function testUsortTypeError()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['numeric']);
$arrayObjectArray->usort('none');
}
/**
* @covers Sikofitt\Utility\ArrayObjectArray::each
*/
public function testEach()
{
$data = $this->arrayObjectArrayData();
$arrayObjectArray = new ArrayObjectArray($data['alpha']);
$defaultEach = function() use ($data) {
$returnArray = array();
while(list($key, $value) = each($data['alpha']))
{
$returnArray[] = sprintf("%s => %s", $key, $value);
}
return $returnArray;
};
$arrayObjectEach = function() use($arrayObjectArray) {
$returnArray = array();
while(list($key, $value) = $arrayObjectArray->each())
{
$returnArray[] = sprintf("%s => %s", $key, $value);
}
return $returnArray;
};
$this->assertSame($defaultEach(), $arrayObjectEach());
}
/**
* @covers \Sikofitt\Utility\ArrayObjectArray::__call
* array_map
*/ */
public function testArrayMap() public function testArrayMap()
{ {
// array_map // array_map
$cube = function ($n) { $cube = function ($n)
{
return ($n * $n * $n); return ($n * $n * $n);
}; };
$timesTwo = function ($value) { $timesTwo = function ($value)
{
return $value * 2; return $value * 2;
}; };
$showSpanish = function ($digit, $spanishNum) { $showSpanish = function ($digit, $spanishNum)
{
return ("The number $digit is called $spanishNum in Spanish"); return ("The number $digit is called $spanishNum in Spanish");
}; };
$mapSpanish = function ($digit, $spanishNum) { $mapSpanish = function ($digit, $spanishNum)
{
return (array($digit => $spanishNum)); return (array($digit => $spanishNum));
}; };
$callbackOne = function ($a) { $callbackOne = function ($a)
{
return array($a); return array($a);
}; };
$callbackTwo = function ($a, $b) { $callbackTwo = function ($a, $b)
{
return array($a, $b); return array($a, $b);
}; };
@ -410,4 +632,4 @@ class ArrayObjectArrayTest extends \PHPUnit_Framework_TestCase
{ {
$this->arrayObjectArray->error(); $this->arrayObjectArray->error();
} }
} }