Skip to content

Commit

Permalink
move stream data api to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 21, 2021
1 parent a612f11 commit 8a3b6c9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 444 deletions.
13 changes: 0 additions & 13 deletions app/Lib/Stream/BaseStream.php

This file was deleted.

174 changes: 1 addition & 173 deletions app/Lib/Stream/ListStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,183 +2,11 @@

namespace Inhere\Kite\Lib\Stream;

use function implode;

/**
* class ListStream
*/
class ListStream extends BaseStream
class ListStream extends \Toolkit\Stdlib\Util\Stream\ListStream
{
/**
* @param array $data
*
* @return static
*/
public static function new(array $data): self
{
return new self($data);
}

/**
* @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|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<string, mixed>
*/
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();
}
}
147 changes: 1 addition & 146 deletions app/Lib/Stream/MapStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
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();
}
}

0 comments on commit 8a3b6c9

Please sign in to comment.