Skip to content

Commit

Permalink
Merge pull request #13 from TheDragonCode/4.x
Browse files Browse the repository at this point in the history
Bump `dragon-code/support` to 6
  • Loading branch information
Andrey Helldar authored Apr 21, 2022
2 parents c9ab2df + 422751e commit 4af216c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 151 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ "7.3", "7.4", "8.0" ]
php: [ "8.0", "8.1" ]

name: PHP ${{ matrix.php }}, Support ${{ matrix.support }}
name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
"source": "https://github.com/TheDragonCode/pretty-array"
},
"require": {
"php": "^7.3 || ^8.0",
"php": "^8.0",
"ext-dom": "*",
"ext-mbstring": "*",
"dragon-code/contracts": "^2.6",
"dragon-code/support": "^5.0"
"dragon-code/support": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"conflict": {
"andrey-helldar/pretty-array": "*"
"phpunit/phpunit": "^9.5"
},
"suggest": {
"symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers"
Expand Down
78 changes: 32 additions & 46 deletions src/Concerns/HasCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,63 +55,49 @@ protected function convertKeysCase(array $array): array
return $result;
}

protected function convertKeyCase($key)
protected function convertKeyCase(mixed $key): mixed
{
if (! is_string($key)) {
return $key;
}

switch ($this->case) {
case static::KEBAB_CASE:
return $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::KEBAB_CASE
);

case static::CAMEL_CASE:
return $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::CAMEL_CASE
);

case static::SNAKE_CASE:
return $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::SNAKE_CASE
);

case static::PASCAL_CASE:
return $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::PASCAL_CASE
);

default:
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($key, int $case = 0)
protected function caseTo(mixed $key, int $case = 0): mixed
{
if (! is_string($key)) {
return $key;
}

switch ($case) {
case static::CAMEL_CASE:
return Str::camel($key);

case static::KEBAB_CASE:
return Str::snake($key, '-');

case static::SNAKE_CASE:
return Str::snake($key);

case static::PASCAL_CASE:
return Str::studly($key);

default:
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,
};
}
}
30 changes: 0 additions & 30 deletions src/Exceptions/FileDoesntExistsException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/UnknownCaseTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class UnknownCaseTypeException extends Exception
{
public function __construct(string $type)
{
parent::__construct("Unknown conversion type: {$type}", 500);
parent::__construct("Unknown conversion type: $type", 500);
}
}
40 changes: 8 additions & 32 deletions src/Services/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,26 @@

namespace DragonCode\PrettyArray\Services;

use DragonCode\PrettyArray\Exceptions\FileDoesntExistsException;
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Helpers\Filesystem\File as FileSupport;
use DragonCode\Support\Facades\Filesystem\File as Storage;
use DragonCode\Support\Facades\Tools\Stub;
use DragonCode\Support\Tools\Stub as StubTool;

/**
* @method static \DragonCode\PrettyArray\Services\File make(string $content = null)
* @method static File make(?string $content = null)
*/
class File
{
use Makeable;

protected $content;

public function __construct(?string $content = null)
{
$this->content = $content;
public function __construct(
protected ?string $content = null
) {
}

/**
* @param string $filename
*
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
*
* @return mixed
*/
public function load(string $filename)
public function load(string $filename): array
{
if (! file_exists($filename)) {
throw new FileDoesntExistsException($filename);
}

return require $filename;
}

public function loadRaw(string $filename)
{
return file_get_contents($filename);
return Storage::load($filename);
}

public function store(string $path, string $stub = StubTool::PHP_ARRAY): void
Expand All @@ -64,11 +45,6 @@ public function store(string $path, string $stub = StubTool::PHP_ARRAY): void
'{{slot}}' => $this->content,
]);

FileSupport::store($path, $content);
}

public function storeRaw(string $path): void
{
FileSupport::store($path, $this->content);
Storage::store($path, $content);
}
}
33 changes: 13 additions & 20 deletions src/Services/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class Formatter implements Caseable
use HasCastable;
use Makeable;

protected $key_as_string = false;
protected bool $key_as_string = false;

protected $equals_align = false;
protected bool $equals_align = false;

protected $is_simple = false;
protected bool $is_simple = false;

protected $pad_length = 4;
protected int $pad_length = 4;

protected $line_break = PHP_EOL;
protected string $line_break = PHP_EOL;

public function setKeyAsString(): void
{
Expand All @@ -64,7 +64,8 @@ public function raw(array $array, int $pad = 1): string

$keys_size = $this->sizeKeys($array);
$pad_length = $this->pad_length * $pad;
$formatted = '[' . $this->line_break;

$formatted = '[' . $this->line_break;

foreach ($array as $key => $value) {
$key = $this->key($key, $keys_size);
Expand All @@ -82,14 +83,12 @@ public function raw(array $array, int $pad = 1): string

protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string
{
$pad += $type === STR_PAD_LEFT
? strlen($value)
: 2;
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2;

return str_pad($value, $pad, ' ', $type);
}

protected function value($value, int $pad = 1)
protected function value($value, int $pad = 1): mixed
{
if (! empty($value) && (is_array($value) || is_object($value))) {
return $this->raw($value, $pad);
Expand All @@ -98,7 +97,7 @@ protected function value($value, int $pad = 1)
return $this->castValue($value);
}

protected function key($key, int $size = 0)
protected function key(mixed $key, int $size = 0): string
{
$key = $this->isStringKey($key) ? "'{$key}'" : $key;

Expand All @@ -111,20 +110,14 @@ protected function key($key, int $size = 0)

protected function sizeKeys(array $array): int
{
$sizes = Arr::longestStringLength(
array_keys($array)
);
$sizes = Arr::of($array)->keys()->longestStringLength();

return $this->key_as_string
? $sizes + 2
: $sizes;
return $this->key_as_string ? $sizes + 2 : $sizes;
}

protected function keySizeCollision($key, int $size): int
{
$collision = is_numeric($key)
? 0
: ($this->isAlignAndString() ? -2 : 0);
$collision = is_numeric($key) ? 0 : ($this->isAlignAndString() ? -2 : 0);

return $size + $collision;
}
Expand Down
14 changes: 0 additions & 14 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,13 @@ protected function getFile(string $filename): string
);
}

/**
* @param string $filename
*
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
*
* @return array
*/
protected function requireFile(string $filename): array
{
return File::make()->load(
$this->path($filename)
);
}

/**
* @param string $filename
*
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
*
* @return array
*/
protected function requireSource(string $filename = 'source.php'): array
{
return $this->requireFile($filename);
Expand Down

0 comments on commit 4af216c

Please sign in to comment.