初始化项目

This commit is contained in:
2025-01-11 14:16:20 +08:00
parent 1ed53bf65b
commit cf64159069
11206 changed files with 1446622 additions and 0 deletions

21
vendor/dragon-code/pretty-array/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Andrey Helldar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,421 @@
# Pretty Array
<img src="https://preview.dragon-code.pro/TheDragonCode/pretty-array.svg?brand=php" alt="Pretty Array"/>
[![Stable Version][badge_stable]][link_packagist]
[![Unstable Version][badge_unstable]][link_packagist]
[![Total Downloads][badge_downloads]][link_packagist]
[![Github Workflow Status][badge_build]][link_build]
[![License][badge_license]][link_license]
> Simple conversion of an array to a pretty view.
## Installation
To get the latest version of `Pretty Array` package, simply require the project using [Composer](https://getcomposer.org):
```
composer require dragon-code/pretty-array
```
Instead, you may of course manually update your `require` block and run `composer update` if you so choose:
```json
{
"require": {
"dragon-code/pretty-array": "^4.0"
}
}
```
## Introduction
> Q: Why did you create this package when there is a cooler [symfony/var-exporter](https://github.com/symfony/var-exporter)?
The big minus of package [symfony/var-exporter](https://github.com/symfony/var-exporter) is that it works differently with numeric keys.
For example, we have an array:
```php
$array = [
100 => 'foo',
200 => 'bar',
201 => 'baz',
202 => 'qwe',
205 => 'ert',
206 => 'tyu'
];
```
When exporting through it, the file will contain the following content:
```php
$array = [
100 => 'foo',
200 => 'bar',
'baz',
'qwe',
205 => 'ert',
'tyu'
];
```
> Q: Why do you think this is bad?
This package has a framework-independent base. However, it was originally developed as an assistant for
the [Laravel Lang: HTTP Statuses](https://github.com/Laravel-Lang/http-statuses)
package.
This package allows you to publish language translations of the HTTP Status Codes for the Laravel and Lumen frameworks.
A feature of the framework is that IDEs that help with development do not know how to read the numeric keys of arrays of translation files, so it was necessary to translate
theminto a text equivalent.
This behavior includes [http-statuses.php](https://github.com/Laravel-Lang/http-statuses/blob/main/source/http-statuses.php) file:
```php
<?php
return [
'unknownError' => 'Unknown Error',
'0' => 'Unknown Error',
'100' => 'Continue',
'101' => 'Switching Protocols',
'102' => 'Processing',
'200' => 'OK',
'201' => 'Created',
'202' => 'Accepted',
'203' => 'Non-Authoritative Information',
'204' => 'No Content',
'205' => 'Reset Content',
'206' => 'Partial Content',
'207' => 'Multi-Status',
'208' => 'Already Reported',
'226' => 'IM Used',
// ...
```
The peculiarity of the package is that it takes the values of the source file and combines it with what is already in the application. Thus, the output is a file with numeric keys
that IDE helpers cannot read:
```php
<?php
return [
'unknownError' => 'Unknown Error',
0 => 'Unknown Error',
100 => 'Continue',
'Switching Protocols',
'Processing',
200 => 'OK',
'201' => 'Created',
'Accepted',
'Non-Authoritative Information',
'No Content',
'Reset Content',
'Partial Content',
'Multi-Status',
'Already Reported',
226 => 'IM Used',
// ...
```
## Using
Source array for all examples:
```php
$array = array (
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => array (
0 => 'qwe',
'1' => 'rty',
'asd' => 'zxc',
),
'asdfgh' => array (
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
),
2 => 'iop',
);
```
### Saving numeric keys without alignment
```php
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
return $service->raw($array);
```
Result:
```text
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
0 => 'qwe',
1 => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
],
2 => 'iop',
]
```
### Saving string keys without alignment
```php
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$service->setKeyAsString();
return $service->raw($array);
```
Result:
```text
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
'0' => 'qwe',
'1' => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
'2' => 'rty',
'qawsed' => 'zxc',
],
'2' => 'iop',
]
```
### Saving numeric keys with alignment
```php
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$service->setEqualsAlign();
return $service->raw($array);
```
Result:
```text
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
0 => 'qwe',
1 => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
],
2 => 'iop',
]
```
### Saving string keys with alignment
```php
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$service->setKeyAsString();
$service->setEqualsAlign();
return $service->raw($array);
```
Result:
```text
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
'0' => 'qwe',
'1' => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
'2' => 'rty',
'qawsed' => 'zxc',
],
'2' => 'iop',
]
```
### Saving simple array
```php
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$service->setSimple();
return $service->raw($array);
```
Result:
```text
[
1,
2,
3,
'qaz',
[
'qwe',
'rty',
'zxc',
],
[
'qwe',
'rty',
'zxc',
],
'iop',
]
```
### Change key case
```php
use DragonCode\Contracts\Pretty\Arr\Caseable;
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$service->setCase(Caseable::PASCAL_CASE);
return $service->raw($array);
```
Result:
```text
[
'Foo' => 1,
'Bar' => 2,
'Baz' => 3,
'QweRty' => 'qaz',
'Baq' => [
0 => 'qwe',
1 => 'rty',
'Asd' => 'zxc',
],
'AsdFgh' => [
'FooBarBaz' => 'qwe',
2 => 'rty',
'QawSed' => 'zxc',
],
2 => 'iop',
]
```
The following options are available:
* camelCase (`DragonCode\Contracts\Pretty\Arr\Caseable::CAMEL_CASE`);
* kebab-case (`DragonCode\Contracts\Pretty\Arr\Caseable::KEBAB_CASE`);
* PascalCase (`DragonCode\Contracts\Pretty\Arr\Caseable::PASCAL_CASE`);
* snake_case (`DragonCode\Contracts\Pretty\Arr\Caseable::SNAKE_CASE`);
* no case (`DragonCode\Contracts\Pretty\Arr\Caseable::NO_CASE`). By default;
`NO_CASE` means that key register processing will not be performed.
### Storing file
```php
use DragonCode\PrettyArray\Services\File;
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$formatted = $service->raw($array);
File::make($formatted)
->store('foo.php');
```
Result in stored file `foo.php`:
```php
<?php
return [
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
0 => 'qwe',
1 => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
],
2 => 'iop',
];
```
## License
This package is licensed under the [MIT License](LICENSE).
[badge_build]: https://img.shields.io/github/workflow/status/TheDragonCode/pretty-array/phpunit?style=flat-square
[badge_downloads]: https://img.shields.io/packagist/dt/dragon-code/pretty-array.svg?style=flat-square
[badge_license]: https://img.shields.io/packagist/l/dragon-code/pretty-array.svg?style=flat-square
[badge_stable]: https://img.shields.io/github/v/release/TheDragonCode/pretty-array?label=stable&style=flat-square
[badge_unstable]: https://img.shields.io/badge/unstable-dev--main-orange?style=flat-square
[link_build]: https://github.com/TheDragonCode/pretty-array/actions
[link_license]: LICENSE
[link_packagist]: https://packagist.org/packages/dragon-code/pretty-array

View File

@@ -0,0 +1,53 @@
{
"name": "dragon-code/pretty-array",
"description": "Simple conversion of an array to a pretty view",
"license": "MIT",
"type": "library",
"keywords": [
"pretty array",
"pretty",
"array",
"dragon code",
"dragon",
"andrey helldar"
],
"authors": [
{
"name": "Andrey Helldar",
"email": "helldar@ai-rus.com"
}
],
"support": {
"issues": "https://github.com/TheDragonCode/pretty-array/issues",
"source": "https://github.com/TheDragonCode/pretty-array"
},
"require": {
"php": "^8.0",
"ext-dom": "*",
"ext-mbstring": "*",
"dragon-code/contracts": "^2.6",
"dragon-code/support": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"suggest": {
"symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers"
},
"minimum-stability": "stable",
"prefer-stable": true,
"autoload": {
"psr-4": {
"DragonCode\\PrettyArray\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}

View File

@@ -0,0 +1,103 @@
<?php
/*
* This file is part of the "dragon-code/pretty-array" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/pretty-array
*/
namespace DragonCode\PrettyArray\Concerns;
use DragonCode\PrettyArray\Exceptions\UnknownCaseTypeException;
use DragonCode\Support\Facades\Helpers\Str;
trait HasCases
{
protected $case = self::NO_CASE;
/**
* @param int $type
*
* @throws \DragonCode\PrettyArray\Exceptions\UnknownCaseTypeException
*/
public function setCase(int $type = self::NO_CASE): void
{
if ($type < 0 || $type > 4) {
throw new UnknownCaseTypeException($type);
}
$this->case = $type;
}
protected function convertKeysCase(array $array): array
{
if ($this->case === static::NO_CASE) {
return $array;
}
$result = [];
foreach ($array as $key => $value) {
$key = $this->convertKeyCase($key);
$result[$key] = $value;
}
return $result;
}
protected function convertKeyCase(mixed $key): mixed
{
if (! is_string($key)) {
return $key;
}
return match ($this->case) {
static::KEBAB_CASE => $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::KEBAB_CASE
),
static::CAMEL_CASE => $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::CAMEL_CASE
),
static::SNAKE_CASE => $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::SNAKE_CASE
),
static::PASCAL_CASE => $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::PASCAL_CASE
),
default => $key,
};
}
protected function caseTo(mixed $key, int $case = 0): mixed
{
if (! is_string($key)) {
return $key;
}
return match ($case) {
static::CAMEL_CASE => Str::camel($key),
static::KEBAB_CASE => Str::snake($key, '-'),
static::SNAKE_CASE => Str::snake($key),
static::PASCAL_CASE => Str::studly($key),
default => $key,
};
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the "dragon-code/pretty-array" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/pretty-array
*/
namespace DragonCode\PrettyArray\Concerns;
trait HasCastable
{
/**
* Castable value.
*
* @param mixed $value
*
* @return mixed
*/
protected function castValue($value = null)
{
if (is_numeric($value)) {
return $value;
}
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_null($value)) {
return 'null';
}
if (is_array($value) || is_object($value)) {
return '[]';
}
return "'" . addslashes($value) . "'";
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the "dragon-code/pretty-array" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/pretty-array
*/
namespace DragonCode\PrettyArray\Exceptions;
use Exception;
class UnknownCaseTypeException extends Exception
{
public function __construct(string $type)
{
parent::__construct("Unknown conversion type: $type", 500);
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the "dragon-code/pretty-array" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/pretty-array
*/
namespace DragonCode\PrettyArray\Services;
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Filesystem\File as Storage;
use DragonCode\Support\Facades\Tools\Stub;
use DragonCode\Support\Tools\Stub as StubTool;
/**
* @method static File make(?string $content = null)
*/
class File
{
use Makeable;
public function __construct(
protected ?string $content = null
) {
}
public function load(string $filename): array
{
return Storage::load($filename);
}
public function store(string $path, string $stub = StubTool::PHP_ARRAY): void
{
$content = Stub::replace($stub, [
'{{slot}}' => $this->content,
]);
Storage::store($path, $content);
}
}

View File

@@ -0,0 +1,134 @@
<?php
/*
* This file is part of the "dragon-code/pretty-array" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/pretty-array
*/
namespace DragonCode\PrettyArray\Services;
use DragonCode\Contracts\Pretty\Arr\Caseable;
use DragonCode\PrettyArray\Concerns\HasCases;
use DragonCode\PrettyArray\Concerns\HasCastable;
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Helpers\Arr;
class Formatter implements Caseable
{
use HasCases;
use HasCastable;
use Makeable;
protected bool $key_as_string = false;
protected bool $equals_align = false;
protected bool $is_simple = false;
protected int $pad_length = 4;
protected string $line_break = PHP_EOL;
public function setKeyAsString(): void
{
$this->key_as_string = true;
}
public function setEqualsAlign(): void
{
$this->equals_align = true;
}
public function setSimple(): void
{
$this->is_simple = true;
}
public function raw(array $array, int $pad = 1): string
{
if (empty($array)) {
return '[]';
}
$array = $this->convertKeysCase($array);
$keys_size = $this->sizeKeys($array);
$pad_length = $this->pad_length * $pad;
$formatted = '[' . $this->line_break;
foreach ($array as $key => $value) {
$key = $this->key($key, $keys_size);
$value = $this->value($value, $pad + 1);
$row = $this->is_simple
? "$value," . $this->line_break
: "$key => $value," . $this->line_break;
$formatted .= $this->pad($row, $pad_length);
}
return $formatted . $this->pad(']', $pad_length - $this->pad_length);
}
protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string
{
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2;
return str_pad($value, $pad, ' ', $type);
}
protected function value($value, int $pad = 1): mixed
{
if (! empty($value) && (is_array($value) || is_object($value))) {
return $this->raw($value, $pad);
}
return $this->castValue($value);
}
protected function key(mixed $key, int $size = 0): string
{
$key = $this->isStringKey($key) ? "'{$key}'" : $key;
if (! $this->equals_align) {
return $key;
}
return $this->pad($key, $this->keySizeCollision($key, $size), STR_PAD_RIGHT);
}
protected function sizeKeys(array $array): int
{
$sizes = Arr::of($array)->keys()->longestStringLength();
return $this->key_as_string ? $sizes + 2 : $sizes;
}
protected function keySizeCollision($key, int $size): int
{
$collision = is_numeric($key) ? 0 : ($this->isAlignAndString() ? -2 : 0);
return $size + $collision;
}
protected function isStringKey($key): bool
{
return $this->key_as_string || ! is_numeric($key);
}
protected function isAlignAndString(): bool
{
return $this->equals_align && $this->key_as_string;
}
}