Store the customized Shopro mall so it can be kept on the remote.
Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
@@ -0,0 +1,550 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
use ArrayAccess;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Most of the methods in this file come from illuminate/support and hyperf/support,
|
||||
* thanks provide such a useful class.
|
||||
*/
|
||||
class Arr
|
||||
{
|
||||
public static function accessible(mixed $value): bool
|
||||
{
|
||||
return is_array($value) || $value instanceof ArrayAccess;
|
||||
}
|
||||
|
||||
public static function add(array $array, string $key, mixed $value): array
|
||||
{
|
||||
if (is_null(static::get($array, $key))) {
|
||||
static::set($array, $key, $value);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function collapse(array $array): array
|
||||
{
|
||||
$results = [];
|
||||
foreach ($array as $values) {
|
||||
if ($values instanceof Collection) {
|
||||
$values = $values->all();
|
||||
} elseif (!is_array($values)) {
|
||||
continue;
|
||||
}
|
||||
$results[] = $values;
|
||||
}
|
||||
|
||||
return array_merge([], ...$results);
|
||||
}
|
||||
|
||||
public static function crossJoin(...$arrays): array
|
||||
{
|
||||
$results = [[]];
|
||||
foreach ($arrays as $index => $array) {
|
||||
$append = [];
|
||||
foreach ($results as $product) {
|
||||
foreach ($array as $item) {
|
||||
$product[$index] = $item;
|
||||
$append[] = $product;
|
||||
}
|
||||
}
|
||||
$results = $append;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function divide(array $array): array
|
||||
{
|
||||
return [array_keys($array), array_values($array)];
|
||||
}
|
||||
|
||||
public static function dot(array $array, string $prepend = ''): array
|
||||
{
|
||||
$results = [];
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value) && !empty($value)) {
|
||||
$results = array_merge($results, static::dot($value, $prepend.$key.'.'));
|
||||
} else {
|
||||
$results[$prepend.$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function except(array $array, array|string $keys): array
|
||||
{
|
||||
static::forget($array, $keys);
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function exists(array|ArrayAccess $array, int|string $key): bool
|
||||
{
|
||||
if ($array instanceof ArrayAccess) {
|
||||
return $array->offsetExists($key);
|
||||
}
|
||||
|
||||
return array_key_exists($key, $array);
|
||||
}
|
||||
|
||||
public static function first(array $array, ?callable $callback = null, mixed $default = null): mixed
|
||||
{
|
||||
if (is_null($callback)) {
|
||||
if (empty($array)) {
|
||||
return $default;
|
||||
}
|
||||
foreach ($array as $item) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
foreach ($array as $key => $value) {
|
||||
if (call_user_func($callback, $value, $key)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public static function last(array $array, ?callable $callback = null, mixed $default = null): mixed
|
||||
{
|
||||
if (is_null($callback)) {
|
||||
return empty($array) ? $default : end($array);
|
||||
}
|
||||
|
||||
return static::first(array_reverse($array, true), $callback, $default);
|
||||
}
|
||||
|
||||
public static function flatten(array $array, float|int $depth = INF): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($array as $item) {
|
||||
$item = $item instanceof Collection ? $item->all() : $item;
|
||||
if (!is_array($item)) {
|
||||
$result[] = $item;
|
||||
} elseif (1 === $depth) {
|
||||
$result = array_merge($result, array_values($item));
|
||||
} else {
|
||||
$result = array_merge($result, static::flatten($item, $depth - 1));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function forget(array &$array, array|string $keys): void
|
||||
{
|
||||
$original = &$array;
|
||||
$keys = (array) $keys;
|
||||
if (0 === count($keys)) {
|
||||
return;
|
||||
}
|
||||
foreach ($keys as $key) {
|
||||
// if the exact key exists in the top-level, remove it
|
||||
if (static::exists($array, $key)) {
|
||||
unset($array[$key]);
|
||||
|
||||
continue;
|
||||
}
|
||||
$parts = explode('.', $key);
|
||||
// clean up before each pass
|
||||
$array = &$original;
|
||||
while (count($parts) > 1) {
|
||||
$part = array_shift($parts);
|
||||
if (isset($array[$part]) && is_array($array[$part])) {
|
||||
$array = &$array[$part];
|
||||
} else {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
unset($array[array_shift($parts)]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function get(array|ArrayAccess $array, null|int|string $key = null, mixed $default = null): mixed
|
||||
{
|
||||
if (!static::accessible($array)) {
|
||||
return $default;
|
||||
}
|
||||
if (is_null($key)) {
|
||||
return $array;
|
||||
}
|
||||
if (static::exists($array, $key)) {
|
||||
return $array[$key];
|
||||
}
|
||||
if (!is_string($key) || !str_contains($key, '.')) {
|
||||
return $array[$key] ?? $default;
|
||||
}
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (static::accessible($array) && static::exists($array, $segment)) {
|
||||
$array = $array[$segment];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function has(array|ArrayAccess $array, null|array|string $keys): bool
|
||||
{
|
||||
if (is_null($keys)) {
|
||||
return false;
|
||||
}
|
||||
$keys = (array) $keys;
|
||||
if (!$array) {
|
||||
return false;
|
||||
}
|
||||
if ([] === $keys) {
|
||||
return false;
|
||||
}
|
||||
foreach ($keys as $key) {
|
||||
$subKeyArray = $array;
|
||||
if (static::exists($array, $key)) {
|
||||
continue;
|
||||
}
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
|
||||
$subKeyArray = $subKeyArray[$segment];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function isAssoc(array $array): bool
|
||||
{
|
||||
$keys = array_keys($array);
|
||||
|
||||
return array_keys($keys) !== $keys;
|
||||
}
|
||||
|
||||
public static function only(array $array, array|string $keys): array
|
||||
{
|
||||
return array_intersect_key($array, array_flip((array) $keys));
|
||||
}
|
||||
|
||||
public static function pluck(array $array, array|string $value, null|array|string $key = null): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach ($array as $item) {
|
||||
$itemValue = data_get($item, $value);
|
||||
// If the key is "null", we will just append the value to the array and keep
|
||||
// looping, Otherwise we will key the array using the value of the key we
|
||||
// received from the developer. Then we'll return the final array form.
|
||||
if (is_null($key)) {
|
||||
$results[] = $itemValue;
|
||||
} else {
|
||||
$itemKey = data_get($item, $key);
|
||||
if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
|
||||
$itemKey = (string) $itemKey;
|
||||
}
|
||||
$results[$itemKey] = $itemValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function prepend(array $array, mixed $value, mixed $key = null): array
|
||||
{
|
||||
if (is_null($key)) {
|
||||
array_unshift($array, $value);
|
||||
} else {
|
||||
$array = [$key => $value] + $array;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function pull(array &$array, string $key, mixed $default = null): mixed
|
||||
{
|
||||
$value = static::get($array, $key, $default);
|
||||
static::forget($array, $key);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function random(array $array, int $number = 1): array
|
||||
{
|
||||
$count = count($array);
|
||||
if ($number > $count) {
|
||||
throw new InvalidArgumentException("You requested {$number} items, but there are only {$count} items available.");
|
||||
}
|
||||
|
||||
if (0 === $number) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$keys = array_rand($array, $number);
|
||||
$results = [];
|
||||
foreach ((array) $keys as $key) {
|
||||
$results[] = $array[$key];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array item to a given value using "dot" notation.
|
||||
* If no key is given to the method, the entire array will be replaced.
|
||||
*/
|
||||
public static function set(array &$array, null|int|string $key, mixed $value): array
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $array = $value;
|
||||
}
|
||||
if (!is_string($key)) {
|
||||
$array[$key] = $value;
|
||||
|
||||
return $array;
|
||||
}
|
||||
$keys = explode('.', $key);
|
||||
while (count($keys) > 1) {
|
||||
$key = array_shift($keys);
|
||||
// If the key doesn't exist at this depth, we will just create an empty array
|
||||
// to hold the next value, allowing us to create the arrays to hold final
|
||||
// values at the correct depth. Then we'll keep digging into the array.
|
||||
if (!isset($array[$key]) || !is_array($array[$key])) {
|
||||
$array[$key] = [];
|
||||
}
|
||||
$array = &$array[$key];
|
||||
}
|
||||
$array[array_shift($keys)] = $value;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function shuffle(array $array, ?int $seed = null): array
|
||||
{
|
||||
if (is_null($seed)) {
|
||||
shuffle($array);
|
||||
} else {
|
||||
srand($seed);
|
||||
usort($array, function () {
|
||||
return rand(-1, 1);
|
||||
});
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function sort(array $array, callable $callback): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
$results[$key] = $callback($value);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function sortRecursive(array $array): array
|
||||
{
|
||||
foreach ($array as &$value) {
|
||||
if (is_array($value)) {
|
||||
$value = static::sortRecursive($value);
|
||||
}
|
||||
}
|
||||
if (static::isAssoc($array)) {
|
||||
ksort($array);
|
||||
} else {
|
||||
sort($array);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function query(array $array, int $encodingType = PHP_QUERY_RFC1738): string
|
||||
{
|
||||
return http_build_query($array, '', '&', $encodingType);
|
||||
}
|
||||
|
||||
public static function toString(array $array, string $separator = '&'): string
|
||||
{
|
||||
$result = '';
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
$result .= $key.'='.$value.$separator;
|
||||
}
|
||||
|
||||
return substr($result, 0, 0 - Str::length($separator));
|
||||
}
|
||||
|
||||
public static function where(array $array, callable $callback): array
|
||||
{
|
||||
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
public static function wrap(mixed $value): array
|
||||
{
|
||||
if (is_null($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return !is_array($value) ? [$value] : $value;
|
||||
}
|
||||
|
||||
public static function wrapJson(string $json): ?array
|
||||
{
|
||||
$result = json_decode($json, true);
|
||||
|
||||
return is_array($result) ? $result : null;
|
||||
}
|
||||
|
||||
public static function wrapXml(string $xml): array
|
||||
{
|
||||
if (empty($xml)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode(json_encode(
|
||||
simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA),
|
||||
JSON_UNESCAPED_UNICODE
|
||||
), true);
|
||||
|
||||
if (JSON_ERROR_NONE === json_last_error()) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $raw 是否原始解析,有些情况下,原始解析会更好
|
||||
* @param bool $spaceToPlus 是否将空格转换为加号
|
||||
*/
|
||||
public static function wrapQuery(string $query, bool $raw = false, bool $spaceToPlus = false): array
|
||||
{
|
||||
if (!$raw) {
|
||||
parse_str($query, $data);
|
||||
|
||||
return $spaceToPlus ? self::querySpaceToPlus($data) : $data;
|
||||
}
|
||||
|
||||
if (empty($query) || !Str::contains($query, '=')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach (explode('&', $query) as $item) {
|
||||
$pos = strpos($item, '=');
|
||||
|
||||
$result[substr($item, 0, $pos)] = substr($item, $pos + 1);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function querySpaceToPlus(array $data): array
|
||||
{
|
||||
foreach ($data as $key => $item) {
|
||||
$data[$key] = is_array($item) ? self::querySpaceToPlus($item) : str_replace(' ', '+', $item);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function unique(array $array): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($array as $key => $item) {
|
||||
if (is_array($item)) {
|
||||
$result[$key] = self::unique($item);
|
||||
} else {
|
||||
$result[$key] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
if (!self::isAssoc($result)) {
|
||||
return array_unique($result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function merge(array $array1, array $array2, bool $unique = true): array
|
||||
{
|
||||
$isAssoc = static::isAssoc($array1 ?: $array2);
|
||||
if ($isAssoc) {
|
||||
foreach ($array2 as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$array1[$key] = static::merge($array1[$key] ?? [], $value, $unique);
|
||||
} else {
|
||||
$array1[$key] = $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($array2 as $value) {
|
||||
if ($unique && in_array($value, $array1, true)) {
|
||||
continue;
|
||||
}
|
||||
$array1[] = $value;
|
||||
}
|
||||
|
||||
$array1 = array_values($array1);
|
||||
}
|
||||
|
||||
return $array1;
|
||||
}
|
||||
|
||||
public static function encoding(array $array, string $to_encoding, string $from_encoding = 'gb2312'): array
|
||||
{
|
||||
$encoded = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
$encoded[$key] = is_array($value) ? self::encoding($value, $to_encoding, $from_encoding) :
|
||||
mb_convert_encoding($value, $to_encoding, $from_encoding);
|
||||
}
|
||||
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
public static function camelCaseKey(mixed $data): mixed
|
||||
{
|
||||
if (!self::accessible($data)
|
||||
&& !(is_object($data) && method_exists($data, 'toArray'))) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ((is_object($data) ? $data->toArray() : $data) as $key => $value) {
|
||||
$result[is_string($key) ? Str::camel($key) : $key] = self::camelCaseKey($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function snakeCaseKey(mixed $data): mixed
|
||||
{
|
||||
if (!self::accessible($data)
|
||||
&& !(is_object($data) && method_exists($data, 'toArray'))) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ((is_object($data) ? $data->toArray() : $data) as $key => $value) {
|
||||
$result[is_string($key) ? Str::snake($key) : $key] = self::snakeCaseKey($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
use Traversable;
|
||||
use Yansongda\Supports\Traits\Accessable;
|
||||
use Yansongda\Supports\Traits\Arrayable;
|
||||
use Yansongda\Supports\Traits\Serializable;
|
||||
|
||||
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
{
|
||||
use Serializable;
|
||||
use Accessable;
|
||||
use Arrayable;
|
||||
|
||||
protected array $items = [];
|
||||
|
||||
public function __construct(mixed $items = [])
|
||||
{
|
||||
foreach ($this->getArrayableItems($items) as $key => $value) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public static function wrap(mixed $value): self
|
||||
{
|
||||
return $value instanceof self ? new static($value) : new static(Arr::wrap($value));
|
||||
}
|
||||
|
||||
public static function wrapJson(string $json): self
|
||||
{
|
||||
return new static(Arr::wrapJson($json));
|
||||
}
|
||||
|
||||
public static function wrapXml(string $xml): self
|
||||
{
|
||||
return new static(Arr::wrapXml($xml));
|
||||
}
|
||||
|
||||
public static function wrapQuery(string $query, bool $raw = false, bool $spaceToPlus = false): self
|
||||
{
|
||||
return new static(Arr::wrapQuery($query, $raw, $spaceToPlus));
|
||||
}
|
||||
|
||||
public static function unwrap(array|Collection $value): array
|
||||
{
|
||||
return $value instanceof self ? $value->all() : $value;
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function only(array $keys): array
|
||||
{
|
||||
$return = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$value = $this->get($key);
|
||||
|
||||
if (!is_null($value)) {
|
||||
$return[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function except(mixed $keys): self
|
||||
{
|
||||
$keys = is_array($keys) ? $keys : func_get_args();
|
||||
|
||||
return new static(Arr::except($this->items, $keys));
|
||||
}
|
||||
|
||||
public function filter(?callable $callback = null): self
|
||||
{
|
||||
if ($callback) {
|
||||
return new static(Arr::where($this->items, $callback));
|
||||
}
|
||||
|
||||
return new static(array_filter($this->items));
|
||||
}
|
||||
|
||||
public function merge(mixed $items): self
|
||||
{
|
||||
return new static(array_merge($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
public function has(int|string $key): bool
|
||||
{
|
||||
return !is_null(Arr::get($this->items, $key));
|
||||
}
|
||||
|
||||
public function first(): mixed
|
||||
{
|
||||
return reset($this->items);
|
||||
}
|
||||
|
||||
public function last(): mixed
|
||||
{
|
||||
$end = end($this->items);
|
||||
|
||||
reset($this->items);
|
||||
|
||||
return $end;
|
||||
}
|
||||
|
||||
public function add(null|int|string $key, mixed $value): void
|
||||
{
|
||||
Arr::set($this->items, $key, $value);
|
||||
}
|
||||
|
||||
public function set(null|int|string $key, mixed $value): void
|
||||
{
|
||||
Arr::set($this->items, $key, $value);
|
||||
}
|
||||
|
||||
public function get(null|int|string $key = null, mixed $default = null): mixed
|
||||
{
|
||||
return Arr::get($this->items, $key, $default);
|
||||
}
|
||||
|
||||
public function forget(int|string $key): void
|
||||
{
|
||||
Arr::forget($this->items, $key);
|
||||
}
|
||||
|
||||
public function flatten(float|int $depth = INF): self
|
||||
{
|
||||
return new static(Arr::flatten($this->items, $depth));
|
||||
}
|
||||
|
||||
public function map(callable $callback): self
|
||||
{
|
||||
$keys = array_keys($this->items);
|
||||
$items = array_map($callback, $this->items, $keys);
|
||||
|
||||
return new static(array_combine($keys, $items));
|
||||
}
|
||||
|
||||
public function pop(): mixed
|
||||
{
|
||||
return array_pop($this->items);
|
||||
}
|
||||
|
||||
public function prepend(mixed $value, mixed $key = null): self
|
||||
{
|
||||
$this->items = Arr::prepend($this->items, $value, $key);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function push(mixed $value): self
|
||||
{
|
||||
$this->offsetSet(null, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function pull(mixed $key, mixed $default = null): mixed
|
||||
{
|
||||
return Arr::pull($this->items, $key, $default);
|
||||
}
|
||||
|
||||
public function put(mixed $key, mixed $value): self
|
||||
{
|
||||
$this->offsetSet($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function random(?int $number = null): self
|
||||
{
|
||||
return new static(Arr::random($this->items, $number ?? 1));
|
||||
}
|
||||
|
||||
public function reduce(callable $callback, mixed $initial = null): mixed
|
||||
{
|
||||
return array_reduce($this->items, $callback, $initial);
|
||||
}
|
||||
|
||||
public function values(): self
|
||||
{
|
||||
return new static(array_values($this->items));
|
||||
}
|
||||
|
||||
public function every(callable|string $key): bool
|
||||
{
|
||||
$callback = $this->valueRetriever($key);
|
||||
|
||||
foreach ($this->items as $k => $v) {
|
||||
if (!$callback($v, $k)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function chunk(int $size): self
|
||||
{
|
||||
if ($size <= 0) {
|
||||
return new static();
|
||||
}
|
||||
$chunks = [];
|
||||
foreach (array_chunk($this->items, $size, true) as $chunk) {
|
||||
$chunks[] = new static($chunk);
|
||||
}
|
||||
|
||||
return new static($chunks);
|
||||
}
|
||||
|
||||
public function sort(?callable $callback = null): self
|
||||
{
|
||||
$items = $this->items;
|
||||
$callback ? uasort($items, $callback) : asort($items);
|
||||
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
public function sortBy(callable|string $callback, int $options = SORT_REGULAR, bool $descending = false): self
|
||||
{
|
||||
$results = [];
|
||||
$callback = $this->valueRetriever($callback);
|
||||
// First we will loop through the items and get the comparator from a callback
|
||||
// function which we were given. Then, we will sort the returned values
|
||||
// and grab the corresponding values for the sorted keys from this array.
|
||||
foreach ($this->items as $key => $value) {
|
||||
$results[$key] = $callback($value, $key);
|
||||
}
|
||||
$descending ? arsort($results, $options) : asort($results, $options);
|
||||
// Once we have sorted all the keys in the array, we will loop through them
|
||||
// and grab the corresponding model, so we can set the underlying items list
|
||||
// to the sorted version. Then we'll just return the collection instance.
|
||||
foreach (array_keys($results) as $key) {
|
||||
$results[$key] = $this->items[$key];
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
}
|
||||
|
||||
public function sortByDesc(callable|string $callback, int $options = SORT_REGULAR): self
|
||||
{
|
||||
return $this->sortBy($callback, $options, true);
|
||||
}
|
||||
|
||||
public function sortKeys(int $options = SORT_REGULAR, bool $descending = false): self
|
||||
{
|
||||
$items = $this->items;
|
||||
$descending ? krsort($items, $options) : ksort($items, $options);
|
||||
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
public function sortKeysDesc(int $options = SORT_REGULAR): self
|
||||
{
|
||||
return $this->sortKeys($options, true);
|
||||
}
|
||||
|
||||
public function query(int $encodingType = PHP_QUERY_RFC1738): string
|
||||
{
|
||||
return Arr::query($this->all(), $encodingType);
|
||||
}
|
||||
|
||||
public function toString(string $separator = '&'): string
|
||||
{
|
||||
return Arr::toString($this->all(), $separator);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->all();
|
||||
}
|
||||
|
||||
public function toXml(): string
|
||||
{
|
||||
$xml = '<xml>';
|
||||
|
||||
foreach ($this->all() as $key => $val) {
|
||||
$xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
|
||||
'<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
|
||||
}
|
||||
|
||||
$xml .= '</xml>';
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
return new ArrayIterator($this->items);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return empty($this->items);
|
||||
}
|
||||
|
||||
public function isNotEmpty(): bool
|
||||
{
|
||||
return !$this->isEmpty();
|
||||
}
|
||||
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
$this->forget($offset);
|
||||
}
|
||||
}
|
||||
|
||||
protected function useAsCallable(mixed $value): bool
|
||||
{
|
||||
return !is_string($value) && is_callable($value);
|
||||
}
|
||||
|
||||
protected function valueRetriever(mixed $value): callable
|
||||
{
|
||||
if ($this->useAsCallable($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return function ($item) use ($value) {
|
||||
return data_get($item, $value);
|
||||
};
|
||||
}
|
||||
|
||||
protected function getArrayableItems(mixed $items): array
|
||||
{
|
||||
if (is_array($items)) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
if ($items instanceof self) {
|
||||
return $items->all();
|
||||
}
|
||||
|
||||
if ($items instanceof JsonSerializable) {
|
||||
return $items->jsonSerialize();
|
||||
}
|
||||
|
||||
return (array) $items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
class Config extends Collection {}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
use Closure;
|
||||
|
||||
if (!function_exists('collect')) {
|
||||
function collect(array $value = []): Collection
|
||||
{
|
||||
return new Collection($value);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('value')) {
|
||||
function value(mixed $value): mixed
|
||||
{
|
||||
return $value instanceof Closure ? $value() : $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('data_get')) {
|
||||
function data_get(mixed $target, null|array|int|string $key, mixed $default = null): mixed
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $target;
|
||||
}
|
||||
|
||||
$key = is_array($key) ? $key : explode('.', is_int($key) ? (string) $key : $key);
|
||||
|
||||
while (!is_null($segment = array_shift($key))) {
|
||||
if ('*' === $segment) {
|
||||
if ($target instanceof Collection) {
|
||||
$target = $target->all();
|
||||
} elseif (!is_array($target)) {
|
||||
return value($default);
|
||||
}
|
||||
$result = [];
|
||||
foreach ($target as $item) {
|
||||
$result[] = data_get($item, $key);
|
||||
}
|
||||
|
||||
return in_array('*', $key) ? Arr::collapse($result) : $result;
|
||||
}
|
||||
if (Arr::accessible($target) && Arr::exists($target, $segment)) {
|
||||
$target = $target[$segment];
|
||||
} elseif (is_object($target) && isset($target->{$segment})) {
|
||||
$target = $target->{$segment};
|
||||
} else {
|
||||
return value($default);
|
||||
}
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
use Closure;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* This file mostly code come from illuminate/pipe and hyperf/utils,
|
||||
* thanks provide such a useful class.
|
||||
*/
|
||||
class Pipeline
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected mixed $passable;
|
||||
|
||||
protected array $pipes = [];
|
||||
|
||||
protected string $method = 'handle';
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function send(mixed $passable): self
|
||||
{
|
||||
$this->passable = $passable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function through(mixed $pipes): self
|
||||
{
|
||||
$this->pipes = is_array($pipes) ? $pipes : func_get_args();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the method to call on the pipes.
|
||||
*/
|
||||
public function via(string $method): self
|
||||
{
|
||||
$this->method = $method;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function then(Closure $destination): mixed
|
||||
{
|
||||
$pipeline = array_reduce(array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination));
|
||||
|
||||
return $pipeline($this->passable);
|
||||
}
|
||||
|
||||
protected function prepareDestination(Closure $destination): Closure
|
||||
{
|
||||
return static function ($passable) use ($destination) {
|
||||
return $destination($passable);
|
||||
};
|
||||
}
|
||||
|
||||
protected function carry(): Closure
|
||||
{
|
||||
return function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if (is_callable($pipe)) {
|
||||
// If the pipe is an instance of a Closure, we will just call it directly, but
|
||||
// otherwise we'll resolve the pipes out of the container and call it with
|
||||
// the appropriate method and arguments, returning the results back out.
|
||||
return $pipe($passable, $stack);
|
||||
}
|
||||
if (!is_object($pipe)) {
|
||||
[$name, $parameters] = $this->parsePipeString($pipe);
|
||||
|
||||
// If the pipe is a string we will parse the string and resolve the class out
|
||||
// of the dependency injection container. We can then build a callable and
|
||||
// execute the pipe function giving in the parameters that are required.
|
||||
$pipe = $this->container->get($name);
|
||||
|
||||
$parameters = array_merge([$passable, $stack], $parameters);
|
||||
} else {
|
||||
// If the pipe is already an object we'll just make a callable and pass it to
|
||||
// the pipe as-is. There is no need to do any extra parsing and formatting
|
||||
// since the object we're given was already a fully instantiated object.
|
||||
$parameters = [$passable, $stack];
|
||||
}
|
||||
|
||||
$carry = method_exists($pipe, $this->method) ? $pipe->{$this->method}(...$parameters) : $pipe(...$parameters);
|
||||
|
||||
return $this->handleCarry($carry);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
protected function parsePipeString(string $pipe): array
|
||||
{
|
||||
[$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []);
|
||||
|
||||
if (is_string($parameters)) {
|
||||
$parameters = explode(',', $parameters);
|
||||
}
|
||||
|
||||
return [$name, $parameters];
|
||||
}
|
||||
|
||||
protected function handleCarry(mixed $carry): mixed
|
||||
{
|
||||
return $carry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
/**
|
||||
* Most of the methods in this file come from illuminate/support.
|
||||
* thanks provide such a useful class.
|
||||
*/
|
||||
class Str
|
||||
{
|
||||
public static function after(string $subject, string $search): string
|
||||
{
|
||||
return '' === $search ? $subject : array_reverse(explode($search, $subject, 2))[0];
|
||||
}
|
||||
|
||||
public static function ascii(string $value, string $language = 'en'): string
|
||||
{
|
||||
$languageSpecific = static::languageSpecificCharsArray($language);
|
||||
|
||||
if (!is_null($languageSpecific)) {
|
||||
$value = str_replace($languageSpecific[0], $languageSpecific[1], $value);
|
||||
}
|
||||
|
||||
foreach (static::charsArray() as $key => $val) {
|
||||
$value = str_replace($val, $key, $value);
|
||||
}
|
||||
|
||||
return preg_replace('/[^\x20-\x7E]/u', '', $value);
|
||||
}
|
||||
|
||||
public static function before(string $subject, string $search): string
|
||||
{
|
||||
return '' === $search ? $subject : explode($search, $subject)[0];
|
||||
}
|
||||
|
||||
public static function camel(string $value): string
|
||||
{
|
||||
return lcfirst(static::studly($value));
|
||||
}
|
||||
|
||||
public static function contains(string $haystack, array|string $needles): bool
|
||||
{
|
||||
foreach ((array) $needles as $needle) {
|
||||
if (str_contains($haystack, $needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function endsWith(string $haystack, array|string $needles): bool
|
||||
{
|
||||
foreach ((array) $needles as $needle) {
|
||||
if (str_ends_with($haystack, $needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function finish(string $value, string $cap): string
|
||||
{
|
||||
$quoted = preg_quote($cap, '/');
|
||||
|
||||
return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
|
||||
}
|
||||
|
||||
public static function is(array|string $pattern, string $value): bool
|
||||
{
|
||||
$patterns = Arr::wrap($pattern);
|
||||
|
||||
if (empty($patterns)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
// If the given value is an exact match we can of course return true right
|
||||
// from the beginning. Otherwise, we will translate asterisks and do an
|
||||
// actual pattern match against the two strings to see if they match.
|
||||
if ($pattern == $value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$pattern = preg_quote($pattern, '#');
|
||||
|
||||
// Asterisks are translated into zero-or-more regular expression wildcards
|
||||
// to make it convenient to check if the strings starts with the given
|
||||
// pattern such as "library/*", making any string check convenient.
|
||||
$pattern = str_replace('\*', '.*', $pattern);
|
||||
|
||||
if (1 === preg_match('#^'.$pattern.'\z#u', $value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function kebab(string $value): string
|
||||
{
|
||||
return static::snake($value, '-');
|
||||
}
|
||||
|
||||
public static function length(string $value, ?string $encoding = null): int
|
||||
{
|
||||
if (null !== $encoding) {
|
||||
return mb_strlen($value, $encoding);
|
||||
}
|
||||
|
||||
return mb_strlen($value);
|
||||
}
|
||||
|
||||
public static function limit(string $value, int $limit = 100, string $end = '...'): string
|
||||
{
|
||||
if (mb_strwidth($value, 'UTF-8') <= $limit) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
|
||||
}
|
||||
|
||||
public static function lower(string $value): string
|
||||
{
|
||||
return mb_strtolower($value, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function words(string $value, int $words = 100, string $end = '...'): string
|
||||
{
|
||||
preg_match('/^\s*+\S++\s*+{1,'.$words.'}/u', $value, $matches);
|
||||
|
||||
if (!isset($matches[0]) || static::length($value) === static::length($matches[0])) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return rtrim($matches[0]).$end;
|
||||
}
|
||||
|
||||
public static function parseCallback(string $callback, ?string $default = null): array
|
||||
{
|
||||
return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
|
||||
}
|
||||
|
||||
public static function random(int $length = 16): string
|
||||
{
|
||||
$string = '';
|
||||
|
||||
while (($len = strlen($string)) < $length) {
|
||||
$size = $length - $len;
|
||||
|
||||
$bytes = random_bytes($size);
|
||||
|
||||
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function uuidV4(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
// 32 bits for "time_low"
|
||||
mt_rand(0, 0xFFFF),
|
||||
mt_rand(0, 0xFFFF),
|
||||
|
||||
// 16 bits for "time_mid"
|
||||
mt_rand(0, 0xFFFF),
|
||||
|
||||
// 16 bits for "time_hi_and_version",
|
||||
// four most significant bits holds version number 4
|
||||
mt_rand(0, 0x0FFF) | 0x4000,
|
||||
|
||||
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||||
// 8 bits for "clk_seq_low",
|
||||
// two most significant bits holds zero and one for variant DCE1.1
|
||||
mt_rand(0, 0x3FFF) | 0x8000,
|
||||
|
||||
// 48 bits for "node"
|
||||
mt_rand(0, 0xFFFF),
|
||||
mt_rand(0, 0xFFFF),
|
||||
mt_rand(0, 0xFFFF)
|
||||
);
|
||||
}
|
||||
|
||||
public static function replaceArray(string $search, array $replace, string $subject): string
|
||||
{
|
||||
foreach ($replace as $value) {
|
||||
$subject = static::replaceFirst($search, $value, $subject);
|
||||
}
|
||||
|
||||
return $subject;
|
||||
}
|
||||
|
||||
public static function replaceFirst(string $search, string $replace, string $subject): string
|
||||
{
|
||||
if ('' == $search) {
|
||||
return $subject;
|
||||
}
|
||||
|
||||
$position = strpos($subject, $search);
|
||||
|
||||
if (false !== $position) {
|
||||
return substr_replace($subject, $replace, $position, strlen($search));
|
||||
}
|
||||
|
||||
return $subject;
|
||||
}
|
||||
|
||||
public static function replaceLast(string $search, string $replace, string $subject): string
|
||||
{
|
||||
$position = strrpos($subject, $search);
|
||||
|
||||
if (false !== $position) {
|
||||
return substr_replace($subject, $replace, $position, strlen($search));
|
||||
}
|
||||
|
||||
return $subject;
|
||||
}
|
||||
|
||||
public static function start(string $value, string $prefix): string
|
||||
{
|
||||
$quoted = preg_quote($prefix, '/');
|
||||
|
||||
return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
|
||||
}
|
||||
|
||||
public static function upper(string $value): string
|
||||
{
|
||||
return mb_strtoupper($value, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function title(string $value): string
|
||||
{
|
||||
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function slug(string $title, string $separator = '-', string $language = 'en'): string
|
||||
{
|
||||
$title = static::ascii($title, $language);
|
||||
|
||||
// Convert all dashes/underscores into separator
|
||||
$flip = '-' == $separator ? '_' : '-';
|
||||
|
||||
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
|
||||
|
||||
// Replace @ with the word 'at'
|
||||
$title = str_replace('@', $separator.'at'.$separator, $title);
|
||||
|
||||
// Remove all characters that are not the separator, letters, numbers, or whitespace.
|
||||
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
|
||||
|
||||
// Replace all separator characters and whitespace by a single separator
|
||||
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
|
||||
|
||||
return trim($title, $separator);
|
||||
}
|
||||
|
||||
public static function snake(string $value, string $delimiter = '_'): string
|
||||
{
|
||||
if (!ctype_lower($value)) {
|
||||
$value = preg_replace('/\s+/u', '', ucwords($value));
|
||||
|
||||
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function startsWith(int|string $haystack, array|string $needles): bool
|
||||
{
|
||||
foreach ((array) $needles as $needle) {
|
||||
if (str_starts_with(strval($haystack), $needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function studly(string $value, string $gap = ''): string
|
||||
{
|
||||
$value = ucwords(str_replace(['-', '_'], ' ', $value));
|
||||
|
||||
return str_replace(' ', $gap, $value);
|
||||
}
|
||||
|
||||
public static function substr(string $string, int $start, ?int $length = null): string
|
||||
{
|
||||
return mb_substr($string, $start, $length, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function ucfirst(string $string): string
|
||||
{
|
||||
return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
|
||||
}
|
||||
|
||||
public static function encoding(string $string, string $to = 'utf-8', string $from = 'gb2312'): string
|
||||
{
|
||||
return mb_convert_encoding($string, $to, $from);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
|
||||
*/
|
||||
protected static function charsArray(): array
|
||||
{
|
||||
static $charsArray;
|
||||
|
||||
if (isset($charsArray)) {
|
||||
return $charsArray;
|
||||
}
|
||||
|
||||
return $charsArray = [
|
||||
'0' => ['°', '₀', '۰', '0'],
|
||||
'1' => ['¹', '₁', '۱', '1'],
|
||||
'2' => ['²', '₂', '۲', '2'],
|
||||
'3' => ['³', '₃', '۳', '3'],
|
||||
'4' => ['⁴', '₄', '۴', '٤', '4'],
|
||||
'5' => ['⁵', '₅', '۵', '٥', '5'],
|
||||
'6' => ['⁶', '₆', '۶', '٦', '6'],
|
||||
'7' => ['⁷', '₇', '۷', '7'],
|
||||
'8' => ['⁸', '₈', '۸', '8'],
|
||||
'9' => ['⁹', '₉', '۹', '9'],
|
||||
'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', 'a', 'ä'],
|
||||
'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'],
|
||||
'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'],
|
||||
'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'],
|
||||
'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', 'e'],
|
||||
'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'],
|
||||
'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'],
|
||||
'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'],
|
||||
'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ', 'ی', 'i'],
|
||||
'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'],
|
||||
'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'],
|
||||
'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'],
|
||||
'm' => ['м', 'μ', 'م', 'မ', 'მ', 'm'],
|
||||
'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'],
|
||||
'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', 'o', 'ö'],
|
||||
'p' => ['п', 'π', 'ပ', 'პ', 'پ', 'p'],
|
||||
'q' => ['ყ', 'q'],
|
||||
'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'],
|
||||
's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'],
|
||||
't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'],
|
||||
'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', 'u', 'ў', 'ü'],
|
||||
'v' => ['в', 'ვ', 'ϐ', 'v'],
|
||||
'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'],
|
||||
'x' => ['χ', 'ξ', 'x'],
|
||||
'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'],
|
||||
'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'],
|
||||
'aa' => ['ع', 'आ', 'آ'],
|
||||
'ae' => ['æ', 'ǽ'],
|
||||
'ai' => ['ऐ'],
|
||||
'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
|
||||
'dj' => ['ђ', 'đ'],
|
||||
'dz' => ['џ', 'ძ'],
|
||||
'ei' => ['ऍ'],
|
||||
'gh' => ['غ', 'ღ'],
|
||||
'ii' => ['ई'],
|
||||
'ij' => ['ij'],
|
||||
'kh' => ['х', 'خ', 'ხ'],
|
||||
'lj' => ['љ'],
|
||||
'nj' => ['њ'],
|
||||
'oe' => ['ö', 'œ', 'ؤ'],
|
||||
'oi' => ['ऑ'],
|
||||
'oii' => ['ऒ'],
|
||||
'ps' => ['ψ'],
|
||||
'sh' => ['ш', 'შ', 'ش'],
|
||||
'shch' => ['щ'],
|
||||
'ss' => ['ß'],
|
||||
'sx' => ['ŝ'],
|
||||
'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
|
||||
'ts' => ['ц', 'ც', 'წ'],
|
||||
'ue' => ['ü'],
|
||||
'uu' => ['ऊ'],
|
||||
'ya' => ['я'],
|
||||
'yu' => ['ю'],
|
||||
'zh' => ['ж', 'ჟ', 'ژ'],
|
||||
'(c)' => ['©'],
|
||||
'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ǻ', 'Ǎ', 'A', 'Ä'],
|
||||
'B' => ['Б', 'Β', 'ब', 'B'],
|
||||
'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ', 'C'],
|
||||
'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ', 'D'],
|
||||
'E' => ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', 'Э', 'Є', 'Ə', 'E'],
|
||||
'F' => ['Ф', 'Φ', 'F'],
|
||||
'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', 'G'],
|
||||
'H' => ['Η', 'Ή', 'Ħ', 'H'],
|
||||
'I' => ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', 'І', 'Ї', 'Ǐ', 'ϒ', 'I'],
|
||||
'J' => ['J'],
|
||||
'K' => ['К', 'Κ', 'K'],
|
||||
'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', 'L'],
|
||||
'M' => ['М', 'Μ', 'M'],
|
||||
'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', 'N'],
|
||||
'O' => ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О', 'Θ', 'Ө', 'Ǒ', 'Ǿ', 'O', 'Ö'],
|
||||
'P' => ['П', 'Π', 'P'],
|
||||
'Q' => ['Q'],
|
||||
'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', 'R'],
|
||||
'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ', 'S'],
|
||||
'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', 'T'],
|
||||
'U' => ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У', 'Ǔ', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', 'U', 'Ў', 'Ü'],
|
||||
'V' => ['В', 'V'],
|
||||
'W' => ['Ω', 'Ώ', 'Ŵ', 'W'],
|
||||
'X' => ['Χ', 'Ξ', 'X'],
|
||||
'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', 'Y'],
|
||||
'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', 'Z'],
|
||||
'AE' => ['Æ', 'Ǽ'],
|
||||
'Ch' => ['Ч'],
|
||||
'Dj' => ['Ђ'],
|
||||
'Dz' => ['Џ'],
|
||||
'Gx' => ['Ĝ'],
|
||||
'Hx' => ['Ĥ'],
|
||||
'Ij' => ['IJ'],
|
||||
'Jx' => ['Ĵ'],
|
||||
'Kh' => ['Х'],
|
||||
'Lj' => ['Љ'],
|
||||
'Nj' => ['Њ'],
|
||||
'Oe' => ['Œ'],
|
||||
'Ps' => ['Ψ'],
|
||||
'Sh' => ['Ш'],
|
||||
'Shch' => ['Щ'],
|
||||
'Ss' => ['ẞ'],
|
||||
'Th' => ['Þ'],
|
||||
'Ts' => ['Ц'],
|
||||
'Ya' => ['Я'],
|
||||
'Yu' => ['Ю'],
|
||||
'Zh' => ['Ж'],
|
||||
' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", "\xEF\xBE\xA0"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
|
||||
*/
|
||||
protected static function languageSpecificCharsArray(string $language): ?array
|
||||
{
|
||||
static $languageSpecific;
|
||||
if (!isset($languageSpecific)) {
|
||||
$languageSpecific = [
|
||||
'bg' => [
|
||||
['х', 'Х', 'щ', 'Щ', 'ъ', 'Ъ', 'ь', 'Ь'],
|
||||
['h', 'H', 'sht', 'SHT', 'a', 'А', 'y', 'Y'],
|
||||
],
|
||||
'de' => [
|
||||
['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'],
|
||||
['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $languageSpecific[$language] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
use Yansongda\Supports\Str;
|
||||
|
||||
trait Accessable
|
||||
{
|
||||
public function __get(string $key): mixed
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return !is_null($this->get($key));
|
||||
}
|
||||
|
||||
public function __unset(string $key): void
|
||||
{
|
||||
$this->offsetUnset($key);
|
||||
}
|
||||
|
||||
public function __set(string $key, mixed $value): void
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
public function get(?string $key = null, mixed $default = null): mixed
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return method_exists($this, 'toArray') ? $this->toArray() : $default;
|
||||
}
|
||||
|
||||
$method = 'get'.Str::studly($key);
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->{$method}();
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function set(string $key, mixed $value): self
|
||||
{
|
||||
$method = 'set'.Str::studly($key);
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return !is_null($this->get($offset));
|
||||
}
|
||||
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
public function offsetUnset(mixed $offset): void {}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
use ReflectionClass;
|
||||
use Yansongda\Supports\Str;
|
||||
|
||||
trait Arrayable
|
||||
{
|
||||
public function toArray(): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ((new ReflectionClass($this))->getProperties() as $item) {
|
||||
$k = $item->getName();
|
||||
$method = 'get'.Str::studly($k);
|
||||
|
||||
$result[Str::snake($k)] = method_exists($this, $method) ? $this->{$method}() : $this->{$k};
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
trait Serializable
|
||||
{
|
||||
public function __serialize(): array
|
||||
{
|
||||
if (method_exists($this, 'toArray')) {
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
$this->unserializeArray($data);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
public function serialize(): ?string
|
||||
{
|
||||
return serialize($this);
|
||||
}
|
||||
|
||||
public function unserialize(string $data): void
|
||||
{
|
||||
unserialize($data);
|
||||
}
|
||||
|
||||
public function toJson(int $option = JSON_UNESCAPED_UNICODE): string
|
||||
{
|
||||
return json_encode($this->__serialize(), $option);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return $this->__serialize();
|
||||
}
|
||||
|
||||
public function unserializeArray(array $data): self
|
||||
{
|
||||
foreach ($data as $key => $item) {
|
||||
if (method_exists($this, 'set')) {
|
||||
$this->set(strval($key), $item);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户