Skip to content

Commit

Permalink
👔 up: update some helper and stream and object logic
Browse files Browse the repository at this point in the history
- add ci test on php8.3
  • Loading branch information
inhere committed Mar 19, 2024
1 parent d3ca71c commit b83e1b8
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.1, 8.2] #
php: [8.1, 8.2, 8.3]
# os: [ubuntu-latest, macOS-latest] # windows-latest,

steps:
Expand Down
17 changes: 17 additions & 0 deletions src/Helper/NumHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Toolkit\Stdlib\Helper;

use Throwable;
use function abs;
use function ceil;
use function floor;
Expand Down Expand Up @@ -73,4 +74,20 @@ public static function roundInt(float|int $value): int
{
return (int)round((float)$value);
}

/**
* @param int $min
* @param int $max
* @param int|null $fallback
*
* @return int
*/
public static function random(int $min, int $max, int $fallback = null): int
{
try {
return random_int($min, $max);
} catch (Throwable) {
return $fallback ?? $min;
}
}
}
83 changes: 83 additions & 0 deletions src/Obj/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types=1);

namespace Toolkit\Stdlib\Obj;

use InvalidArgumentException;

/**
* @author inhere
*/
final class Singleton
{
/**
* @var array<string, object>
*/
private static array $objMap = [];

/**
* @param string $id
* @param object $obj
*/
public static function set(string $id, object $obj): void
{
self::$objMap[$id] = $obj;
}

/**
* get object by id
*
* @param string $id
*
* @return object|null
*/
public static function get(string $id): ?object
{
return self::$objMap[$id] ?? null;
}

/**
* must get the object, or throw an exception
*
* @param string $id
*
* @return object
*/
public static function must(string $id): object
{
return self::$objMap[$id] ?? throw new InvalidArgumentException("object not found: $id");
}

/**
* @param string $id
*
* @return bool
*/
public static function has(string $id): bool
{
return isset(self::$objMap[$id]);
}

/**
* @param string $id
*/
public static function del(string $id): void
{
unset(self::$objMap[$id]);
}

/**
* @return array
*/
public static function all(): array
{
return self::$objMap;
}

/**
* @return void
*/
public static function clear(): void
{
self::$objMap = [];
}
}
14 changes: 6 additions & 8 deletions src/Std/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Collection implements IteratorAggregate, ArrayAccess, Countable, JsonSeria
*
* @return static
*/
public static function new(array $data = []): self
public static function new(array $data = []): static
{
return new static($data);
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public function __destruct()
*
* @return $this
*/
public function set(string $key, mixed $value): self
public function set(string $key, mixed $value): static
{
$this->data[$key] = $value;
return $this;
Expand All @@ -83,7 +83,7 @@ public function set(string $key, mixed $value): self
*
* @return $this
*/
public function add(string $name, $value): self
public function add(string $name, $value): static
{
if (!$this->has($name)) {
$this->set($name, $value);
Expand Down Expand Up @@ -170,7 +170,6 @@ public function getString(string $key, string $default = ''): string
public function getArray(string $key, array $default = []): array
{
$data = $this->get($key);

return $data ? (array)$data : $default;
}

Expand All @@ -183,7 +182,6 @@ public function getArray(string $key, array $default = []): array
public function getDataObject(string $key, array $default = []): DataObject
{
$data = $this->get($key);

return DataObject::new($data ? (array)$data : $default);
}

Expand Down Expand Up @@ -241,7 +239,7 @@ public function gets(array $keys): array
*
* @return static
*/
public function sets(array $data): self
public function sets(array $data): static
{
foreach ($data as $key => $value) {
$this->set($key, $value);
Expand All @@ -255,7 +253,7 @@ public function sets(array $data): self
*
* @return static
*/
public function reject(callable $filter): self
public function reject(callable $filter): static
{
$data = [];

Expand All @@ -275,7 +273,7 @@ public function reject(callable $filter): self
*
* @return static
*/
public function map(callable $callback): self
public function map(callable $callback): static
{
$data = [];
foreach ($this->getIterator() as $key => $value) {
Expand Down
19 changes: 6 additions & 13 deletions src/Str/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,13 @@ public static function renderVars(string $tplCode, array $vars, string $format =
return $tplCode;
}

$fmtVars = Arr::flattenMap($vars, Arr\ArrConst::FLAT_DOT_JOIN_INDEX);
$pattern = sprintf('/%s([\w\s.-]+)%s/', preg_quote($left, '/'), preg_quote($right, '/'));

// convert array value to string.
foreach ($vars as $name => $val) {
if (is_array($val)) {
$fmtVars[$name] = Arr::toStringV2($val);
}
}

return preg_replace_callback($pattern, static function (array $match) use ($fmtVars) {
$var = trim($match[1]);
if ($var && isset($fmtVars[$var])) {
return $fmtVars[$var];
return preg_replace_callback($pattern, static function (array $match) use ($vars) {
if ($var = trim($match[1])) {
$val = Arr::getByPath($vars, $var);
if ($val !== null) {
return is_array($val) ? Arr::toStringV2($val) : (string)$val;
}
}

return $match[0];
Expand Down
62 changes: 49 additions & 13 deletions src/Util/Stream/DataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function map(callable $mapper): static
* @template U
*
* @param callable(T):U $mapper
* @param bool $boolExpr
* @param bool $boolExpr
*
* @return static
*/
Expand All @@ -228,7 +228,7 @@ public function mapIf(callable $mapper, bool $boolExpr): static
* @template U
*
* @param callable(T):U $mapper
* @param DataStream $stream
* @param DataStream $stream
*
* @return static
*/
Expand All @@ -245,7 +245,7 @@ public function mapTo(callable $mapper, self $stream): static
* Mapping values to MapStream
*
* @param callable(array|mixed): array{string,mixed} $mapper
* @param MapStream|null $new
* @param MapStream|null $new
*
* @return MapStream
*/
Expand All @@ -264,7 +264,7 @@ public function mapToMap(callable $mapper, MapStream $new = null): MapStream
* Mapping values to IntStream
*
* @param callable(T):int $mapper
* @param IntStream|null $new
* @param IntStream|null $new
*
* @return IntStream
*/
Expand All @@ -282,7 +282,7 @@ public function mapToInt(callable $mapper, IntStream $new = null): IntStream
* Mapping values to StringStream
*
* @param callable(T):string $mapper
* @param StringStream|null $new
* @param StringStream|null $new
*
* @return StringStream
*/
Expand Down Expand Up @@ -528,7 +528,7 @@ public function forEach(callable $handler): void

/**
* @param callable(array|mixed, int|string): array $func
* @param array $arr
* @param array $arr
*
* @return array
*/
Expand All @@ -544,7 +544,7 @@ public function eachToArray(callable $func, array $arr = []): array
* Each item to map
*
* @param callable(array|mixed): array{string, mixed} $func
* @param array $map
* @param array $map
*
* @return array<string, mixed> return the map key and value
*/
Expand All @@ -559,15 +559,51 @@ public function eachToMap(callable $func, array $map = []): array
}

/**
* @param callable $handler
* @param ...$args
* Collect data items to list
*
* @return mixed
* @param callable(mixed): mixed $func
* @param array $list
*
* @return array return an array: [item, ...]
*/
public function collectToList(callable $func, array $list = []): array
{
foreach ($this as $item) {
$list[] = $func($item);
}
return $list;
}

/**
* Collect data items to map
*
* @param callable $func
* @param array $map
*
* @return array<string, mixed> return a map: [key => item]
*/
public function collectToMap(callable $func, array $map = []): array
{
return $this->eachToMap($func, $map);
}

/**
* Group data by key
*
* @param callable $getKeyFn
*
* @return array
*/
public function collect(callable $handler, ...$args): mixed
public function groupBy(callable $getKeyFn): array
{
// TODO
return null;
$map = [];
foreach ($this as $item) {
$key = $getKeyFn($item);
// collect to map
$map[$key][] = $item;
}

return $map;
}

/**
Expand Down

0 comments on commit b83e1b8

Please sign in to comment.