From 33db35d8438365987543cbc1cc0e97fe1b3a5c0b Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 8 Oct 2020 19:03:29 -0300 Subject: [PATCH 1/3] Add ArrayObject::isAssoc --- src/core/ArrayObject.php | 1376 ++++++++++++++++---------------- tests/unit/ArrayObjectTest.php | 1176 +++++++++++++-------------- 2 files changed, 1287 insertions(+), 1265 deletions(-) diff --git a/src/core/ArrayObject.php b/src/core/ArrayObject.php index 19680f11..a879ec73 100644 --- a/src/core/ArrayObject.php +++ b/src/core/ArrayObject.php @@ -1,681 +1,695 @@ -array = $array; - } - - public function __toArray(): array - { - return $this->array; - } - - public function toArray(): array - { - return $this->array; - } - - public function isEmpty(): bool - { - return empty($this->array); - } - - public function count(): int - { - return count($this->array); - } - - /** - * @return mixed - */ - public function current() - { - return current($this->array); - } - - /** - * @return mixed - */ - public function key() - { - return key($this->array); - } - - public function valid(): bool - { - return array_key_exists($this->key(), $this->array); - } - - /** - * @return mixed - */ - public function rewind() - { - return reset($this->array); - } - - /** - * @return mixed - */ - public function next() - { - return next($this->array); - } - - /** - * @param mixed $key - * @return ArrayObject|StringObject - */ - public function get($key) - { - if (!$this->exists($key)) { - throw new ArrayKeyNotExists($key); - } - return static::detectType($this->array[$key]); - } - - /** - * @return mixed - */ - public function last() - { - $key = array_key_last($this->array); - if ($key === null) { - return null; - } - return $this->get($key); - } - - /** - * @return null|int|string - */ - public function firstKey() - { - return array_key_first($this->array); - } - - /** - * @return null|int|string - */ - public function lastKey() - { - return array_key_last($this->array); - } - - /** - * @return mixed - */ - public function first() - { - $key = array_key_first($this->array); - if ($key === null) { - return null; - } - return $this->get($key); - } - - /** - * @param mixed $key - * @param mixed $value - * @return $this - */ - public function set($key, $value): self - { - $this->array[$key] = $value; - return $this; - } - - /** - * @param mixed $key - * @return $this - */ - public function delete($key): self - { - unset($this->array[$key]); - return $this; - } - - /** - * @param mixed $value - * @return $this - */ - public function remove($value, bool $strict = true, bool $loop = false): self - { - do { - $key = $this->search($value, $strict); - if ($key === false) { - break; - } - unset($this->array[$key]); - } while ($loop); - - return $this; - } - - /** - * @return $this - */ - public function clear(): self - { - $this->array = []; - return $this; - } - - /** - * @param mixed $key - * @return null|mixed - */ - public function offsetGet($key) - { - if (!array_key_exists($key, $this->array)) { - return null; - } - return $this->array[$key]; - } - - /** - * @param mixed $key - * @param mixed $value - */ - public function offsetSet($key, $value): void - { - $this->array[$key] = $value; - } - - /** - * @param mixed $key - */ - public function offsetUnset($key): void - { - unset($this->array[$key]); - } - - /** - * @param mixed $key - * @return bool - */ - public function offsetExists($key) - { - return isset($this->array[$key]); - } - - /** - * @param mixed $key - */ - public function exists($key): bool - { - return array_key_exists($key, $this->array); - } - - /** - * @param mixed $value - */ - public function contains($value, bool $strict = true): bool - { - return in_array($value, $this->array, $strict); - } - - /** - * @param mixed $value - * @return mixed - */ - public function indexOf($value, bool $strict = true) - { - return $this->search($value, $strict); - } - - /** - * @param mixed $value - * @return mixed - */ - public function lastIndexOf($value, bool $strict = true) - { - $array = $this->array; - for (end($array); ($currentKey = key($array)) !== null; prev($array)) { - $currentValue = current($array); - if ($currentValue == $value) { - if ($strict && $currentValue !== $value) { - continue; - } - break; - } - } - return $currentKey; - } - - /** - * @param mixed $needle - * @return mixed - */ - public function search($needle, bool $strict = true) - { - return array_search($needle, $this->array, $strict); - } - - public function join(string $glue = ''): StringObject - { - return static::detectStringType(implode($glue, $this->array)); - } - - public function serialize(): StringObject - { - return static::detectStringType(serialize($this->array)); - } - - /** - * @param string $string - * @return $this - */ - public function unserialize($string): self - { - $this->array = (array) unserialize((string) $string); - return $this; - } - - /** - * @return float|int - */ - public function sum() - { - return array_sum($this->array); - } - - /** - * @return float|int - */ - public function product() - { - return array_product($this->array); - } - - /** - * @param mixed $value - * @return int - */ - public function push($value) - { - return $this->pushBack($value); - } - - /** - * @param mixed $value - * @return int - */ - public function pushFront($value) - { - return array_unshift($this->array, $value); - } - - public function append(...$values): ArrayObject - { - array_push($this->array, ...$values); - return $this; - } - - /** - * @param mixed $value - * @return int - */ - public function pushBack($value) - { - return array_push($this->array, $value); - } - - /** - * @param mixed $value - * @return $this - */ - public function insert(int $offset, $value): self - { - if (is_array($value) || is_object($value) || is_null($value)) { - $value = [$value]; - } - array_splice($this->array, $offset, 0, $value); - return $this; - } - - /** - * @return mixed - */ - public function pop() - { - return $this->popBack(); - } - - /** - * @return mixed - */ - public function popFront() - { - return array_shift($this->array); - } - - /** - * @return mixed - */ - public function popBack() - { - return array_pop($this->array); - } - - /** - * @param mixed $offset - * @param int $length - * @return static - */ - public function slice($offset, int $length = null, bool $preserve_keys = false): self - { - return new static(array_slice($this->array, ...func_get_args())); - } - - /** - * @return ArrayObject|mixed|StringObject - */ - public function randomGet() - { - return static::detectType($this->array[array_rand($this->array, 1)]); - } - - /** - * @return $this - */ - public function each(callable $fn): self - { - if (array_walk($this->array, $fn) === false) { - throw new RuntimeException('array_walk() failed'); - } - return $this; - } - - /** - * @param array $args - * @return static - */ - public function map(callable $fn, ...$args): self - { - return new static(array_map($fn, $this->array, ...$args)); - } - - /** - * @param null $initial - * @return mixed - */ - public function reduce(callable $fn, $initial = null) - { - return array_reduce($this->array, $fn, $initial); - } - - /** - * @param array $args - * @return static - */ - public function keys(...$args): self - { - return new static(array_keys($this->array, ...$args)); - } - - /** - * @return static - */ - public function values(): self - { - return new static(array_values($this->array)); - } - - /** - * @param mixed $column_key - * @param mixed $index - * @return static - */ - public function column($column_key, $index = null): self - { - return new static(array_column($this->array, $column_key, $index)); - } - - /** - * @return static - */ - public function unique(int $sort_flags = SORT_STRING): self - { - return new static(array_unique($this->array, $sort_flags)); - } - - /** - * @return static - */ - public function reverse(bool $preserve_keys = false): self - { - return new static(array_reverse($this->array, $preserve_keys)); - } - - /** - * @return static - */ - public function chunk(int $size, bool $preserve_keys = false): self - { - return new static(array_chunk($this->array, $size, $preserve_keys)); - } - - /** - * Swap keys and values in an array. - * @return static - */ - public function flip(): self - { - return new static(array_flip($this->array)); - } - - /** - * @return static - */ - public function filter(callable $fn, int $flag = 0): self - { - return new static(array_filter($this->array, $fn, $flag)); - } - - /** - * | Function name | Sorts by | Maintains key association | Order of sort | Related functions | - * | :---------------- | :------- | :-------------------------- | :-------------------------- | :---------------- | - * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | - * | asort() | value | yes | low to high | arsort() | - * | arsort() | value | yes | high to low | asort() | - * | krsort() | key | yes | high to low | ksort() | - * | ksort() | key | yes | low to high | asort() | - * | natcasesort() | value | yes | natural, case insensitive | natsort() | - * | natsort() | value | yes | natural | natcasesort() | - * | rsort() | value | no | high to low | sort() | - * | shuffle() | value | no | random | array_rand() | - * | sort() | value | no | low to high | rsort() | - * | uasort() | value | yes | user defined | uksort() | - * | uksort() | key | yes | user defined | uasort() | - * | usort() | value | no | user defined | uasort() | - */ - - /** - * @return $this - */ - public function asort(int $sort_flags = SORT_REGULAR): self - { - if (asort($this->array, $sort_flags) !== true) { - throw new RuntimeException('asort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function arsort(int $sort_flags = SORT_REGULAR): self - { - if (arsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('arsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function krsort(int $sort_flags = SORT_REGULAR): self - { - if (krsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('krsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function ksort(int $sort_flags = SORT_REGULAR): self - { - if (ksort($this->array, $sort_flags) !== true) { - throw new RuntimeException('ksort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function natcasesort(): self - { - if (natcasesort($this->array) !== true) { - throw new RuntimeException('natcasesort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function natsort(): self - { - if (natsort($this->array) !== true) { - throw new RuntimeException('natsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function rsort(int $sort_flags = SORT_REGULAR): self - { - if (rsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('rsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function shuffle(): self - { - if (shuffle($this->array) !== true) { - throw new RuntimeException('shuffle() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function sort(int $sort_flags = SORT_REGULAR): self - { - if (sort($this->array, $sort_flags) !== true) { - throw new RuntimeException('sort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function uasort(callable $value_compare_func): self - { - if (uasort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('uasort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function uksort(callable $value_compare_func): self - { - if (uksort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('uksort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function usort(callable $value_compare_func): self - { - if (usort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('usort() failed'); - } - return $this; - } - - /** - * @param mixed $value - * @return ArrayObject|mixed|StringObject - */ - protected static function detectType($value) - { - if (is_string($value)) { - return static::detectStringType($value); - } - if (is_array($value)) { - return static::detectArrayType($value); - } - return $value; - } - - protected static function detectStringType(string $value): StringObject - { - return new StringObject($value); - } - - /** - * @return static - */ - protected static function detectArrayType(array $value): self - { - return new static($value); - } -} +array = $array; + } + + public function __toArray(): array + { + return $this->array; + } + + public function toArray(): array + { + return $this->array; + } + + public function isEmpty(): bool + { + return empty($this->array); + } + + public function count(): int + { + return count($this->array); + } + + /** + * @return mixed + */ + public function current() + { + return current($this->array); + } + + /** + * @return mixed + */ + public function key() + { + return key($this->array); + } + + public function valid(): bool + { + return array_key_exists($this->key(), $this->array); + } + + /** + * @return mixed + */ + public function rewind() + { + return reset($this->array); + } + + /** + * @return mixed + */ + public function next() + { + return next($this->array); + } + + /** + * @param mixed $key + * @return ArrayObject|StringObject + */ + public function get($key) + { + if (!$this->exists($key)) { + throw new ArrayKeyNotExists($key); + } + return static::detectType($this->array[$key]); + } + + /** + * @return mixed + */ + public function last() + { + $key = array_key_last($this->array); + if ($key === null) { + return null; + } + return $this->get($key); + } + + /** + * @return null|int|string + */ + public function firstKey() + { + return array_key_first($this->array); + } + + /** + * @return null|int|string + */ + public function lastKey() + { + return array_key_last($this->array); + } + + /** + * @return mixed + */ + public function first() + { + $key = array_key_first($this->array); + if ($key === null) { + return null; + } + return $this->get($key); + } + + /** + * @param mixed $key + * @param mixed $value + * @return $this + */ + public function set($key, $value): self + { + $this->array[$key] = $value; + return $this; + } + + /** + * @param mixed $key + * @return $this + */ + public function delete($key): self + { + unset($this->array[$key]); + return $this; + } + + /** + * @param mixed $value + * @return $this + */ + public function remove($value, bool $strict = true, bool $loop = false): self + { + do { + $key = $this->search($value, $strict); + if ($key === false) { + break; + } + unset($this->array[$key]); + } while ($loop); + + return $this; + } + + /** + * @return $this + */ + public function clear(): self + { + $this->array = []; + return $this; + } + + /** + * @param mixed $key + * @return null|mixed + */ + public function offsetGet($key) + { + if (!array_key_exists($key, $this->array)) { + return null; + } + return $this->array[$key]; + } + + /** + * @param mixed $key + * @param mixed $value + */ + public function offsetSet($key, $value): void + { + $this->array[$key] = $value; + } + + /** + * @param mixed $key + */ + public function offsetUnset($key): void + { + unset($this->array[$key]); + } + + /** + * @param mixed $key + * @return bool + */ + public function offsetExists($key) + { + return isset($this->array[$key]); + } + + /** + * @param mixed $key + */ + public function exists($key): bool + { + return array_key_exists($key, $this->array); + } + + /** + * @param mixed $value + */ + public function contains($value, bool $strict = true): bool + { + return in_array($value, $this->array, $strict); + } + + /** + * @param mixed $value + * @return mixed + */ + public function indexOf($value, bool $strict = true) + { + return $this->search($value, $strict); + } + + /** + * @param mixed $value + * @return mixed + */ + public function lastIndexOf($value, bool $strict = true) + { + $array = $this->array; + for (end($array); ($currentKey = key($array)) !== null; prev($array)) { + $currentValue = current($array); + if ($currentValue == $value) { + if ($strict && $currentValue !== $value) { + continue; + } + break; + } + } + return $currentKey; + } + + /** + * @param mixed $needle + * @return mixed + */ + public function search($needle, bool $strict = true) + { + return array_search($needle, $this->array, $strict); + } + + public function join(string $glue = ''): StringObject + { + return static::detectStringType(implode($glue, $this->array)); + } + + public function serialize(): StringObject + { + return static::detectStringType(serialize($this->array)); + } + + /** + * @param string $string + * @return $this + */ + public function unserialize($string): self + { + $this->array = (array) unserialize((string) $string); + return $this; + } + + /** + * @return float|int + */ + public function sum() + { + return array_sum($this->array); + } + + /** + * @return float|int + */ + public function product() + { + return array_product($this->array); + } + + /** + * @param mixed $value + * @return int + */ + public function push($value) + { + return $this->pushBack($value); + } + + /** + * @param mixed $value + * @return int + */ + public function pushFront($value) + { + return array_unshift($this->array, $value); + } + + public function append(...$values): ArrayObject + { + array_push($this->array, ...$values); + return $this; + } + + /** + * @param mixed $value + * @return int + */ + public function pushBack($value) + { + return array_push($this->array, $value); + } + + /** + * @param mixed $value + * @return $this + */ + public function insert(int $offset, $value): self + { + if (is_array($value) || is_object($value) || is_null($value)) { + $value = [$value]; + } + array_splice($this->array, $offset, 0, $value); + return $this; + } + + /** + * @return mixed + */ + public function pop() + { + return $this->popBack(); + } + + /** + * @return mixed + */ + public function popFront() + { + return array_shift($this->array); + } + + /** + * @return mixed + */ + public function popBack() + { + return array_pop($this->array); + } + + /** + * @param mixed $offset + * @param int $length + * @return static + */ + public function slice($offset, int $length = null, bool $preserve_keys = false): self + { + return new static(array_slice($this->array, ...func_get_args())); + } + + /** + * @return ArrayObject|mixed|StringObject + */ + public function randomGet() + { + return static::detectType($this->array[array_rand($this->array, 1)]); + } + + /** + * @return $this + */ + public function each(callable $fn): self + { + if (array_walk($this->array, $fn) === false) { + throw new RuntimeException('array_walk() failed'); + } + return $this; + } + + /** + * @param array $args + * @return static + */ + public function map(callable $fn, ...$args): self + { + return new static(array_map($fn, $this->array, ...$args)); + } + + /** + * @param null $initial + * @return mixed + */ + public function reduce(callable $fn, $initial = null) + { + return array_reduce($this->array, $fn, $initial); + } + + /** + * @param array $args + * @return static + */ + public function keys(...$args): self + { + return new static(array_keys($this->array, ...$args)); + } + + /** + * @return static + */ + public function values(): self + { + return new static(array_values($this->array)); + } + + /** + * @param mixed $column_key + * @param mixed $index + * @return static + */ + public function column($column_key, $index = null): self + { + return new static(array_column($this->array, $column_key, $index)); + } + + /** + * @return static + */ + public function unique(int $sort_flags = SORT_STRING): self + { + return new static(array_unique($this->array, $sort_flags)); + } + + /** + * @return static + */ + public function reverse(bool $preserve_keys = false): self + { + return new static(array_reverse($this->array, $preserve_keys)); + } + + /** + * @return static + */ + public function chunk(int $size, bool $preserve_keys = false): self + { + return new static(array_chunk($this->array, $size, $preserve_keys)); + } + + /** + * Swap keys and values in an array. + * @return static + */ + public function flip(): self + { + return new static(array_flip($this->array)); + } + + /** + * @return static + */ + public function filter(callable $fn, int $flag = 0): self + { + return new static(array_filter($this->array, $fn, $flag)); + } + + /** + * | Function name | Sorts by | Maintains key association | Order of sort | Related functions | + * | :---------------- | :------- | :-------------------------- | :-------------------------- | :---------------- | + * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | + * | asort() | value | yes | low to high | arsort() | + * | arsort() | value | yes | high to low | asort() | + * | krsort() | key | yes | high to low | ksort() | + * | ksort() | key | yes | low to high | asort() | + * | natcasesort() | value | yes | natural, case insensitive | natsort() | + * | natsort() | value | yes | natural | natcasesort() | + * | rsort() | value | no | high to low | sort() | + * | shuffle() | value | no | random | array_rand() | + * | sort() | value | no | low to high | rsort() | + * | uasort() | value | yes | user defined | uksort() | + * | uksort() | key | yes | user defined | uasort() | + * | usort() | value | no | user defined | uasort() | + */ + + /** + * @return $this + */ + public function asort(int $sort_flags = SORT_REGULAR): self + { + if (asort($this->array, $sort_flags) !== true) { + throw new RuntimeException('asort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function arsort(int $sort_flags = SORT_REGULAR): self + { + if (arsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('arsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function krsort(int $sort_flags = SORT_REGULAR): self + { + if (krsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('krsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function ksort(int $sort_flags = SORT_REGULAR): self + { + if (ksort($this->array, $sort_flags) !== true) { + throw new RuntimeException('ksort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function natcasesort(): self + { + if (natcasesort($this->array) !== true) { + throw new RuntimeException('natcasesort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function natsort(): self + { + if (natsort($this->array) !== true) { + throw new RuntimeException('natsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function rsort(int $sort_flags = SORT_REGULAR): self + { + if (rsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('rsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function shuffle(): self + { + if (shuffle($this->array) !== true) { + throw new RuntimeException('shuffle() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function sort(int $sort_flags = SORT_REGULAR): self + { + if (sort($this->array, $sort_flags) !== true) { + throw new RuntimeException('sort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function uasort(callable $value_compare_func): self + { + if (uasort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('uasort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function uksort(callable $value_compare_func): self + { + if (uksort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('uksort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function usort(callable $value_compare_func): self + { + if (usort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('usort() failed'); + } + return $this; + } + + /** + * @return bool + */ + public function isAssoc(): bool + { + foreach (array_keys($this->array) as $key) { + if (is_string($key)) { + return true; + } + } + + return false; + } + + /** + * @param mixed $value + * @return ArrayObject|mixed|StringObject + */ + protected static function detectType($value) + { + if (is_string($value)) { + return static::detectStringType($value); + } + if (is_array($value)) { + return static::detectArrayType($value); + } + return $value; + } + + protected static function detectStringType(string $value): StringObject + { + return new StringObject($value); + } + + /** + * @return static + */ + protected static function detectArrayType(array $value): self + { + return new static($value); + } +} diff --git a/tests/unit/ArrayObjectTest.php b/tests/unit/ArrayObjectTest.php index 68a9bea6..dd1c4349 100644 --- a/tests/unit/ArrayObjectTest.php +++ b/tests/unit/ArrayObjectTest.php @@ -1,584 +1,592 @@ -data = swoole_string($_data)->split(',')->each(function (&$item) { - $item = intval($item); - }); - - $this->data_2 = swoole_array(['hello', 'world', 'swoole']); - $this->data_3 = swoole_array([ - 'hello' => 'world', - 'swoole' => 'php', - 'nihao' => '中国人', - ]); - $this->data_4 = swoole_array([ - 'd' => 'lemon', - 'a' => 'orange', - 'b' => 'banana', - 'c' => 'apple', - ]); - - $array = explode(',', $_data); - foreach ($array as &$v) { - $v = trim($v); - } - $this->control_data = $array; - parent::__construct($name, $data, $dataName); - } - - /** - * @covers \Swoole\ArrayObject::toArray() - */ - public function testToArray() - { - $this->assertEquals($this->data->toArray(), $this->control_data); - } - - /** - * @covers \Swoole\ArrayObject::each() - * @covers \Swoole\ArrayObject::sort() - * @covers \Swoole\ArrayObject::unique() - */ - public function testMix() - { - $datao = clone $this->data; - $data = $datao->sort()->unique()->toArray(); - - $copy_data = $this->control_data; - sort($copy_data); - $expectResult = array_unique($copy_data); - - $this->assertEquals($data, $expectResult); - } - - /** - * @covers \Swoole\ArrayObject::serialize() - */ - public function testSerialize() - { - $this->assertEquals(serialize($this->data->toArray()), $this->data->serialize()); - } - - /** - * @covers \Swoole\ArrayObject::unique() - */ - public function testUnique() - { - $data = $this->data->unique()->toArray(); - $copy_data = $this->control_data; - $expectResult = array_unique($copy_data); - - $this->assertEquals($data, $expectResult); - } - - public function testTraverse() - { - $newArray = []; - foreach ($this->data as $value) { - $newArray[] = $value; - } - $this->assertEquals($this->control_data, $newArray); - } - - public function testRemove() - { - $data1 = swoole_array_list('hello', 'world', 'swoole'); - $data2 = $data1->toArray(); - - $this->assertEquals( - $data1->remove('hello')->values()->toArray(), - array_values(array_slice($data2, 1)) - ); - } - - public function testFilter() - { - $data = $this->data->filter(function ($v) { - return $v > 20; - }); - - $find = false; - foreach ($data as $v) { - if ($v <= 20) { - $find = true; - break; - } - } - $this->assertFalse($find); - } - - public function testOffsetExists() - { - $this->assertEquals($this->data->offsetExists(9), isset($this->control_data[9])); - } - - public function testSearch() - { - $this->assertEquals($this->data_2->search('swoole'), 2); - } - - public function testValues() - { - $this->assertEquals( - $this->data_3->values()->toArray(), - array_values($this->data_3->toArray()) - ); - } - - public function testMap() - { - $arr1 = [1, 2, 3, 4, 5]; - $arr2 = [6, 7, 8, 9, 10]; - $arr3 = [62, 71, 82, 93, 103]; - - $this->assertEquals(swoole_array($arr1)->map(function ($val1) { - return $val1 * 3; - })->toArray(), [ - 3, - 6, - 9, - 12, - 15, - ]); - - $this->assertEquals(swoole_array($arr1)->map(function ($val1, $val2, $val3) { - return $val1 + $val2 + $val3; - }, $arr2, $arr3)->toArray(), [ - 69, - 80, - 93, - 106, - 118, - ]); - } - - public function testClear() - { - $this->assertEquals((clone $this->data->clear())->toArray(), []); - } - - public function testReverse() - { - $this->assertEquals($this->data->reverse()->toArray(), array_reverse($this->control_data)); - } - - public function testDelete() - { - $data = clone $this->data_3; - $expectData = $data->toArray(); - unset($expectData['swoole']); - - $this->assertEquals( - $data->delete('swoole')->toArray(), - $expectData - ); - } - - public function testContains() - { - $this->assertTrue($this->data_2->contains('swoole')); - $this->assertFalse($this->data_2->contains('aliyun')); - } - - public function testUnserialize() - { - $str = serialize($this->data->toArray()); - $this->assertEquals( - swoole_array([])->unserialize($str)->toArray(), - $this->data->toArray() - ); - } - - public function testPushBack() - { - $data = clone $this->data; - $data->pushBack(999); - $this->assertEquals($data->search(999), $data->count() - 1); - } - - public function testPushFront() - { - $data = clone $this->data; - $data->pushFront(999); - $this->assertEquals($data->search(999), 0); - } - - public function testPopFront() - { - $data = clone $this->data; - $value = $data->popFront(); - $this->assertEquals($value, $this->data->first()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPopBack() - { - $data = clone $this->data; - $value = $data->popBack(); - $this->assertEquals($value, $this->data->last()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPop() - { - $data = clone $this->data; - $value = $data->pop(); - $this->assertEquals($value, $this->data->last()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPush() - { - $data = clone $this->data; - $data->pushBack(999); - $this->assertEquals($data->search(999), $data->count() - 1); - } - - public function testAppend() - { - $data = clone $this->data; - $data->append(999)->append(888); - $data->append(10000, 20000, 30000); - $this->assertTrue($data->contains(999)); - $this->assertTrue($data->contains(888)); - $this->assertTrue($data->contains(30000)); - } - - public function testShuffle() - { - $data1 = clone $this->data; - $data2 = clone $this->data; - $data1->shuffle(); - $this->assertNotEquals($data1->values()->toArray(), $data2->values()->toArray()); - $this->assertEquals($data1->values()->sort()->toArray(), $data2->values()->sort()->toArray()); - } - - public function testColumn() - { - $records = [ - [ - 'id' => 2135, - 'first_name' => 'John', - 'last_name' => 'Doe', - ], - [ - 'id' => 3245, - 'first_name' => 'Sally', - 'last_name' => 'Smith', - ], - [ - 'id' => 5342, - 'first_name' => 'Jane', - 'last_name' => 'Jones', - ], - [ - 'id' => 5623, - 'first_name' => 'Peter', - 'last_name' => 'Doe', - ], - ]; - - $this->assertEquals( - array_column($records, 'first_name'), - swoole_array($records)->column('first_name')->toArray() - ); - - $this->assertEquals( - array_column($records, 'first_name', 'id'), - swoole_array($records)->column('first_name', 'id')->toArray() - ); - } - - public function testIndexOf() - { - $this->assertEquals($this->data->indexOf(23), 7); - } - - public function testProduct() - { - $value = 1; - foreach ($this->control_data as $v) { - $value *= $v; - } - $this->assertEquals($this->data->product(), $value); - } - - public function testAsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - asort($data1); - $this->assertEquals($data1, $data2->asort()->toArray()); - } - - public function testArsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - arsort($data1); - $this->assertEquals($data1, $data2->arsort()->toArray()); - } - - public function testKsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - ksort($data1); - $this->assertEquals($data1, $data2->ksort()->toArray()); - } - - public function testKrsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - krsort($data1); - $this->assertEquals($data1, $data2->krsort()->toArray()); - } - - public function testSort() - { - $data1 = $this->data->toArray(); - $data2 = clone $this->data; - sort($data1); - $this->assertEquals($data1, $data2->sort()->toArray()); - } - - public function testRsort() - { - $data1 = $this->data->toArray(); - $data2 = clone $this->data; - rsort($data1); - $this->assertEquals($data1, $data2->rsort()->toArray()); - } - - public function testUasort() - { - $data1 = ['a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4]; - $data2 = swoole_array($data1); - $cmp = function ($a, $b) { - if ($a == $b) { - return 0; - } - return ($a < $b) ? -1 : 1; - }; - uasort($data1, $cmp); - $this->assertEquals($data1, $data2->uasort($cmp)->toArray()); - } - - public function testNatsort() - { - $data1 = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; - $data2 = swoole_array($data1); - natsort($data1); - $this->assertEquals($data1, $data2->natsort()->toArray()); - } - - public function testNatcasesort() - { - $data1 = ['IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png']; - $data2 = swoole_array($data1); - natcasesort($data1); - $this->assertEquals($data1, $data2->natcasesort()->toArray()); - } - - public function testUsort() - { - $cmp = function ($a, $b) { - if ($a == $b) { - return 0; - } - return ($a < $b) ? -1 : 1; - }; - - $data1 = [3, 2, 5, 6, 1]; - $data2 = swoole_array($data1); - usort($data1, $cmp); - $this->assertEquals($data1, $data2->usort($cmp)->toArray()); - } - - public function testUksort() - { - $cmp = function ($a, $b) { - $a = preg_replace('@^(a|an|the) @', '', $a); - $b = preg_replace('@^(a|an|the) @', '', $b); - return strcasecmp($a, $b); - }; - - $data1 = ['John' => 1, 'the Earth' => 2, 'an apple' => 3, 'a banana' => 4]; - $data2 = swoole_array($data1); - uksort($data1, $cmp); - $this->assertEquals($data1, $data2->uksort($cmp)->toArray()); - } - - public function testCount() - { - $this->assertEquals($this->data->count(), count($this->control_data)); - } - - public function testOffsetGet() - { - $this->assertEquals($this->control_data[6], $this->data[6]); - } - - public function testReduce() - { - $this->assertEquals($this->data->product(), $this->data->reduce(function ($carry, $item) { - $carry *= $item; - return $carry; - }, 1)); - } - - public function testOffsetSet() - { - $value = 9999; - $data = clone $this->data; - $data[7] = $value; - $this->assertEquals($data->get(7), $value); - } - - public function testOffsetUnset() - { - $data = clone $this->data; - unset($data[6]); - $this->assertFalse($data->exists(6)); - } - - public function testIsEmpty() - { - $this->assertFalse($this->data->isEmpty()); - $this->assertTrue(swoole_array()->isEmpty()); - } - - public function testKeys() - { - $this->assertEquals( - $this->data_4->keys()->toArray(), - array_keys($this->data_4->toArray()) - ); - } - - public function testSet() - { - $data = clone $this->data_3; - $data->set('com', 'tal100'); - $this->assertEquals($data->get('com'), 'tal100'); - $this->assertEquals($data->count(), $this->data_3->count() + 1); - } - - public function testInsert() - { - $data = clone $this->data; - $data->insert(7, 888); - $this->assertEquals($data->get(7), 888); - } - - public function testRandomGet() - { - $this->assertTrue($this->data->contains($this->data->randomGet())); - } - - public function testSum() - { - $this->assertEquals($this->data->sum(), array_sum($this->control_data)); - } - - public function testFlip() - { - $result = $this->data_3->flip(); - $this->assertEquals($result->toArray(), array_flip($this->data_3->toArray())); - } - - public function testJoin() - { - $this->assertEquals($this->data_2->join('-'), implode('-', $this->data_2->toArray())); - } - - public function testChunk() - { - $this->assertEquals( - $this->data->chunk(2)->toArray(), - array_chunk($this->data->toArray(), 2) - ); - } - - public function testSlice() - { - $this->assertEquals( - $this->data->slice(2, 4)->toArray(), - array_slice($this->control_data, 2, 4) - ); - } - - public function testLastIndexOf() - { - $this->assertEquals($this->data->lastIndexOf(23), 9); - } - - public function testExists() - { - $this->assertTrue($this->data_3->exists('swoole')); - } - - public function testFirst() - { - $this->assertEquals($this->data_4->first(), 'lemon'); - } - - public function testLast() - { - $this->assertEquals($this->data_4->last(), 'apple'); - } - - public function testFirstKey() - { - $this->assertEquals($this->data_4->firstKey(), 'd'); - } - - public function testLastKey() - { - $this->assertEquals($this->data_4->lastKey(), 'c'); - } -} +data = swoole_string($_data)->split(',')->each(function (&$item) { + $item = intval($item); + }); + + $this->data_2 = swoole_array(['hello', 'world', 'swoole']); + $this->data_3 = swoole_array([ + 'hello' => 'world', + 'swoole' => 'php', + 'nihao' => '中国人', + ]); + $this->data_4 = swoole_array([ + 'd' => 'lemon', + 'a' => 'orange', + 'b' => 'banana', + 'c' => 'apple', + ]); + + $array = explode(',', $_data); + foreach ($array as &$v) { + $v = trim($v); + } + $this->control_data = $array; + parent::__construct($name, $data, $dataName); + } + + /** + * @covers \Swoole\ArrayObject::toArray() + */ + public function testToArray() + { + $this->assertEquals($this->data->toArray(), $this->control_data); + } + + /** + * @covers \Swoole\ArrayObject::each() + * @covers \Swoole\ArrayObject::sort() + * @covers \Swoole\ArrayObject::unique() + */ + public function testMix() + { + $datao = clone $this->data; + $data = $datao->sort()->unique()->toArray(); + + $copy_data = $this->control_data; + sort($copy_data); + $expectResult = array_unique($copy_data); + + $this->assertEquals($data, $expectResult); + } + + /** + * @covers \Swoole\ArrayObject::serialize() + */ + public function testSerialize() + { + $this->assertEquals(serialize($this->data->toArray()), $this->data->serialize()); + } + + /** + * @covers \Swoole\ArrayObject::unique() + */ + public function testUnique() + { + $data = $this->data->unique()->toArray(); + $copy_data = $this->control_data; + $expectResult = array_unique($copy_data); + + $this->assertEquals($data, $expectResult); + } + + public function testTraverse() + { + $newArray = []; + foreach ($this->data as $value) { + $newArray[] = $value; + } + $this->assertEquals($this->control_data, $newArray); + } + + public function testRemove() + { + $data1 = swoole_array_list('hello', 'world', 'swoole'); + $data2 = $data1->toArray(); + + $this->assertEquals( + $data1->remove('hello')->values()->toArray(), + array_values(array_slice($data2, 1)) + ); + } + + public function testFilter() + { + $data = $this->data->filter(function ($v) { + return $v > 20; + }); + + $find = false; + foreach ($data as $v) { + if ($v <= 20) { + $find = true; + break; + } + } + $this->assertFalse($find); + } + + public function testOffsetExists() + { + $this->assertEquals($this->data->offsetExists(9), isset($this->control_data[9])); + } + + public function testSearch() + { + $this->assertEquals($this->data_2->search('swoole'), 2); + } + + public function testValues() + { + $this->assertEquals( + $this->data_3->values()->toArray(), + array_values($this->data_3->toArray()) + ); + } + + public function testMap() + { + $arr1 = [1, 2, 3, 4, 5]; + $arr2 = [6, 7, 8, 9, 10]; + $arr3 = [62, 71, 82, 93, 103]; + + $this->assertEquals(swoole_array($arr1)->map(function ($val1) { + return $val1 * 3; + })->toArray(), [ + 3, + 6, + 9, + 12, + 15, + ]); + + $this->assertEquals(swoole_array($arr1)->map(function ($val1, $val2, $val3) { + return $val1 + $val2 + $val3; + }, $arr2, $arr3)->toArray(), [ + 69, + 80, + 93, + 106, + 118, + ]); + } + + public function testClear() + { + $this->assertEquals((clone $this->data->clear())->toArray(), []); + } + + public function testReverse() + { + $this->assertEquals($this->data->reverse()->toArray(), array_reverse($this->control_data)); + } + + public function testDelete() + { + $data = clone $this->data_3; + $expectData = $data->toArray(); + unset($expectData['swoole']); + + $this->assertEquals( + $data->delete('swoole')->toArray(), + $expectData + ); + } + + public function testContains() + { + $this->assertTrue($this->data_2->contains('swoole')); + $this->assertFalse($this->data_2->contains('aliyun')); + } + + public function testUnserialize() + { + $str = serialize($this->data->toArray()); + $this->assertEquals( + swoole_array([])->unserialize($str)->toArray(), + $this->data->toArray() + ); + } + + public function testPushBack() + { + $data = clone $this->data; + $data->pushBack(999); + $this->assertEquals($data->search(999), $data->count() - 1); + } + + public function testPushFront() + { + $data = clone $this->data; + $data->pushFront(999); + $this->assertEquals($data->search(999), 0); + } + + public function testPopFront() + { + $data = clone $this->data; + $value = $data->popFront(); + $this->assertEquals($value, $this->data->first()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPopBack() + { + $data = clone $this->data; + $value = $data->popBack(); + $this->assertEquals($value, $this->data->last()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPop() + { + $data = clone $this->data; + $value = $data->pop(); + $this->assertEquals($value, $this->data->last()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPush() + { + $data = clone $this->data; + $data->pushBack(999); + $this->assertEquals($data->search(999), $data->count() - 1); + } + + public function testAppend() + { + $data = clone $this->data; + $data->append(999)->append(888); + $data->append(10000, 20000, 30000); + $this->assertTrue($data->contains(999)); + $this->assertTrue($data->contains(888)); + $this->assertTrue($data->contains(30000)); + } + + public function testShuffle() + { + $data1 = clone $this->data; + $data2 = clone $this->data; + $data1->shuffle(); + $this->assertNotEquals($data1->values()->toArray(), $data2->values()->toArray()); + $this->assertEquals($data1->values()->sort()->toArray(), $data2->values()->sort()->toArray()); + } + + public function testColumn() + { + $records = [ + [ + 'id' => 2135, + 'first_name' => 'John', + 'last_name' => 'Doe', + ], + [ + 'id' => 3245, + 'first_name' => 'Sally', + 'last_name' => 'Smith', + ], + [ + 'id' => 5342, + 'first_name' => 'Jane', + 'last_name' => 'Jones', + ], + [ + 'id' => 5623, + 'first_name' => 'Peter', + 'last_name' => 'Doe', + ], + ]; + + $this->assertEquals( + array_column($records, 'first_name'), + swoole_array($records)->column('first_name')->toArray() + ); + + $this->assertEquals( + array_column($records, 'first_name', 'id'), + swoole_array($records)->column('first_name', 'id')->toArray() + ); + } + + public function testIndexOf() + { + $this->assertEquals($this->data->indexOf(23), 7); + } + + public function testProduct() + { + $value = 1; + foreach ($this->control_data as $v) { + $value *= $v; + } + $this->assertEquals($this->data->product(), $value); + } + + public function testAsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + asort($data1); + $this->assertEquals($data1, $data2->asort()->toArray()); + } + + public function testArsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + arsort($data1); + $this->assertEquals($data1, $data2->arsort()->toArray()); + } + + public function testKsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + ksort($data1); + $this->assertEquals($data1, $data2->ksort()->toArray()); + } + + public function testKrsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + krsort($data1); + $this->assertEquals($data1, $data2->krsort()->toArray()); + } + + public function testSort() + { + $data1 = $this->data->toArray(); + $data2 = clone $this->data; + sort($data1); + $this->assertEquals($data1, $data2->sort()->toArray()); + } + + public function testRsort() + { + $data1 = $this->data->toArray(); + $data2 = clone $this->data; + rsort($data1); + $this->assertEquals($data1, $data2->rsort()->toArray()); + } + + public function testUasort() + { + $data1 = ['a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4]; + $data2 = swoole_array($data1); + $cmp = function ($a, $b) { + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }; + uasort($data1, $cmp); + $this->assertEquals($data1, $data2->uasort($cmp)->toArray()); + } + + public function testNatsort() + { + $data1 = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; + $data2 = swoole_array($data1); + natsort($data1); + $this->assertEquals($data1, $data2->natsort()->toArray()); + } + + public function testNatcasesort() + { + $data1 = ['IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png']; + $data2 = swoole_array($data1); + natcasesort($data1); + $this->assertEquals($data1, $data2->natcasesort()->toArray()); + } + + public function testUsort() + { + $cmp = function ($a, $b) { + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }; + + $data1 = [3, 2, 5, 6, 1]; + $data2 = swoole_array($data1); + usort($data1, $cmp); + $this->assertEquals($data1, $data2->usort($cmp)->toArray()); + } + + public function testUksort() + { + $cmp = function ($a, $b) { + $a = preg_replace('@^(a|an|the) @', '', $a); + $b = preg_replace('@^(a|an|the) @', '', $b); + return strcasecmp($a, $b); + }; + + $data1 = ['John' => 1, 'the Earth' => 2, 'an apple' => 3, 'a banana' => 4]; + $data2 = swoole_array($data1); + uksort($data1, $cmp); + $this->assertEquals($data1, $data2->uksort($cmp)->toArray()); + } + + public function testCount() + { + $this->assertEquals($this->data->count(), count($this->control_data)); + } + + public function testOffsetGet() + { + $this->assertEquals($this->control_data[6], $this->data[6]); + } + + public function testReduce() + { + $this->assertEquals($this->data->product(), $this->data->reduce(function ($carry, $item) { + $carry *= $item; + return $carry; + }, 1)); + } + + public function testOffsetSet() + { + $value = 9999; + $data = clone $this->data; + $data[7] = $value; + $this->assertEquals($data->get(7), $value); + } + + public function testOffsetUnset() + { + $data = clone $this->data; + unset($data[6]); + $this->assertFalse($data->exists(6)); + } + + public function testIsEmpty() + { + $this->assertFalse($this->data->isEmpty()); + $this->assertTrue(swoole_array()->isEmpty()); + } + + public function testKeys() + { + $this->assertEquals( + $this->data_4->keys()->toArray(), + array_keys($this->data_4->toArray()) + ); + } + + public function testSet() + { + $data = clone $this->data_3; + $data->set('com', 'tal100'); + $this->assertEquals($data->get('com'), 'tal100'); + $this->assertEquals($data->count(), $this->data_3->count() + 1); + } + + public function testInsert() + { + $data = clone $this->data; + $data->insert(7, 888); + $this->assertEquals($data->get(7), 888); + } + + public function testRandomGet() + { + $this->assertTrue($this->data->contains($this->data->randomGet())); + } + + public function testSum() + { + $this->assertEquals($this->data->sum(), array_sum($this->control_data)); + } + + public function testFlip() + { + $result = $this->data_3->flip(); + $this->assertEquals($result->toArray(), array_flip($this->data_3->toArray())); + } + + public function testJoin() + { + $this->assertEquals($this->data_2->join('-'), implode('-', $this->data_2->toArray())); + } + + public function testChunk() + { + $this->assertEquals( + $this->data->chunk(2)->toArray(), + array_chunk($this->data->toArray(), 2) + ); + } + + public function testSlice() + { + $this->assertEquals( + $this->data->slice(2, 4)->toArray(), + array_slice($this->control_data, 2, 4) + ); + } + + public function testLastIndexOf() + { + $this->assertEquals($this->data->lastIndexOf(23), 9); + } + + public function testExists() + { + $this->assertTrue($this->data_3->exists('swoole')); + } + + public function testFirst() + { + $this->assertEquals($this->data_4->first(), 'lemon'); + } + + public function testLast() + { + $this->assertEquals($this->data_4->last(), 'apple'); + } + + public function testFirstKey() + { + $this->assertEquals($this->data_4->firstKey(), 'd'); + } + + public function testLastKey() + { + $this->assertEquals($this->data_4->lastKey(), 'c'); + } + + public function testIsAssoc() + { + $this->assertFalse($this->data->isAssoc()); + $this->assertFalse($this->data_2->isAssoc()); + $this->assertTrue($this->data_3->isAssoc()); + $this->assertTrue($this->data_4->isAssoc()); + } +} From 28852b1dfafa547cc6a46c6b337b59174b27b772 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 8 Oct 2020 19:11:07 -0300 Subject: [PATCH 2/3] Fix line endings --- src/core/ArrayObject.php | 1390 ++++++++++++++++---------------- tests/unit/ArrayObjectTest.php | 1184 +++++++++++++-------------- 2 files changed, 1287 insertions(+), 1287 deletions(-) diff --git a/src/core/ArrayObject.php b/src/core/ArrayObject.php index a879ec73..a70210bb 100644 --- a/src/core/ArrayObject.php +++ b/src/core/ArrayObject.php @@ -1,695 +1,695 @@ -array = $array; - } - - public function __toArray(): array - { - return $this->array; - } - - public function toArray(): array - { - return $this->array; - } - - public function isEmpty(): bool - { - return empty($this->array); - } - - public function count(): int - { - return count($this->array); - } - - /** - * @return mixed - */ - public function current() - { - return current($this->array); - } - - /** - * @return mixed - */ - public function key() - { - return key($this->array); - } - - public function valid(): bool - { - return array_key_exists($this->key(), $this->array); - } - - /** - * @return mixed - */ - public function rewind() - { - return reset($this->array); - } - - /** - * @return mixed - */ - public function next() - { - return next($this->array); - } - - /** - * @param mixed $key - * @return ArrayObject|StringObject - */ - public function get($key) - { - if (!$this->exists($key)) { - throw new ArrayKeyNotExists($key); - } - return static::detectType($this->array[$key]); - } - - /** - * @return mixed - */ - public function last() - { - $key = array_key_last($this->array); - if ($key === null) { - return null; - } - return $this->get($key); - } - - /** - * @return null|int|string - */ - public function firstKey() - { - return array_key_first($this->array); - } - - /** - * @return null|int|string - */ - public function lastKey() - { - return array_key_last($this->array); - } - - /** - * @return mixed - */ - public function first() - { - $key = array_key_first($this->array); - if ($key === null) { - return null; - } - return $this->get($key); - } - - /** - * @param mixed $key - * @param mixed $value - * @return $this - */ - public function set($key, $value): self - { - $this->array[$key] = $value; - return $this; - } - - /** - * @param mixed $key - * @return $this - */ - public function delete($key): self - { - unset($this->array[$key]); - return $this; - } - - /** - * @param mixed $value - * @return $this - */ - public function remove($value, bool $strict = true, bool $loop = false): self - { - do { - $key = $this->search($value, $strict); - if ($key === false) { - break; - } - unset($this->array[$key]); - } while ($loop); - - return $this; - } - - /** - * @return $this - */ - public function clear(): self - { - $this->array = []; - return $this; - } - - /** - * @param mixed $key - * @return null|mixed - */ - public function offsetGet($key) - { - if (!array_key_exists($key, $this->array)) { - return null; - } - return $this->array[$key]; - } - - /** - * @param mixed $key - * @param mixed $value - */ - public function offsetSet($key, $value): void - { - $this->array[$key] = $value; - } - - /** - * @param mixed $key - */ - public function offsetUnset($key): void - { - unset($this->array[$key]); - } - - /** - * @param mixed $key - * @return bool - */ - public function offsetExists($key) - { - return isset($this->array[$key]); - } - - /** - * @param mixed $key - */ - public function exists($key): bool - { - return array_key_exists($key, $this->array); - } - - /** - * @param mixed $value - */ - public function contains($value, bool $strict = true): bool - { - return in_array($value, $this->array, $strict); - } - - /** - * @param mixed $value - * @return mixed - */ - public function indexOf($value, bool $strict = true) - { - return $this->search($value, $strict); - } - - /** - * @param mixed $value - * @return mixed - */ - public function lastIndexOf($value, bool $strict = true) - { - $array = $this->array; - for (end($array); ($currentKey = key($array)) !== null; prev($array)) { - $currentValue = current($array); - if ($currentValue == $value) { - if ($strict && $currentValue !== $value) { - continue; - } - break; - } - } - return $currentKey; - } - - /** - * @param mixed $needle - * @return mixed - */ - public function search($needle, bool $strict = true) - { - return array_search($needle, $this->array, $strict); - } - - public function join(string $glue = ''): StringObject - { - return static::detectStringType(implode($glue, $this->array)); - } - - public function serialize(): StringObject - { - return static::detectStringType(serialize($this->array)); - } - - /** - * @param string $string - * @return $this - */ - public function unserialize($string): self - { - $this->array = (array) unserialize((string) $string); - return $this; - } - - /** - * @return float|int - */ - public function sum() - { - return array_sum($this->array); - } - - /** - * @return float|int - */ - public function product() - { - return array_product($this->array); - } - - /** - * @param mixed $value - * @return int - */ - public function push($value) - { - return $this->pushBack($value); - } - - /** - * @param mixed $value - * @return int - */ - public function pushFront($value) - { - return array_unshift($this->array, $value); - } - - public function append(...$values): ArrayObject - { - array_push($this->array, ...$values); - return $this; - } - - /** - * @param mixed $value - * @return int - */ - public function pushBack($value) - { - return array_push($this->array, $value); - } - - /** - * @param mixed $value - * @return $this - */ - public function insert(int $offset, $value): self - { - if (is_array($value) || is_object($value) || is_null($value)) { - $value = [$value]; - } - array_splice($this->array, $offset, 0, $value); - return $this; - } - - /** - * @return mixed - */ - public function pop() - { - return $this->popBack(); - } - - /** - * @return mixed - */ - public function popFront() - { - return array_shift($this->array); - } - - /** - * @return mixed - */ - public function popBack() - { - return array_pop($this->array); - } - - /** - * @param mixed $offset - * @param int $length - * @return static - */ - public function slice($offset, int $length = null, bool $preserve_keys = false): self - { - return new static(array_slice($this->array, ...func_get_args())); - } - - /** - * @return ArrayObject|mixed|StringObject - */ - public function randomGet() - { - return static::detectType($this->array[array_rand($this->array, 1)]); - } - - /** - * @return $this - */ - public function each(callable $fn): self - { - if (array_walk($this->array, $fn) === false) { - throw new RuntimeException('array_walk() failed'); - } - return $this; - } - - /** - * @param array $args - * @return static - */ - public function map(callable $fn, ...$args): self - { - return new static(array_map($fn, $this->array, ...$args)); - } - - /** - * @param null $initial - * @return mixed - */ - public function reduce(callable $fn, $initial = null) - { - return array_reduce($this->array, $fn, $initial); - } - - /** - * @param array $args - * @return static - */ - public function keys(...$args): self - { - return new static(array_keys($this->array, ...$args)); - } - - /** - * @return static - */ - public function values(): self - { - return new static(array_values($this->array)); - } - - /** - * @param mixed $column_key - * @param mixed $index - * @return static - */ - public function column($column_key, $index = null): self - { - return new static(array_column($this->array, $column_key, $index)); - } - - /** - * @return static - */ - public function unique(int $sort_flags = SORT_STRING): self - { - return new static(array_unique($this->array, $sort_flags)); - } - - /** - * @return static - */ - public function reverse(bool $preserve_keys = false): self - { - return new static(array_reverse($this->array, $preserve_keys)); - } - - /** - * @return static - */ - public function chunk(int $size, bool $preserve_keys = false): self - { - return new static(array_chunk($this->array, $size, $preserve_keys)); - } - - /** - * Swap keys and values in an array. - * @return static - */ - public function flip(): self - { - return new static(array_flip($this->array)); - } - - /** - * @return static - */ - public function filter(callable $fn, int $flag = 0): self - { - return new static(array_filter($this->array, $fn, $flag)); - } - - /** - * | Function name | Sorts by | Maintains key association | Order of sort | Related functions | - * | :---------------- | :------- | :-------------------------- | :-------------------------- | :---------------- | - * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | - * | asort() | value | yes | low to high | arsort() | - * | arsort() | value | yes | high to low | asort() | - * | krsort() | key | yes | high to low | ksort() | - * | ksort() | key | yes | low to high | asort() | - * | natcasesort() | value | yes | natural, case insensitive | natsort() | - * | natsort() | value | yes | natural | natcasesort() | - * | rsort() | value | no | high to low | sort() | - * | shuffle() | value | no | random | array_rand() | - * | sort() | value | no | low to high | rsort() | - * | uasort() | value | yes | user defined | uksort() | - * | uksort() | key | yes | user defined | uasort() | - * | usort() | value | no | user defined | uasort() | - */ - - /** - * @return $this - */ - public function asort(int $sort_flags = SORT_REGULAR): self - { - if (asort($this->array, $sort_flags) !== true) { - throw new RuntimeException('asort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function arsort(int $sort_flags = SORT_REGULAR): self - { - if (arsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('arsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function krsort(int $sort_flags = SORT_REGULAR): self - { - if (krsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('krsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function ksort(int $sort_flags = SORT_REGULAR): self - { - if (ksort($this->array, $sort_flags) !== true) { - throw new RuntimeException('ksort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function natcasesort(): self - { - if (natcasesort($this->array) !== true) { - throw new RuntimeException('natcasesort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function natsort(): self - { - if (natsort($this->array) !== true) { - throw new RuntimeException('natsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function rsort(int $sort_flags = SORT_REGULAR): self - { - if (rsort($this->array, $sort_flags) !== true) { - throw new RuntimeException('rsort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function shuffle(): self - { - if (shuffle($this->array) !== true) { - throw new RuntimeException('shuffle() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function sort(int $sort_flags = SORT_REGULAR): self - { - if (sort($this->array, $sort_flags) !== true) { - throw new RuntimeException('sort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function uasort(callable $value_compare_func): self - { - if (uasort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('uasort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function uksort(callable $value_compare_func): self - { - if (uksort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('uksort() failed'); - } - return $this; - } - - /** - * @return $this - */ - public function usort(callable $value_compare_func): self - { - if (usort($this->array, $value_compare_func) !== true) { - throw new RuntimeException('usort() failed'); - } - return $this; - } - - /** - * @return bool - */ - public function isAssoc(): bool - { - foreach (array_keys($this->array) as $key) { - if (is_string($key)) { - return true; - } - } - - return false; - } - - /** - * @param mixed $value - * @return ArrayObject|mixed|StringObject - */ - protected static function detectType($value) - { - if (is_string($value)) { - return static::detectStringType($value); - } - if (is_array($value)) { - return static::detectArrayType($value); - } - return $value; - } - - protected static function detectStringType(string $value): StringObject - { - return new StringObject($value); - } - - /** - * @return static - */ - protected static function detectArrayType(array $value): self - { - return new static($value); - } -} +array = $array; + } + + public function __toArray(): array + { + return $this->array; + } + + public function toArray(): array + { + return $this->array; + } + + public function isEmpty(): bool + { + return empty($this->array); + } + + public function count(): int + { + return count($this->array); + } + + /** + * @return mixed + */ + public function current() + { + return current($this->array); + } + + /** + * @return mixed + */ + public function key() + { + return key($this->array); + } + + public function valid(): bool + { + return array_key_exists($this->key(), $this->array); + } + + /** + * @return mixed + */ + public function rewind() + { + return reset($this->array); + } + + /** + * @return mixed + */ + public function next() + { + return next($this->array); + } + + /** + * @param mixed $key + * @return ArrayObject|StringObject + */ + public function get($key) + { + if (!$this->exists($key)) { + throw new ArrayKeyNotExists($key); + } + return static::detectType($this->array[$key]); + } + + /** + * @return mixed + */ + public function last() + { + $key = array_key_last($this->array); + if ($key === null) { + return null; + } + return $this->get($key); + } + + /** + * @return null|int|string + */ + public function firstKey() + { + return array_key_first($this->array); + } + + /** + * @return null|int|string + */ + public function lastKey() + { + return array_key_last($this->array); + } + + /** + * @return mixed + */ + public function first() + { + $key = array_key_first($this->array); + if ($key === null) { + return null; + } + return $this->get($key); + } + + /** + * @param mixed $key + * @param mixed $value + * @return $this + */ + public function set($key, $value): self + { + $this->array[$key] = $value; + return $this; + } + + /** + * @param mixed $key + * @return $this + */ + public function delete($key): self + { + unset($this->array[$key]); + return $this; + } + + /** + * @param mixed $value + * @return $this + */ + public function remove($value, bool $strict = true, bool $loop = false): self + { + do { + $key = $this->search($value, $strict); + if ($key === false) { + break; + } + unset($this->array[$key]); + } while ($loop); + + return $this; + } + + /** + * @return $this + */ + public function clear(): self + { + $this->array = []; + return $this; + } + + /** + * @param mixed $key + * @return null|mixed + */ + public function offsetGet($key) + { + if (!array_key_exists($key, $this->array)) { + return null; + } + return $this->array[$key]; + } + + /** + * @param mixed $key + * @param mixed $value + */ + public function offsetSet($key, $value): void + { + $this->array[$key] = $value; + } + + /** + * @param mixed $key + */ + public function offsetUnset($key): void + { + unset($this->array[$key]); + } + + /** + * @param mixed $key + * @return bool + */ + public function offsetExists($key) + { + return isset($this->array[$key]); + } + + /** + * @param mixed $key + */ + public function exists($key): bool + { + return array_key_exists($key, $this->array); + } + + /** + * @param mixed $value + */ + public function contains($value, bool $strict = true): bool + { + return in_array($value, $this->array, $strict); + } + + /** + * @param mixed $value + * @return mixed + */ + public function indexOf($value, bool $strict = true) + { + return $this->search($value, $strict); + } + + /** + * @param mixed $value + * @return mixed + */ + public function lastIndexOf($value, bool $strict = true) + { + $array = $this->array; + for (end($array); ($currentKey = key($array)) !== null; prev($array)) { + $currentValue = current($array); + if ($currentValue == $value) { + if ($strict && $currentValue !== $value) { + continue; + } + break; + } + } + return $currentKey; + } + + /** + * @param mixed $needle + * @return mixed + */ + public function search($needle, bool $strict = true) + { + return array_search($needle, $this->array, $strict); + } + + public function join(string $glue = ''): StringObject + { + return static::detectStringType(implode($glue, $this->array)); + } + + public function serialize(): StringObject + { + return static::detectStringType(serialize($this->array)); + } + + /** + * @param string $string + * @return $this + */ + public function unserialize($string): self + { + $this->array = (array) unserialize((string) $string); + return $this; + } + + /** + * @return float|int + */ + public function sum() + { + return array_sum($this->array); + } + + /** + * @return float|int + */ + public function product() + { + return array_product($this->array); + } + + /** + * @param mixed $value + * @return int + */ + public function push($value) + { + return $this->pushBack($value); + } + + /** + * @param mixed $value + * @return int + */ + public function pushFront($value) + { + return array_unshift($this->array, $value); + } + + public function append(...$values): ArrayObject + { + array_push($this->array, ...$values); + return $this; + } + + /** + * @param mixed $value + * @return int + */ + public function pushBack($value) + { + return array_push($this->array, $value); + } + + /** + * @param mixed $value + * @return $this + */ + public function insert(int $offset, $value): self + { + if (is_array($value) || is_object($value) || is_null($value)) { + $value = [$value]; + } + array_splice($this->array, $offset, 0, $value); + return $this; + } + + /** + * @return mixed + */ + public function pop() + { + return $this->popBack(); + } + + /** + * @return mixed + */ + public function popFront() + { + return array_shift($this->array); + } + + /** + * @return mixed + */ + public function popBack() + { + return array_pop($this->array); + } + + /** + * @param mixed $offset + * @param int $length + * @return static + */ + public function slice($offset, int $length = null, bool $preserve_keys = false): self + { + return new static(array_slice($this->array, ...func_get_args())); + } + + /** + * @return ArrayObject|mixed|StringObject + */ + public function randomGet() + { + return static::detectType($this->array[array_rand($this->array, 1)]); + } + + /** + * @return $this + */ + public function each(callable $fn): self + { + if (array_walk($this->array, $fn) === false) { + throw new RuntimeException('array_walk() failed'); + } + return $this; + } + + /** + * @param array $args + * @return static + */ + public function map(callable $fn, ...$args): self + { + return new static(array_map($fn, $this->array, ...$args)); + } + + /** + * @param null $initial + * @return mixed + */ + public function reduce(callable $fn, $initial = null) + { + return array_reduce($this->array, $fn, $initial); + } + + /** + * @param array $args + * @return static + */ + public function keys(...$args): self + { + return new static(array_keys($this->array, ...$args)); + } + + /** + * @return static + */ + public function values(): self + { + return new static(array_values($this->array)); + } + + /** + * @param mixed $column_key + * @param mixed $index + * @return static + */ + public function column($column_key, $index = null): self + { + return new static(array_column($this->array, $column_key, $index)); + } + + /** + * @return static + */ + public function unique(int $sort_flags = SORT_STRING): self + { + return new static(array_unique($this->array, $sort_flags)); + } + + /** + * @return static + */ + public function reverse(bool $preserve_keys = false): self + { + return new static(array_reverse($this->array, $preserve_keys)); + } + + /** + * @return static + */ + public function chunk(int $size, bool $preserve_keys = false): self + { + return new static(array_chunk($this->array, $size, $preserve_keys)); + } + + /** + * Swap keys and values in an array. + * @return static + */ + public function flip(): self + { + return new static(array_flip($this->array)); + } + + /** + * @return static + */ + public function filter(callable $fn, int $flag = 0): self + { + return new static(array_filter($this->array, $fn, $flag)); + } + + /** + * | Function name | Sorts by | Maintains key association | Order of sort | Related functions | + * | :---------------- | :------- | :-------------------------- | :-------------------------- | :---------------- | + * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | + * | asort() | value | yes | low to high | arsort() | + * | arsort() | value | yes | high to low | asort() | + * | krsort() | key | yes | high to low | ksort() | + * | ksort() | key | yes | low to high | asort() | + * | natcasesort() | value | yes | natural, case insensitive | natsort() | + * | natsort() | value | yes | natural | natcasesort() | + * | rsort() | value | no | high to low | sort() | + * | shuffle() | value | no | random | array_rand() | + * | sort() | value | no | low to high | rsort() | + * | uasort() | value | yes | user defined | uksort() | + * | uksort() | key | yes | user defined | uasort() | + * | usort() | value | no | user defined | uasort() | + */ + + /** + * @return $this + */ + public function asort(int $sort_flags = SORT_REGULAR): self + { + if (asort($this->array, $sort_flags) !== true) { + throw new RuntimeException('asort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function arsort(int $sort_flags = SORT_REGULAR): self + { + if (arsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('arsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function krsort(int $sort_flags = SORT_REGULAR): self + { + if (krsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('krsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function ksort(int $sort_flags = SORT_REGULAR): self + { + if (ksort($this->array, $sort_flags) !== true) { + throw new RuntimeException('ksort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function natcasesort(): self + { + if (natcasesort($this->array) !== true) { + throw new RuntimeException('natcasesort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function natsort(): self + { + if (natsort($this->array) !== true) { + throw new RuntimeException('natsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function rsort(int $sort_flags = SORT_REGULAR): self + { + if (rsort($this->array, $sort_flags) !== true) { + throw new RuntimeException('rsort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function shuffle(): self + { + if (shuffle($this->array) !== true) { + throw new RuntimeException('shuffle() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function sort(int $sort_flags = SORT_REGULAR): self + { + if (sort($this->array, $sort_flags) !== true) { + throw new RuntimeException('sort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function uasort(callable $value_compare_func): self + { + if (uasort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('uasort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function uksort(callable $value_compare_func): self + { + if (uksort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('uksort() failed'); + } + return $this; + } + + /** + * @return $this + */ + public function usort(callable $value_compare_func): self + { + if (usort($this->array, $value_compare_func) !== true) { + throw new RuntimeException('usort() failed'); + } + return $this; + } + + /** + * @return bool + */ + public function isAssoc(): bool + { + foreach (array_keys($this->array) as $key) { + if (is_string($key)) { + return true; + } + } + + return false; + } + + /** + * @param mixed $value + * @return ArrayObject|mixed|StringObject + */ + protected static function detectType($value) + { + if (is_string($value)) { + return static::detectStringType($value); + } + if (is_array($value)) { + return static::detectArrayType($value); + } + return $value; + } + + protected static function detectStringType(string $value): StringObject + { + return new StringObject($value); + } + + /** + * @return static + */ + protected static function detectArrayType(array $value): self + { + return new static($value); + } +} diff --git a/tests/unit/ArrayObjectTest.php b/tests/unit/ArrayObjectTest.php index dd1c4349..ed5848e1 100644 --- a/tests/unit/ArrayObjectTest.php +++ b/tests/unit/ArrayObjectTest.php @@ -1,592 +1,592 @@ -data = swoole_string($_data)->split(',')->each(function (&$item) { - $item = intval($item); - }); - - $this->data_2 = swoole_array(['hello', 'world', 'swoole']); - $this->data_3 = swoole_array([ - 'hello' => 'world', - 'swoole' => 'php', - 'nihao' => '中国人', - ]); - $this->data_4 = swoole_array([ - 'd' => 'lemon', - 'a' => 'orange', - 'b' => 'banana', - 'c' => 'apple', - ]); - - $array = explode(',', $_data); - foreach ($array as &$v) { - $v = trim($v); - } - $this->control_data = $array; - parent::__construct($name, $data, $dataName); - } - - /** - * @covers \Swoole\ArrayObject::toArray() - */ - public function testToArray() - { - $this->assertEquals($this->data->toArray(), $this->control_data); - } - - /** - * @covers \Swoole\ArrayObject::each() - * @covers \Swoole\ArrayObject::sort() - * @covers \Swoole\ArrayObject::unique() - */ - public function testMix() - { - $datao = clone $this->data; - $data = $datao->sort()->unique()->toArray(); - - $copy_data = $this->control_data; - sort($copy_data); - $expectResult = array_unique($copy_data); - - $this->assertEquals($data, $expectResult); - } - - /** - * @covers \Swoole\ArrayObject::serialize() - */ - public function testSerialize() - { - $this->assertEquals(serialize($this->data->toArray()), $this->data->serialize()); - } - - /** - * @covers \Swoole\ArrayObject::unique() - */ - public function testUnique() - { - $data = $this->data->unique()->toArray(); - $copy_data = $this->control_data; - $expectResult = array_unique($copy_data); - - $this->assertEquals($data, $expectResult); - } - - public function testTraverse() - { - $newArray = []; - foreach ($this->data as $value) { - $newArray[] = $value; - } - $this->assertEquals($this->control_data, $newArray); - } - - public function testRemove() - { - $data1 = swoole_array_list('hello', 'world', 'swoole'); - $data2 = $data1->toArray(); - - $this->assertEquals( - $data1->remove('hello')->values()->toArray(), - array_values(array_slice($data2, 1)) - ); - } - - public function testFilter() - { - $data = $this->data->filter(function ($v) { - return $v > 20; - }); - - $find = false; - foreach ($data as $v) { - if ($v <= 20) { - $find = true; - break; - } - } - $this->assertFalse($find); - } - - public function testOffsetExists() - { - $this->assertEquals($this->data->offsetExists(9), isset($this->control_data[9])); - } - - public function testSearch() - { - $this->assertEquals($this->data_2->search('swoole'), 2); - } - - public function testValues() - { - $this->assertEquals( - $this->data_3->values()->toArray(), - array_values($this->data_3->toArray()) - ); - } - - public function testMap() - { - $arr1 = [1, 2, 3, 4, 5]; - $arr2 = [6, 7, 8, 9, 10]; - $arr3 = [62, 71, 82, 93, 103]; - - $this->assertEquals(swoole_array($arr1)->map(function ($val1) { - return $val1 * 3; - })->toArray(), [ - 3, - 6, - 9, - 12, - 15, - ]); - - $this->assertEquals(swoole_array($arr1)->map(function ($val1, $val2, $val3) { - return $val1 + $val2 + $val3; - }, $arr2, $arr3)->toArray(), [ - 69, - 80, - 93, - 106, - 118, - ]); - } - - public function testClear() - { - $this->assertEquals((clone $this->data->clear())->toArray(), []); - } - - public function testReverse() - { - $this->assertEquals($this->data->reverse()->toArray(), array_reverse($this->control_data)); - } - - public function testDelete() - { - $data = clone $this->data_3; - $expectData = $data->toArray(); - unset($expectData['swoole']); - - $this->assertEquals( - $data->delete('swoole')->toArray(), - $expectData - ); - } - - public function testContains() - { - $this->assertTrue($this->data_2->contains('swoole')); - $this->assertFalse($this->data_2->contains('aliyun')); - } - - public function testUnserialize() - { - $str = serialize($this->data->toArray()); - $this->assertEquals( - swoole_array([])->unserialize($str)->toArray(), - $this->data->toArray() - ); - } - - public function testPushBack() - { - $data = clone $this->data; - $data->pushBack(999); - $this->assertEquals($data->search(999), $data->count() - 1); - } - - public function testPushFront() - { - $data = clone $this->data; - $data->pushFront(999); - $this->assertEquals($data->search(999), 0); - } - - public function testPopFront() - { - $data = clone $this->data; - $value = $data->popFront(); - $this->assertEquals($value, $this->data->first()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPopBack() - { - $data = clone $this->data; - $value = $data->popBack(); - $this->assertEquals($value, $this->data->last()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPop() - { - $data = clone $this->data; - $value = $data->pop(); - $this->assertEquals($value, $this->data->last()); - $this->assertEquals($data->count(), $this->data->count() - 1); - } - - public function testPush() - { - $data = clone $this->data; - $data->pushBack(999); - $this->assertEquals($data->search(999), $data->count() - 1); - } - - public function testAppend() - { - $data = clone $this->data; - $data->append(999)->append(888); - $data->append(10000, 20000, 30000); - $this->assertTrue($data->contains(999)); - $this->assertTrue($data->contains(888)); - $this->assertTrue($data->contains(30000)); - } - - public function testShuffle() - { - $data1 = clone $this->data; - $data2 = clone $this->data; - $data1->shuffle(); - $this->assertNotEquals($data1->values()->toArray(), $data2->values()->toArray()); - $this->assertEquals($data1->values()->sort()->toArray(), $data2->values()->sort()->toArray()); - } - - public function testColumn() - { - $records = [ - [ - 'id' => 2135, - 'first_name' => 'John', - 'last_name' => 'Doe', - ], - [ - 'id' => 3245, - 'first_name' => 'Sally', - 'last_name' => 'Smith', - ], - [ - 'id' => 5342, - 'first_name' => 'Jane', - 'last_name' => 'Jones', - ], - [ - 'id' => 5623, - 'first_name' => 'Peter', - 'last_name' => 'Doe', - ], - ]; - - $this->assertEquals( - array_column($records, 'first_name'), - swoole_array($records)->column('first_name')->toArray() - ); - - $this->assertEquals( - array_column($records, 'first_name', 'id'), - swoole_array($records)->column('first_name', 'id')->toArray() - ); - } - - public function testIndexOf() - { - $this->assertEquals($this->data->indexOf(23), 7); - } - - public function testProduct() - { - $value = 1; - foreach ($this->control_data as $v) { - $value *= $v; - } - $this->assertEquals($this->data->product(), $value); - } - - public function testAsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - asort($data1); - $this->assertEquals($data1, $data2->asort()->toArray()); - } - - public function testArsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - arsort($data1); - $this->assertEquals($data1, $data2->arsort()->toArray()); - } - - public function testKsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - ksort($data1); - $this->assertEquals($data1, $data2->ksort()->toArray()); - } - - public function testKrsort() - { - $data1 = $this->data_4->toArray(); - $data2 = clone $this->data_4; - krsort($data1); - $this->assertEquals($data1, $data2->krsort()->toArray()); - } - - public function testSort() - { - $data1 = $this->data->toArray(); - $data2 = clone $this->data; - sort($data1); - $this->assertEquals($data1, $data2->sort()->toArray()); - } - - public function testRsort() - { - $data1 = $this->data->toArray(); - $data2 = clone $this->data; - rsort($data1); - $this->assertEquals($data1, $data2->rsort()->toArray()); - } - - public function testUasort() - { - $data1 = ['a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4]; - $data2 = swoole_array($data1); - $cmp = function ($a, $b) { - if ($a == $b) { - return 0; - } - return ($a < $b) ? -1 : 1; - }; - uasort($data1, $cmp); - $this->assertEquals($data1, $data2->uasort($cmp)->toArray()); - } - - public function testNatsort() - { - $data1 = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; - $data2 = swoole_array($data1); - natsort($data1); - $this->assertEquals($data1, $data2->natsort()->toArray()); - } - - public function testNatcasesort() - { - $data1 = ['IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png']; - $data2 = swoole_array($data1); - natcasesort($data1); - $this->assertEquals($data1, $data2->natcasesort()->toArray()); - } - - public function testUsort() - { - $cmp = function ($a, $b) { - if ($a == $b) { - return 0; - } - return ($a < $b) ? -1 : 1; - }; - - $data1 = [3, 2, 5, 6, 1]; - $data2 = swoole_array($data1); - usort($data1, $cmp); - $this->assertEquals($data1, $data2->usort($cmp)->toArray()); - } - - public function testUksort() - { - $cmp = function ($a, $b) { - $a = preg_replace('@^(a|an|the) @', '', $a); - $b = preg_replace('@^(a|an|the) @', '', $b); - return strcasecmp($a, $b); - }; - - $data1 = ['John' => 1, 'the Earth' => 2, 'an apple' => 3, 'a banana' => 4]; - $data2 = swoole_array($data1); - uksort($data1, $cmp); - $this->assertEquals($data1, $data2->uksort($cmp)->toArray()); - } - - public function testCount() - { - $this->assertEquals($this->data->count(), count($this->control_data)); - } - - public function testOffsetGet() - { - $this->assertEquals($this->control_data[6], $this->data[6]); - } - - public function testReduce() - { - $this->assertEquals($this->data->product(), $this->data->reduce(function ($carry, $item) { - $carry *= $item; - return $carry; - }, 1)); - } - - public function testOffsetSet() - { - $value = 9999; - $data = clone $this->data; - $data[7] = $value; - $this->assertEquals($data->get(7), $value); - } - - public function testOffsetUnset() - { - $data = clone $this->data; - unset($data[6]); - $this->assertFalse($data->exists(6)); - } - - public function testIsEmpty() - { - $this->assertFalse($this->data->isEmpty()); - $this->assertTrue(swoole_array()->isEmpty()); - } - - public function testKeys() - { - $this->assertEquals( - $this->data_4->keys()->toArray(), - array_keys($this->data_4->toArray()) - ); - } - - public function testSet() - { - $data = clone $this->data_3; - $data->set('com', 'tal100'); - $this->assertEquals($data->get('com'), 'tal100'); - $this->assertEquals($data->count(), $this->data_3->count() + 1); - } - - public function testInsert() - { - $data = clone $this->data; - $data->insert(7, 888); - $this->assertEquals($data->get(7), 888); - } - - public function testRandomGet() - { - $this->assertTrue($this->data->contains($this->data->randomGet())); - } - - public function testSum() - { - $this->assertEquals($this->data->sum(), array_sum($this->control_data)); - } - - public function testFlip() - { - $result = $this->data_3->flip(); - $this->assertEquals($result->toArray(), array_flip($this->data_3->toArray())); - } - - public function testJoin() - { - $this->assertEquals($this->data_2->join('-'), implode('-', $this->data_2->toArray())); - } - - public function testChunk() - { - $this->assertEquals( - $this->data->chunk(2)->toArray(), - array_chunk($this->data->toArray(), 2) - ); - } - - public function testSlice() - { - $this->assertEquals( - $this->data->slice(2, 4)->toArray(), - array_slice($this->control_data, 2, 4) - ); - } - - public function testLastIndexOf() - { - $this->assertEquals($this->data->lastIndexOf(23), 9); - } - - public function testExists() - { - $this->assertTrue($this->data_3->exists('swoole')); - } - - public function testFirst() - { - $this->assertEquals($this->data_4->first(), 'lemon'); - } - - public function testLast() - { - $this->assertEquals($this->data_4->last(), 'apple'); - } - - public function testFirstKey() - { - $this->assertEquals($this->data_4->firstKey(), 'd'); - } - - public function testLastKey() - { - $this->assertEquals($this->data_4->lastKey(), 'c'); - } - - public function testIsAssoc() - { - $this->assertFalse($this->data->isAssoc()); - $this->assertFalse($this->data_2->isAssoc()); - $this->assertTrue($this->data_3->isAssoc()); - $this->assertTrue($this->data_4->isAssoc()); - } -} +data = swoole_string($_data)->split(',')->each(function (&$item) { + $item = intval($item); + }); + + $this->data_2 = swoole_array(['hello', 'world', 'swoole']); + $this->data_3 = swoole_array([ + 'hello' => 'world', + 'swoole' => 'php', + 'nihao' => '中国人', + ]); + $this->data_4 = swoole_array([ + 'd' => 'lemon', + 'a' => 'orange', + 'b' => 'banana', + 'c' => 'apple', + ]); + + $array = explode(',', $_data); + foreach ($array as &$v) { + $v = trim($v); + } + $this->control_data = $array; + parent::__construct($name, $data, $dataName); + } + + /** + * @covers \Swoole\ArrayObject::toArray() + */ + public function testToArray() + { + $this->assertEquals($this->data->toArray(), $this->control_data); + } + + /** + * @covers \Swoole\ArrayObject::each() + * @covers \Swoole\ArrayObject::sort() + * @covers \Swoole\ArrayObject::unique() + */ + public function testMix() + { + $datao = clone $this->data; + $data = $datao->sort()->unique()->toArray(); + + $copy_data = $this->control_data; + sort($copy_data); + $expectResult = array_unique($copy_data); + + $this->assertEquals($data, $expectResult); + } + + /** + * @covers \Swoole\ArrayObject::serialize() + */ + public function testSerialize() + { + $this->assertEquals(serialize($this->data->toArray()), $this->data->serialize()); + } + + /** + * @covers \Swoole\ArrayObject::unique() + */ + public function testUnique() + { + $data = $this->data->unique()->toArray(); + $copy_data = $this->control_data; + $expectResult = array_unique($copy_data); + + $this->assertEquals($data, $expectResult); + } + + public function testTraverse() + { + $newArray = []; + foreach ($this->data as $value) { + $newArray[] = $value; + } + $this->assertEquals($this->control_data, $newArray); + } + + public function testRemove() + { + $data1 = swoole_array_list('hello', 'world', 'swoole'); + $data2 = $data1->toArray(); + + $this->assertEquals( + $data1->remove('hello')->values()->toArray(), + array_values(array_slice($data2, 1)) + ); + } + + public function testFilter() + { + $data = $this->data->filter(function ($v) { + return $v > 20; + }); + + $find = false; + foreach ($data as $v) { + if ($v <= 20) { + $find = true; + break; + } + } + $this->assertFalse($find); + } + + public function testOffsetExists() + { + $this->assertEquals($this->data->offsetExists(9), isset($this->control_data[9])); + } + + public function testSearch() + { + $this->assertEquals($this->data_2->search('swoole'), 2); + } + + public function testValues() + { + $this->assertEquals( + $this->data_3->values()->toArray(), + array_values($this->data_3->toArray()) + ); + } + + public function testMap() + { + $arr1 = [1, 2, 3, 4, 5]; + $arr2 = [6, 7, 8, 9, 10]; + $arr3 = [62, 71, 82, 93, 103]; + + $this->assertEquals(swoole_array($arr1)->map(function ($val1) { + return $val1 * 3; + })->toArray(), [ + 3, + 6, + 9, + 12, + 15, + ]); + + $this->assertEquals(swoole_array($arr1)->map(function ($val1, $val2, $val3) { + return $val1 + $val2 + $val3; + }, $arr2, $arr3)->toArray(), [ + 69, + 80, + 93, + 106, + 118, + ]); + } + + public function testClear() + { + $this->assertEquals((clone $this->data->clear())->toArray(), []); + } + + public function testReverse() + { + $this->assertEquals($this->data->reverse()->toArray(), array_reverse($this->control_data)); + } + + public function testDelete() + { + $data = clone $this->data_3; + $expectData = $data->toArray(); + unset($expectData['swoole']); + + $this->assertEquals( + $data->delete('swoole')->toArray(), + $expectData + ); + } + + public function testContains() + { + $this->assertTrue($this->data_2->contains('swoole')); + $this->assertFalse($this->data_2->contains('aliyun')); + } + + public function testUnserialize() + { + $str = serialize($this->data->toArray()); + $this->assertEquals( + swoole_array([])->unserialize($str)->toArray(), + $this->data->toArray() + ); + } + + public function testPushBack() + { + $data = clone $this->data; + $data->pushBack(999); + $this->assertEquals($data->search(999), $data->count() - 1); + } + + public function testPushFront() + { + $data = clone $this->data; + $data->pushFront(999); + $this->assertEquals($data->search(999), 0); + } + + public function testPopFront() + { + $data = clone $this->data; + $value = $data->popFront(); + $this->assertEquals($value, $this->data->first()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPopBack() + { + $data = clone $this->data; + $value = $data->popBack(); + $this->assertEquals($value, $this->data->last()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPop() + { + $data = clone $this->data; + $value = $data->pop(); + $this->assertEquals($value, $this->data->last()); + $this->assertEquals($data->count(), $this->data->count() - 1); + } + + public function testPush() + { + $data = clone $this->data; + $data->pushBack(999); + $this->assertEquals($data->search(999), $data->count() - 1); + } + + public function testAppend() + { + $data = clone $this->data; + $data->append(999)->append(888); + $data->append(10000, 20000, 30000); + $this->assertTrue($data->contains(999)); + $this->assertTrue($data->contains(888)); + $this->assertTrue($data->contains(30000)); + } + + public function testShuffle() + { + $data1 = clone $this->data; + $data2 = clone $this->data; + $data1->shuffle(); + $this->assertNotEquals($data1->values()->toArray(), $data2->values()->toArray()); + $this->assertEquals($data1->values()->sort()->toArray(), $data2->values()->sort()->toArray()); + } + + public function testColumn() + { + $records = [ + [ + 'id' => 2135, + 'first_name' => 'John', + 'last_name' => 'Doe', + ], + [ + 'id' => 3245, + 'first_name' => 'Sally', + 'last_name' => 'Smith', + ], + [ + 'id' => 5342, + 'first_name' => 'Jane', + 'last_name' => 'Jones', + ], + [ + 'id' => 5623, + 'first_name' => 'Peter', + 'last_name' => 'Doe', + ], + ]; + + $this->assertEquals( + array_column($records, 'first_name'), + swoole_array($records)->column('first_name')->toArray() + ); + + $this->assertEquals( + array_column($records, 'first_name', 'id'), + swoole_array($records)->column('first_name', 'id')->toArray() + ); + } + + public function testIndexOf() + { + $this->assertEquals($this->data->indexOf(23), 7); + } + + public function testProduct() + { + $value = 1; + foreach ($this->control_data as $v) { + $value *= $v; + } + $this->assertEquals($this->data->product(), $value); + } + + public function testAsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + asort($data1); + $this->assertEquals($data1, $data2->asort()->toArray()); + } + + public function testArsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + arsort($data1); + $this->assertEquals($data1, $data2->arsort()->toArray()); + } + + public function testKsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + ksort($data1); + $this->assertEquals($data1, $data2->ksort()->toArray()); + } + + public function testKrsort() + { + $data1 = $this->data_4->toArray(); + $data2 = clone $this->data_4; + krsort($data1); + $this->assertEquals($data1, $data2->krsort()->toArray()); + } + + public function testSort() + { + $data1 = $this->data->toArray(); + $data2 = clone $this->data; + sort($data1); + $this->assertEquals($data1, $data2->sort()->toArray()); + } + + public function testRsort() + { + $data1 = $this->data->toArray(); + $data2 = clone $this->data; + rsort($data1); + $this->assertEquals($data1, $data2->rsort()->toArray()); + } + + public function testUasort() + { + $data1 = ['a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4]; + $data2 = swoole_array($data1); + $cmp = function ($a, $b) { + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }; + uasort($data1, $cmp); + $this->assertEquals($data1, $data2->uasort($cmp)->toArray()); + } + + public function testNatsort() + { + $data1 = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; + $data2 = swoole_array($data1); + natsort($data1); + $this->assertEquals($data1, $data2->natsort()->toArray()); + } + + public function testNatcasesort() + { + $data1 = ['IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png']; + $data2 = swoole_array($data1); + natcasesort($data1); + $this->assertEquals($data1, $data2->natcasesort()->toArray()); + } + + public function testUsort() + { + $cmp = function ($a, $b) { + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }; + + $data1 = [3, 2, 5, 6, 1]; + $data2 = swoole_array($data1); + usort($data1, $cmp); + $this->assertEquals($data1, $data2->usort($cmp)->toArray()); + } + + public function testUksort() + { + $cmp = function ($a, $b) { + $a = preg_replace('@^(a|an|the) @', '', $a); + $b = preg_replace('@^(a|an|the) @', '', $b); + return strcasecmp($a, $b); + }; + + $data1 = ['John' => 1, 'the Earth' => 2, 'an apple' => 3, 'a banana' => 4]; + $data2 = swoole_array($data1); + uksort($data1, $cmp); + $this->assertEquals($data1, $data2->uksort($cmp)->toArray()); + } + + public function testCount() + { + $this->assertEquals($this->data->count(), count($this->control_data)); + } + + public function testOffsetGet() + { + $this->assertEquals($this->control_data[6], $this->data[6]); + } + + public function testReduce() + { + $this->assertEquals($this->data->product(), $this->data->reduce(function ($carry, $item) { + $carry *= $item; + return $carry; + }, 1)); + } + + public function testOffsetSet() + { + $value = 9999; + $data = clone $this->data; + $data[7] = $value; + $this->assertEquals($data->get(7), $value); + } + + public function testOffsetUnset() + { + $data = clone $this->data; + unset($data[6]); + $this->assertFalse($data->exists(6)); + } + + public function testIsEmpty() + { + $this->assertFalse($this->data->isEmpty()); + $this->assertTrue(swoole_array()->isEmpty()); + } + + public function testKeys() + { + $this->assertEquals( + $this->data_4->keys()->toArray(), + array_keys($this->data_4->toArray()) + ); + } + + public function testSet() + { + $data = clone $this->data_3; + $data->set('com', 'tal100'); + $this->assertEquals($data->get('com'), 'tal100'); + $this->assertEquals($data->count(), $this->data_3->count() + 1); + } + + public function testInsert() + { + $data = clone $this->data; + $data->insert(7, 888); + $this->assertEquals($data->get(7), 888); + } + + public function testRandomGet() + { + $this->assertTrue($this->data->contains($this->data->randomGet())); + } + + public function testSum() + { + $this->assertEquals($this->data->sum(), array_sum($this->control_data)); + } + + public function testFlip() + { + $result = $this->data_3->flip(); + $this->assertEquals($result->toArray(), array_flip($this->data_3->toArray())); + } + + public function testJoin() + { + $this->assertEquals($this->data_2->join('-'), implode('-', $this->data_2->toArray())); + } + + public function testChunk() + { + $this->assertEquals( + $this->data->chunk(2)->toArray(), + array_chunk($this->data->toArray(), 2) + ); + } + + public function testSlice() + { + $this->assertEquals( + $this->data->slice(2, 4)->toArray(), + array_slice($this->control_data, 2, 4) + ); + } + + public function testLastIndexOf() + { + $this->assertEquals($this->data->lastIndexOf(23), 9); + } + + public function testExists() + { + $this->assertTrue($this->data_3->exists('swoole')); + } + + public function testFirst() + { + $this->assertEquals($this->data_4->first(), 'lemon'); + } + + public function testLast() + { + $this->assertEquals($this->data_4->last(), 'apple'); + } + + public function testFirstKey() + { + $this->assertEquals($this->data_4->firstKey(), 'd'); + } + + public function testLastKey() + { + $this->assertEquals($this->data_4->lastKey(), 'c'); + } + + public function testIsAssoc() + { + $this->assertFalse($this->data->isAssoc()); + $this->assertFalse($this->data_2->isAssoc()); + $this->assertTrue($this->data_3->isAssoc()); + $this->assertTrue($this->data_4->isAssoc()); + } +} From c6ba1b1e91fa53890368dc925b4527037d868895 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 8 Oct 2020 19:16:51 -0300 Subject: [PATCH 3/3] Remove unnecessary docblock for return type --- src/core/ArrayObject.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/core/ArrayObject.php b/src/core/ArrayObject.php index a70210bb..ccc0abce 100644 --- a/src/core/ArrayObject.php +++ b/src/core/ArrayObject.php @@ -651,9 +651,6 @@ public function usort(callable $value_compare_func): self return $this; } - /** - * @return bool - */ public function isAssoc(): bool { foreach (array_keys($this->array) as $key) {