From 8a3b6c905f6f28447891a9f051d6d3063f553f92 Mon Sep 17 00:00:00 2001 From: inhere Date: Sun, 21 Nov 2021 21:25:26 +0800 Subject: [PATCH] move stream data api to stdlib --- app/Lib/Stream/BaseStream.php | 13 --- app/Lib/Stream/ListStream.php | 174 +------------------------------ app/Lib/Stream/MapStream.php | 147 +------------------------- app/Lib/Stream/StringsStream.php | 113 +------------------- 4 files changed, 3 insertions(+), 444 deletions(-) delete mode 100644 app/Lib/Stream/BaseStream.php diff --git a/app/Lib/Stream/BaseStream.php b/app/Lib/Stream/BaseStream.php deleted file mode 100644 index 8646755..0000000 --- a/app/Lib/Stream/BaseStream.php +++ /dev/null @@ -1,13 +0,0 @@ -each($func); - } - - /** - * @param callable(array|mixed): string $func - * - * @return $this - */ - public function each(callable $func): self - { - $new = new self(); - foreach ($this as $item) { - $new->append($func($item)); - } - - return $new; - } - - /** - * @param callable(array|mixed): string $func - * @param BaseStream $new - * - * @return BaseStream - */ - public function eachTo(callable $func, BaseStream $new): BaseStream - { - foreach ($this as $item) { - $new->append($func($item)); - // $new->offsetSet($key, $func($item)); - } - - return $new; - } - - /** - * @param callable(array|mixed, int|string): array $func - * @param array $arr - * - * @return array - */ - public function eachToArray(callable $func, array $arr = []): array - { - foreach ($this as $idx => $item) { - $arr[] = $func($item, $idx); - } - return $arr; - } - - /** - * @param callable(array|mixed): array $func - * @param MapStream $new - * - * @return MapStream - */ - public function eachToMapStream(callable $func, MapStream $new): MapStream - { - foreach ($this as $item) { - [$key, $val] = $func($item); - $new->offsetSet($key, $val); - } - - return $new; - } - - /** - * @param callable(array|mixed): array{string, mixed} $func - * @param array $map - * - * @return array - */ - public function eachToMap(callable $func, array $map = []): array - { - foreach ($this as $item) { - [$key, $val] = $func($item); - $map[$key] = $val; - } - - return $map; - } - - /** - * @param callable(array): bool $func - * @param bool|mixed $apply - * - * @return $this - */ - public function filterIf(callable $func, mixed $apply): self - { - if (!$apply) { - return $this; - } - - return $this->filter($func); - } - - /** - * @param callable(array|mixed): bool $func - * - * @return $this - */ - public function filter(callable $func): self - { - $new = new self(); - foreach ($this as $item) { - if ($func($item)) { - $new->append($item); - } - } - - return $new; - } - - /** - * @param string $sep - * - * @return string - */ - public function join(string $sep = ','): string - { - return $this->implode($sep); - } - - /** - * @param string $sep - * - * @return string - */ - public function implode(string $sep = ','): string - { - return implode($sep, $this->getArrayCopy()); - } - - // public function prepend(string $value): self - // { - // return $this; - // } - - public function append($value): self - { - parent::append($value); - return $this; - } - - /** - * @return array - */ - public function toArray(): array - { - return $this->getArrayCopy(); - } } diff --git a/app/Lib/Stream/MapStream.php b/app/Lib/Stream/MapStream.php index f00071a..ce0f6fc 100644 --- a/app/Lib/Stream/MapStream.php +++ b/app/Lib/Stream/MapStream.php @@ -7,152 +7,7 @@ /** * class ListStream */ -class MapStream extends BaseStream +class MapStream extends \Toolkit\Stdlib\Util\Stream\MapStream { - /** - * @param array[] $data - * - * @return static - */ - public static function new(array $data): self - { - return new self($data); - } - /** - * @param array $data - * - * @return $this - */ - public function load(array $data): self - { - foreach ($data as $key => $value) { - $this->offsetSet($key, $value); - } - - return $this; - } - - /** - * @param callable(array): string $func - * @param bool|mixed $apply - * - * @return $this - */ - public function eachIf(callable $func, mixed $apply): self - { - if (!$apply) { - return $this; - } - - return $this->each($func); - } - - /** - * @param callable(array): string $func - * - * @return $this - */ - public function each(callable $func): self - { - $new = new self(); - foreach ($this as $key => $str) { - // $new->append($func($str)); - $new->offsetSet($key, $func($str)); - } - - return $new; - } - - /** - * @param callable(array): string $func - * - * @return $this - */ - public function eachTo(callable $func, BaseStream $new): BaseStream - { - foreach ($this as $key => $item) { - // $new->append($func($str)); - $item = $func($item, $key); - $new->offsetSet($key, $item); - } - - return $new; - } - - /** - * @param callable(array): mixed $func - * - * @return array - */ - public function eachToMap(callable $func): array - { - $map = []; - foreach ($this as $key => $item) { - $map[$key] = $func($item); - } - - return $map; - } - - /** - * @param callable(array): bool $func - * @param bool|mixed $apply - * - * @return $this - */ - public function filterIf(callable $func, mixed $apply): self - { - if (!$apply) { - return $this; - } - - return $this->filter($func); - } - - /** - * @param callable(array): bool $func - * - * @return $this - */ - public function filter(callable $func): self - { - $new = new self(); - foreach ($this as $key => $str) { - if ($func($str)) { - // $new->append($str); - $new->offsetSet($key, $func($str)); - } - } - - return $new; - } - - /** - * @param string $sep - * - * @return string - */ - public function joinValues(string $sep = ','): string - { - return $this->implodeValues($sep); - } - - /** - * @param string $sep - * - * @return string - */ - public function implodeValues(string $sep = ','): string - { - return implode($sep, $this->getArrayCopy()); - } - - /** - * @return array - */ - public function toArray(): array - { - return $this->getArrayCopy(); - } } diff --git a/app/Lib/Stream/StringsStream.php b/app/Lib/Stream/StringsStream.php index 8d5bcd7..70cb69b 100644 --- a/app/Lib/Stream/StringsStream.php +++ b/app/Lib/Stream/StringsStream.php @@ -2,121 +2,10 @@ namespace Inhere\Kite\Lib\Stream; -use function implode; - /** * class StringsStream */ -class StringsStream extends BaseStream +class StringsStream extends \Toolkit\Stdlib\Util\Stream\StringsStream { - /** - * @param string[] $strings - * - * @return static - */ - public static function new(array $strings): self - { - return new self($strings); - } - - /** - * @param callable(string): string $func - * @param bool|mixed $apply - * - * @return $this - */ - public function eachIf(callable $func, mixed $apply): self - { - if (!$apply) { - return $this; - } - - return $this->each($func); - } - - /** - * @param callable(string): string $func - * - * @return $this - */ - public function each(callable $func): self - { - $new = new self(); - foreach ($this as $str) { - $new->append($func($str)); - } - - return $new; - } - - /** - * @param callable(string): bool $func - * @param bool|mixed $apply - * - * @return $this - */ - public function filterIf(callable $func, mixed $apply): self - { - if (!$apply) { - return $this; - } - - return $this->filter($func); - } - - /** - * @param callable(string): bool $func - * - * @return $this - */ - public function filter(callable $func): self - { - $new = new self(); - foreach ($this as $str) { - if ($func($str)) { - $new->append($str); - } - } - - return $new; - } - - /** - * @param string $sep - * - * @return string - */ - public function join(string $sep = ','): string - { - return $this->implode($sep); - } - - /** - * @param string $sep - * - * @return string - */ - public function implode(string $sep = ','): string - { - return implode($sep, $this->getArrayCopy()); - } - - // public function prepend(string $value): self - // { - // return $this; - // } - - public function append($value): self - { - parent::append((string)$value); - return $this; - } - /** - * @return array - */ - public function toArray(): array - { - return $this->getArrayCopy(); - } }