diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache index 4f057c1..01da114 100644 --- a/.php-cs-fixer.cache +++ b/.php-cs-fixer.cache @@ -1 +1 @@ -{"php":"8.1.8","version":"3.9.5","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Components\/Components.php":4113470597,"src\/ValueObjects\/Types.php":976112391,"src\/Fields\/Image.php":3475913326,"src\/Fields\/Text.php":1843933408,"src\/Fields\/Fields.php":3082770791,"src\/Fields\/Boolean.php":288662375,"src\/Fields\/Tabs.php":2338077135,"src\/Fields\/Select.php":2468598124,"src\/Fields\/Tab.php":122323775,"src\/Fields\/MultiSelect.php":2967987406,"src\/Fields\/Number.php":2972592454,"src\/Contracts\/Field.php":3529337728,"src\/Contracts\/Route.php":1966485194,"src\/Contracts\/IndexComponents.php":1435574258,"src\/Contracts\/Invokables.php":1570910920,"src\/Contracts\/Filter.php":1482797957,"src\/Contracts\/Header.php":3412176535,"src\/Contracts\/FormComponents.php":2006127091,"src\/Contracts\/ComponentsInterface.php":39333525,"src\/Contracts\/Component.php":4154481113,"src\/Contracts\/Renderer.php":3899250064,"src\/Services\/CreateCustomService.php":2751149137,"src\/Services\/FilterService.php":1136423926,"src\/Services\/SortingService.php":971942833,"src\/BreadServiceProvider.php":3682323247,"src\/Routes\/Destroy.php":392466729,"src\/Routes\/Create.php":3261679531,"src\/Routes\/Index.php":2467093646,"src\/Routes\/Edit.php":3380151641,"src\/Routes\/Store.php":901166628,"src\/Routes\/Routes.php":1567158451,"src\/Routes\/Update.php":1183005567,"src\/Filters\/Text.php":4047841771,"src\/Filters\/Filter.php":1453482139,"src\/Filters\/Boolean.php":1579975008,"src\/Filters\/Filters.php":3143774296,"src\/Filters\/Select.php":412827139,"src\/Headers\/Date.php":801899764,"src\/Headers\/Text.php":1963652945,"src\/Headers\/Headers.php":3282279307,"src\/Headers\/Boolean.php":3260714470,"src\/Headers\/Trend.php":1947377138,"src\/Headers\/Link.php":3399789503,"src\/Headers\/Set.php":186843820,"src\/Headers\/Money.php":3303539918,"src\/Headers\/Download.php":2796001948,"src\/Headers\/Rating.php":1094539246,"src\/Headers\/Label.php":3381470344,"src\/Headers\/InertiaLink.php":3761921318,"src\/Headers\/Number.php":844704962,"src\/Commands\/CreateCustomField.php":1830559918,"src\/Commands\/CreateCustomHeader.php":1362670939,"src\/Commands\/CreateCustomFilter.php":2772280546,"src\/Renderers\/FormRenderer.php":119095043,"src\/Renderers\/IndexRenderer.php":144237398,"tests\/Pest.php":1399058707,"tests\/TestCase.php":51090682}} \ No newline at end of file +{"php":"8.1.10","version":"3.11.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Services\/FilterService.php":3242563327,"src\/Services\/SortingService.php":570185584,"src\/Services\/CreateCustomService.php":3045546300,"src\/ValueObjects\/Types.php":976112391,"src\/BreadServiceProvider.php":3682323247,"src\/Contracts\/Component.php":413477820,"src\/Contracts\/Route.php":1994232514,"src\/Contracts\/Header.php":1729022448,"src\/Contracts\/Invokables.php":3589802302,"src\/Contracts\/FormComponents.php":2006127091,"src\/Contracts\/Renderer.php":2450971325,"src\/Contracts\/Field.php":4212146176,"src\/Contracts\/ComponentsInterface.php":957920066,"src\/Contracts\/IndexComponents.php":1435574258,"src\/Contracts\/Filter.php":2143273424,"src\/Routes\/Create.php":1695394094,"src\/Routes\/Destroy.php":1458008311,"src\/Routes\/Update.php":4166386385,"src\/Routes\/Store.php":460891973,"src\/Routes\/Routes.php":2675183373,"src\/Routes\/Index.php":2907359599,"src\/Routes\/Edit.php":3991995671,"src\/Filters\/Filters.php":3707575036,"src\/Filters\/Text.php":1721742153,"src\/Filters\/Boolean.php":1951698771,"src\/Filters\/Select.php":3820557334,"src\/Filters\/Filter.php":1213338388,"src\/Commands\/CreateCustomFilter.php":1671250702,"src\/Commands\/CreateCustomHeader.php":250823473,"src\/Commands\/CreateCustomField.php":4000417138,"src\/Renderers\/IndexRenderer.php":1195470683,"src\/Renderers\/FormRenderer.php":1601248856,"src\/Fields\/Number.php":124780854,"src\/Fields\/MultiSelect.php":2655589068,"src\/Fields\/Text.php":3296465271,"src\/Fields\/Image.php":4032533664,"src\/Fields\/Fields.php":3572158689,"src\/Fields\/Boolean.php":710075004,"src\/Fields\/Tab.php":3137926331,"src\/Fields\/Tabs.php":3422655071,"src\/Fields\/Select.php":2269241859,"src\/Headers\/Number.php":3468185039,"src\/Headers\/Text.php":3453491020,"src\/Headers\/Link.php":3708658392,"src\/Headers\/Money.php":1414716803,"src\/Headers\/Boolean.php":1436861502,"src\/Headers\/Date.php":791925921,"src\/Headers\/Label.php":2559479232,"src\/Headers\/Download.php":1670883268,"src\/Headers\/Headers.php":1828549759,"src\/Headers\/InertiaLink.php":3847878985,"src\/Headers\/Rating.php":2195370648,"src\/Headers\/Trend.php":3244001513,"src\/Headers\/Set.php":4116951602,"src\/Components\/Components.php":3025943152,"tests\/Pest.php":1399058707,"tests\/TestCase.php":51090682}} \ No newline at end of file diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 7c01435..1f25c2a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,8 +2,9 @@ includes: - ./vendor/nunomaduro/larastan/extension.neon parameters: - level: 5 + level: 9 paths: - src tmpDir: build/phpstan checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false diff --git a/src/Commands/CreateCustomField.php b/src/Commands/CreateCustomField.php index 38aa273..e315575 100644 --- a/src/Commands/CreateCustomField.php +++ b/src/Commands/CreateCustomField.php @@ -15,6 +15,7 @@ class CreateCustomField extends Command public function handle(): int { $name = $this->argument('name'); + $name = is_string($name) ? $name : ''; if (! CreateCustomService::isNameValid($name)) { $this->error("Name: '$name' is not valid"); diff --git a/src/Commands/CreateCustomFilter.php b/src/Commands/CreateCustomFilter.php index f7e7494..850edce 100644 --- a/src/Commands/CreateCustomFilter.php +++ b/src/Commands/CreateCustomFilter.php @@ -15,6 +15,7 @@ class CreateCustomFilter extends Command public function handle(): int { $name = $this->argument('name'); + $name = is_string($name) ? $name : ''; if (! CreateCustomService::isNameValid($name)) { $this->error("Name: '$name' is not valid"); diff --git a/src/Commands/CreateCustomHeader.php b/src/Commands/CreateCustomHeader.php index fc4d3f3..ff271e9 100644 --- a/src/Commands/CreateCustomHeader.php +++ b/src/Commands/CreateCustomHeader.php @@ -15,6 +15,7 @@ class CreateCustomHeader extends Command public function handle(): int { $name = $this->argument('name'); + $name = is_string($name) ? $name : ''; if (! CreateCustomService::isNameValid($name)) { $this->error("Name: '$name' is not valid"); diff --git a/src/Components/Components.php b/src/Components/Components.php index 02763c4..ee759b8 100644 --- a/src/Components/Components.php +++ b/src/Components/Components.php @@ -9,6 +9,9 @@ final class Components { + /** + * @var Collection + */ private Collection $components; public function __construct() @@ -16,7 +19,7 @@ public function __construct() $this->components = new Collection(); } - public static function add(...$components) + public static function add(Component ...$components): self { $class = new self(); @@ -27,7 +30,7 @@ public static function add(...$components) return $class; } - public function push(Component $component) + public function push(Component $component): self { $this->components->push($component); @@ -39,14 +42,14 @@ public function get(): Collection return $this->components; } - public function toArray() + public function toArray(): array { return $this->components->map(function (Component $component) { return $component->toArray(); })->toArray(); } - public function getOptions() + public function getOptions(): array { return []; } diff --git a/src/Contracts/Component.php b/src/Contracts/Component.php index eda90bf..c29607d 100644 --- a/src/Contracts/Component.php +++ b/src/Contracts/Component.php @@ -6,8 +6,14 @@ abstract class Component { + /** + * @return string + */ abstract public function getVue(); + /** + * @return array + */ public function toArray() { return [ diff --git a/src/Contracts/ComponentsInterface.php b/src/Contracts/ComponentsInterface.php index 32b3182..ab8262c 100644 --- a/src/Contracts/ComponentsInterface.php +++ b/src/Contracts/ComponentsInterface.php @@ -6,9 +6,18 @@ interface ComponentsInterface { + /** + * @return mixed + */ public function header(); + /** + * @return mixed + */ public function aboveForm(); + /** + * @return mixed + */ public function belowForm(); } diff --git a/src/Contracts/Field.php b/src/Contracts/Field.php index fc84c90..c9c92ca 100644 --- a/src/Contracts/Field.php +++ b/src/Contracts/Field.php @@ -6,6 +6,7 @@ abstract class Field { + /** @var string */ public $type = 'text'; public string $label = ''; public string $name = ''; @@ -15,6 +16,9 @@ abstract class Field 'mt-6', ], ]; + /** + * @var mixed + */ protected $options; public string $vueComponent; @@ -34,7 +38,7 @@ public function __construct(string $name, string $label) * @param bool $overwrite * @return Field */ - public function addWrapperClass($class, $overwrite = false) + public function addWrapperClass(string $class, $overwrite = false): Field { return $this->addClass('wrapper', $class, $overwrite); } @@ -66,7 +70,7 @@ public function toArray() ]; } - private function addClass(string $element, $class, $overwrite = false) + private function addClass(string $element, string $class, bool $overwrite = false): Field { if ($overwrite) { $this->classes[$element] = $class; diff --git a/src/Contracts/Filter.php b/src/Contracts/Filter.php index d3f3b5a..8484357 100644 --- a/src/Contracts/Filter.php +++ b/src/Contracts/Filter.php @@ -27,7 +27,7 @@ public function __construct(string $label, string $field) $this->filterMethods = new Collection(); } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): Filter { if (method_exists($this, $name)) { return $this->{$name}(...$arguments); @@ -38,33 +38,38 @@ public function __call($name, $arguments) return $this; } - public function setLabel(string $label) + public function setLabel(string $label): Filter { $this->label = $label; return $this; } - public function setField(string $field) + public function setField(string $field): Filter { $this->field = $field; return $this; } - public function setType(string $type) + public function setType(string $type): Filter { $this->type = $type; return $this; } - public function setOption(string $key, $value) + /** + * @param string $key + * @param mixed $value + * @return void + */ + public function setOption(string $key, $value): void { $this->options[$key] = $value; } - public function toArray() + public function toArray(): array { return [ 'label' => $this->label, @@ -102,7 +107,7 @@ public function setOperator(string $operator): self return $this; } - public function apply(Builder $builder, $value) + public function apply(Builder $builder, mixed $value): Builder { return $builder->where($this->getField(), $this->getOperator(), $value); } @@ -115,6 +120,9 @@ public function getFilterMethods(): Collection return $this->filterMethods; } + /** + * @return mixed + */ private function getValue() { return app('request')->input("filters.{$this->getField()}"); diff --git a/src/Contracts/Header.php b/src/Contracts/Header.php index a7e06d6..8409c7c 100644 --- a/src/Contracts/Header.php +++ b/src/Contracts/Header.php @@ -6,12 +6,12 @@ abstract class Header { - protected $value; + protected string $value; protected string $label; protected string $type = 'property'; - protected $options = []; + protected array $options = []; protected bool $sortable = false; protected bool $editable = false; @@ -22,20 +22,25 @@ public function __construct(string $label, string $value) $this->setValue($value); } - protected function setLabel(string $label) + protected function setLabel(string $label): self { $this->label = $label; return $this; } - protected function setValue($value) + protected function setValue(string $value): self { $this->value = $value; return $this; } + /** + * @param string $key + * @param mixed $value + * @return $this + */ protected function setOption(string $key, $value): self { $this->options[$key] = $value; @@ -54,28 +59,28 @@ protected function setType(string $type): Header * Available Options */ - public function editable(bool $editable = false) + public function editable(bool $editable = false): self { $this->editable = $editable; return $this; } - public function sortable() + public function sortable(): self { $this->sortable = true; return $this; } - public function tooltip(string $tooltip) + public function tooltip(string $tooltip): self { $this->setOption('tooltip', $tooltip); return $this; } - public function toArray() + public function toArray(): array { return [ 'type' => $this->type, diff --git a/src/Contracts/Invokables.php b/src/Contracts/Invokables.php index 21a3b35..09b8549 100644 --- a/src/Contracts/Invokables.php +++ b/src/Contracts/Invokables.php @@ -6,5 +6,9 @@ interface Invokables { + /** + * @param Renderer $renderer + * @return mixed + */ public function __invoke(Renderer $renderer); } diff --git a/src/Contracts/Renderer.php b/src/Contracts/Renderer.php index 9472738..8f936f2 100644 --- a/src/Contracts/Renderer.php +++ b/src/Contracts/Renderer.php @@ -7,17 +7,34 @@ use Illuminate\Contracts\Support\Responsable; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; +use Libaro\Bread\Fields\Fields; use Libaro\Bread\Routes\Routes; abstract class Renderer implements Responsable { protected string $title = ''; protected string $action = ''; + + /** + * @var mixed + */ protected $entity; + + /** + * @var mixed + */ protected $items; + /** + * @var mixed + */ protected $resource; - protected $classes = []; + protected array $classes = []; protected ?Collection $components; + + + /** + * @var ?Fields + */ protected $fields; protected ?Routes $routes = null; @@ -42,7 +59,7 @@ public function resource(string $resource): self return $this; } - public function with(array $array) + public function with(array $array): self { foreach ($array as $key => $value) { $this->{$key} = $value; @@ -51,20 +68,26 @@ public function with(array $array) return $this; } - public function classes($classes) + /** + * @param mixed $classes + * @return $this + */ + public function classes($classes): self { if (is_string($classes)) { $classes = explode(' ', $classes); } - foreach ($classes as $class) { - $this->classes[] = $class; + if (is_iterable($classes)) { + foreach ($classes as $class) { + $this->classes[] = $class; + } } return $this; } - public function __call($name, array $arguments) + public function __call(string $name, array $arguments): self { if (method_exists($this, $name)) { return $this->{$name}(...$arguments); @@ -89,21 +112,28 @@ public function __call($name, array $arguments) } } + /** + * @return mixed + */ public function getEntity() { return $this->entity; } + /** + * @return mixed|string + */ protected function guessResource() { if ($this->resource) { return $this->resource; } + /** @phpstan-ignore-next-line */ $first = $this->items->first(); if ($first === null && $this->items instanceof LengthAwarePaginator) { - $path = explode('/', $this->items->path()); + $path = ($this->items->path()) ? explode('/', $this->items->path()) : []; $this->resource = $path[count($path) - 1]; } @@ -115,13 +145,19 @@ protected function guessResource() return $this->resource; } + /** + * @param mixed ...$components + * @return $this + */ public function components(...$components) { foreach ($components as $i => $component) { $class = new $component(); $methods = get_class_methods($class); foreach ($methods as $methodKey => $method) { - $this->components->put($method, $class->{$method}()); + if ($this->components) { + $this->components->put($method, $class->{$method}()); + } } } @@ -130,12 +166,13 @@ public function components(...$components) public function getComponents(): array { - return $this->components->map(function ($components) { + return $this->components ? $this->components->map(function ($components) { if ($components === null) { return $components; } + /** @phpstan-ignore-next-line */ return $components->toArray(); - })->toArray(); + })->toArray() : []; } } diff --git a/src/Contracts/Route.php b/src/Contracts/Route.php index da60e5a..fc93eb4 100644 --- a/src/Contracts/Route.php +++ b/src/Contracts/Route.php @@ -6,26 +6,30 @@ abstract class Route { - protected $name; + protected string $name; - abstract public function __construct($name); + abstract public function __construct(string $name); - public static function make($name) + /** + * @param string $name + * @return static + */ + public static function make(string $name) { return new static($name); } - public function getName() + public function getName(): string { return $this->name; } - public function getType() + public function getType(): string { return strtolower(class_basename($this)); } - public function toArray() + public function toArray(): array { return ['name' => $this->name]; } diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php index fa57454..5136978 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -9,9 +9,9 @@ final class Boolean extends Field { public $type = 'boolean'; - public $attributes = []; + public array $attributes = []; - public static function make(string $name, string $label, array $attributes = []) + public static function make(string $name, string $label, array $attributes = []): self { return new self($name, $label, $attributes); } @@ -22,7 +22,7 @@ public function __construct(string $name, string $label, array $attributes = []) $this->attributes = $attributes; } - public function toArray() + public function toArray(): array { $array = parent::toArray(); $array['attributes'] = $this->attributes; diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index 2e514d5..497cbc6 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -6,9 +6,13 @@ use Illuminate\Support\Collection; use Illuminate\Support\Fluent; +use Libaro\Bread\Contracts\Field; class Fields { + /** + * @var Collection + */ private Collection $fields; public function __construct() @@ -16,7 +20,11 @@ public function __construct() $this->fields = new Collection(); } - public static function add(...$fields) + /** + * @param Field ...$fields + * @return Fields + */ + public static function add(...$fields): Fields { $class = new self(); @@ -27,7 +35,11 @@ public static function add(...$fields) return $class; } - public function push($field) + /** + * @param Field $field + * @return $this + */ + public function push(Field $field): Fields { $this->fields->push($field); @@ -39,11 +51,11 @@ public function get(): Collection return $this->fields; } - public function toArray() + public function toArray(): Fluent { $class = new Fluent(); $class->offsetSet('data', $this->fields - ->map(function ($field) { + ->map(function (Field $field) { return $field->toArray(); }) ->toArray()); @@ -53,7 +65,7 @@ public function toArray() return $class; } - public function getOptions() + public function getOptions(): array { return []; } diff --git a/src/Fields/Image.php b/src/Fields/Image.php index 994ff11..7f51c4e 100644 --- a/src/Fields/Image.php +++ b/src/Fields/Image.php @@ -10,7 +10,7 @@ final class Image extends Field { public $type = 'image'; - public $collection = 'images'; + public string $collection = 'images'; /** * @param string $name @@ -21,12 +21,12 @@ public function __construct(string $name, string $label) parent::__construct($name, $label); } - public static function make(string $name, string $label) + public static function make(string $name, string $label): Image { return new self($name, $label); } - public function toArray() + public function toArray(): array { $array = parent::toArray(); $array['collection'] = $this->collection; diff --git a/src/Fields/MultiSelect.php b/src/Fields/MultiSelect.php index a5414a6..ee63ba5 100644 --- a/src/Fields/MultiSelect.php +++ b/src/Fields/MultiSelect.php @@ -22,7 +22,7 @@ public function __construct(string $name, string $label, Collection $options) $this->options = $options; } - public static function make(string $name, string $label, Collection $options) + public static function make(string $name, string $label, Collection $options): MultiSelect { return new self($name, $label, $options); } diff --git a/src/Fields/Number.php b/src/Fields/Number.php index a921853..a50f4dd 100644 --- a/src/Fields/Number.php +++ b/src/Fields/Number.php @@ -20,7 +20,7 @@ public function __construct(string $name, string $label) $this->options = ['float' => true]; } - public static function make(string $name, string $label) + public static function make(string $name, string $label): Number { return new self($name, $label); } diff --git a/src/Fields/Select.php b/src/Fields/Select.php index e26b088..add3c61 100644 --- a/src/Fields/Select.php +++ b/src/Fields/Select.php @@ -10,7 +10,7 @@ final class Select extends Field { public $type = 'select'; - public $multiple = true; + public bool $multiple = true; /** * @param string $name @@ -23,19 +23,19 @@ public function __construct(string $name, string $label, Collection $options) $this->options = $options; } - public static function make(string $name, string $label, Collection $options) + public static function make(string $name, string $label, Collection $options): Select { return new self($name, $label, $options); } - public function multiple() + public function multiple(): Select { $this->multiple = true; return $this; } - public function toArray() + public function toArray(): array { $array = parent::toArray(); $array['multiple'] = $this->multiple; diff --git a/src/Fields/Tab.php b/src/Fields/Tab.php index ef8b418..cc3de79 100644 --- a/src/Fields/Tab.php +++ b/src/Fields/Tab.php @@ -11,15 +11,23 @@ final class Tab extends Field { public $type = 'tab'; + /** + * @var Collection + */ public $fields; - protected $title; + protected string $title; public function __construct() { $this->fields = new Collection(); } - public static function make($title, ...$fields) + /** + * @param $title + * @param Field ...$fields + * @return Tab + */ + public static function make(string $title, ...$fields): Tab { $class = new self(); $class->setTitle($title); @@ -31,7 +39,7 @@ public static function make($title, ...$fields) return $class; } - public function setTitle(string $title) + public function setTitle(string $title): Tab { $this->title = $title; @@ -45,6 +53,7 @@ public function toArray() { $array = parent::toArray(); $array['title'] = $this->title; + $array['fields'] = $this->fields->map(function (Field $field) { return $field->toArray(); })->toArray(); diff --git a/src/Fields/Tabs.php b/src/Fields/Tabs.php index 408ab30..27d0319 100644 --- a/src/Fields/Tabs.php +++ b/src/Fields/Tabs.php @@ -10,14 +10,21 @@ final class Tabs extends Field { public $type = 'tabs'; - public $tabs; + /** + * @var Collection + */ + public Collection $tabs; public function __construct() { $this->tabs = new Collection(); } - public static function add(...$tabs) + /** + * @param Tab ...$tabs + * @return Tabs + */ + public static function add(...$tabs): Tabs { $class = new self(); @@ -28,7 +35,7 @@ public static function add(...$tabs) return $class; } - public function push(Tab $tab) + public function push(Tab $tab): Tabs { $this->tabs->push($tab); @@ -38,7 +45,7 @@ public function push(Tab $tab) /** * @return array */ - public function toArray() + public function toArray(): array { $array = parent::toArray(); $array['tabs'] = $this->tabs diff --git a/src/Fields/Text.php b/src/Fields/Text.php index fdc5d11..b326812 100644 --- a/src/Fields/Text.php +++ b/src/Fields/Text.php @@ -8,7 +8,7 @@ final class Text extends Field { - public static function make(string $name, string $label) + public static function make(string $name, string $label): Text { return new self($name, $label); } diff --git a/src/Filters/Boolean.php b/src/Filters/Boolean.php index fca2d3d..cc2d731 100644 --- a/src/Filters/Boolean.php +++ b/src/Filters/Boolean.php @@ -11,7 +11,7 @@ public function __construct(string $label, string $field) $this->setType('boolean'); } - public static function make(string $label, string $field) + public static function make(string $label, string $field): self { return new self($label, $field); } diff --git a/src/Filters/Filter.php b/src/Filters/Filter.php index e0d0f62..0cf7d0d 100644 --- a/src/Filters/Filter.php +++ b/src/Filters/Filter.php @@ -25,7 +25,7 @@ public function __construct(string $label, string $field) $this->filterMethods = new Collection(); } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): Filter { if (method_exists($this, $name)) { return $this->{$name}(...$arguments); @@ -36,33 +36,38 @@ public function __call($name, $arguments) return $this; } - public function setLabel(string $label) + public function setLabel(string $label): Filter { $this->label = $label; return $this; } - public function setField(string $field) + public function setField(string $field): Filter { $this->field = $field; return $this; } - public function setType(string $type) + public function setType(string $type): Filter { $this->type = $type; return $this; } - public function setOption(string $key, $value) + /** + * @param string $key + * @param mixed $value + * @return void + */ + public function setOption(string $key, $value): void { $this->options[$key] = $value; } - public function toArray() + public function toArray(): array { return [ 'label' => $this->label, @@ -100,6 +105,11 @@ public function setOperator(string $operator): self return $this; } + /** + * @param Builder $builder + * @param mixed $value + * @return Builder + */ public function apply(Builder $builder, $value) { return $builder->where($this->getField(), $this->getOperator(), $value); @@ -113,6 +123,9 @@ public function getFilterMethods(): Collection return $this->filterMethods; } + /** + * @return mixed + */ private function getValue() { return app('request')->input("filters.{$this->getField()}"); diff --git a/src/Filters/Filters.php b/src/Filters/Filters.php index b7af8b5..a946129 100644 --- a/src/Filters/Filters.php +++ b/src/Filters/Filters.php @@ -7,7 +7,10 @@ class Filters { - private Collection $filters; + /** + * @var Collection + */ + private $filters; private int $sideBarStarsAt = 5; public function __construct() @@ -15,7 +18,11 @@ public function __construct() $this->filters = new Collection(); } - public static function add(...$filters) + /** + * @param Filter ...$filters + * @return Filters + */ + public static function add(...$filters): Filters { $class = new self(); @@ -26,7 +33,7 @@ public static function add(...$filters) return $class; } - public function push(Filter $filter) + public function push(Filter $filter): Filters { $this->filters->push($filter); @@ -38,7 +45,7 @@ public function get(): Collection return $this->filters; } - public function toArray() + public function toArray(): stdClass { $class = new stdClass(); $class->data = $this->filters @@ -52,14 +59,14 @@ public function toArray() return $class; } - public function sideBarStarsAt(int $int) + public function sideBarStarsAt(int $int): Filters { $this->sideBarStarsAt = $int; return $this; } - public function getOptions() + public function getOptions(): array { return [ 'sidebar_starts_at' => $this->sideBarStarsAt, diff --git a/src/Filters/Select.php b/src/Filters/Select.php index c4317c1..adb33fa 100644 --- a/src/Filters/Select.php +++ b/src/Filters/Select.php @@ -15,17 +15,22 @@ public function __construct(string $label, string $field, array $values) $this->setOption('values', $values); } - public static function make(string $label, string $field, array $values) + public static function make(string $label, string $field, array $values): Select { return new self($label, $field, $values); } - public function multi(bool $multi = true) + public function multi(bool $multi = true): void { $this->setOption('multiple', $multi); } - public function apply(Builder $builder, $value) + /** + * @param Builder $builder + * @param mixed $value + * @return Builder + */ + public function apply(Builder $builder, $value): Builder { if ($this->options['multiple']) { return $builder->whereIn($this->field, $value); diff --git a/src/Filters/Text.php b/src/Filters/Text.php index 5c8be60..967e814 100644 --- a/src/Filters/Text.php +++ b/src/Filters/Text.php @@ -15,13 +15,22 @@ public function __construct(string $label, string $field) $this->setType('text'); } - public static function make(string $label, string $field) + public static function make(string $label, string $field): Text { return new self($label, $field); } - public function apply(Builder $builder, $value) + /** + * @param Builder $builder + * @param string $value + * @return Builder + */ + public function apply(Builder $builder, $value): Builder { - return $builder->where($this->getField(), $this->getOperator(), "%$value%"); +// \PHPStan\dumpType($value); + + + return $builder + ->where($this->getField(), $this->getOperator(), "%$value%"); } } diff --git a/src/Headers/Boolean.php b/src/Headers/Boolean.php index d71c607..12f08ab 100644 --- a/src/Headers/Boolean.php +++ b/src/Headers/Boolean.php @@ -15,7 +15,7 @@ public function __construct(string $label, string $value) $this->setType('boolean'); } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Boolean { return new self($label, $value); } diff --git a/src/Headers/Date.php b/src/Headers/Date.php index 5a81dc7..e8465d3 100644 --- a/src/Headers/Date.php +++ b/src/Headers/Date.php @@ -16,7 +16,7 @@ public function __construct(string $label, string $value, string $format) $this->setType('date'); } - public static function make(string $label, string $value, string $format = 'DD-MM-YYYY HH:mm') + public static function make(string $label, string $value, string $format = 'DD-MM-YYYY HH:mm'): Date { return new self($label, $value, $format); } diff --git a/src/Headers/Download.php b/src/Headers/Download.php index e70079a..33ee9ba 100644 --- a/src/Headers/Download.php +++ b/src/Headers/Download.php @@ -15,7 +15,7 @@ public function __construct(string $label, string $value) $this->setType('download'); } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Download { return new self($label, $value); } diff --git a/src/Headers/Headers.php b/src/Headers/Headers.php index 46c9d1a..c808c23 100644 --- a/src/Headers/Headers.php +++ b/src/Headers/Headers.php @@ -10,14 +10,21 @@ final class Headers { - protected Collection $headers; + /** + * @var Collection + */ + protected $headers; public function __construct() { $this->headers = new Collection(); } - public static function add(...$headers) + /** + * @param Header ...$headers + * @return Headers + */ + public static function add(...$headers): Headers { $class = new self(); @@ -28,7 +35,7 @@ public static function add(...$headers) return $class; } - public function push(Header $header) + public function push(Header $header): Headers { $this->headers->push($header); @@ -40,7 +47,7 @@ public function get(): Collection return $this->headers; } - public function toArray() + public function toArray(): Fluent { $class = new Fluent(); $class->offsetSet('data', $this->headers @@ -54,7 +61,7 @@ public function toArray() return $class; } - public function getOptions() + public function getOptions(): array { return []; } diff --git a/src/Headers/InertiaLink.php b/src/Headers/InertiaLink.php index 3fb8907..3ebff5c 100644 --- a/src/Headers/InertiaLink.php +++ b/src/Headers/InertiaLink.php @@ -17,7 +17,7 @@ public function __construct(string $label, string $value, string|null $name, str $this->setType('inertia_link'); } - public static function make(string $label, string $value, string|null $name, string|null $propName = null) + public static function make(string $label, string $value, string|null $name, string|null $propName = null): InertiaLink { return new self($label, $value, $name, $propName); } diff --git a/src/Headers/Label.php b/src/Headers/Label.php index f1b2e13..ac62698 100644 --- a/src/Headers/Label.php +++ b/src/Headers/Label.php @@ -16,7 +16,7 @@ public function __construct(string $label, string $value, array $options) $this->setType('label'); } - public static function make(string $label, string $value, array $options) + public static function make(string $label, string $value, array $options): Label { return new self($label, $value, $options); } diff --git a/src/Headers/Link.php b/src/Headers/Link.php index afb9a14..66a1880 100644 --- a/src/Headers/Link.php +++ b/src/Headers/Link.php @@ -16,7 +16,7 @@ public function __construct(string $label, string $value, bool $_blank) $this->setType('link'); } - public static function make(string $label, string $value, bool $_blank = false) + public static function make(string $label, string $value, bool $_blank = false): Link { return new self($label, $value, $_blank); } diff --git a/src/Headers/Money.php b/src/Headers/Money.php index 6bfb3b5..26c9c73 100644 --- a/src/Headers/Money.php +++ b/src/Headers/Money.php @@ -21,33 +21,33 @@ public function __construct(string $label, string $value) ]; } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Money { return new self($label, $value); } - public function prefix(string $prefix) + public function prefix(string $prefix): Money { $this->setOption('prefix', $prefix); return $this; } - public function suffix(string $suffix) + public function suffix(string $suffix): Money { $this->setOption('suffix', $suffix); return $this; } - public function centSeparator(string $separator) + public function centSeparator(string $separator): Money { $this->setOption('cent_separator', $separator); return $this; } - public function thousandSeparator(string $separator) + public function thousandSeparator(string $separator): Money { $this->setOption('thousand_separator', $separator); diff --git a/src/Headers/Number.php b/src/Headers/Number.php index d8400b6..255db2c 100644 --- a/src/Headers/Number.php +++ b/src/Headers/Number.php @@ -15,7 +15,7 @@ public function __construct(string $label, string $value) $this->setType('number'); } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Number { return new self($label, $value); } diff --git a/src/Headers/Rating.php b/src/Headers/Rating.php index e49a4ac..20a40e4 100644 --- a/src/Headers/Rating.php +++ b/src/Headers/Rating.php @@ -16,7 +16,7 @@ public function __construct(string $label, string $value, array $options) $this->setType('rating'); } - public static function make(string $label, string $value, array $options) + public static function make(string $label, string $value, array $options): Rating { return new self($label, $value, $options); } diff --git a/src/Headers/Set.php b/src/Headers/Set.php index 3259b7c..29c91b0 100644 --- a/src/Headers/Set.php +++ b/src/Headers/Set.php @@ -16,7 +16,7 @@ public function __construct(string $label, string $value, string $separator) $this->setType('set'); } - public static function make(string $label, string $value, string $separator = ', ') + public static function make(string $label, string $value, string $separator = ', '): Set { return new self($label, $value, $separator); } diff --git a/src/Headers/Text.php b/src/Headers/Text.php index df88a0a..892ee11 100644 --- a/src/Headers/Text.php +++ b/src/Headers/Text.php @@ -15,7 +15,7 @@ public function __construct(string $label, string $value) $this->setType('property'); } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Text { return new self($label, $value); } diff --git a/src/Headers/Trend.php b/src/Headers/Trend.php index be27ecf..44f1579 100644 --- a/src/Headers/Trend.php +++ b/src/Headers/Trend.php @@ -15,7 +15,7 @@ public function __construct(string $label, string $value) $this->setType('trend'); } - public static function make(string $label, string $value) + public static function make(string $label, string $value): Trend { return new self($label, $value); } diff --git a/src/Renderers/FormRenderer.php b/src/Renderers/FormRenderer.php index e61675e..50d76a1 100644 --- a/src/Renderers/FormRenderer.php +++ b/src/Renderers/FormRenderer.php @@ -5,6 +5,7 @@ namespace Libaro\Bread\Renderers; use Illuminate\Database\Eloquent\Model; +use Inertia\Inertia; use Libaro\Bread\Contracts\Renderer; /** @@ -14,9 +15,15 @@ */ final class FormRenderer extends Renderer { + /** + * @var Model|mixed + */ protected $entity; - private $method = 'POST'; + private string $method = 'POST'; + /** + * @param mixed $entity + */ public function __construct($entity = null) { parent::__construct(); @@ -28,7 +35,7 @@ public function __construct($entity = null) $this->entity = $entity; } - public function setMethod($method) + public function setMethod(string $method): FormRenderer { if (! in_array($method, ['POST', 'PUT', 'PATCH'])) { throw new \InvalidArgumentException('Invalid method'); @@ -39,11 +46,15 @@ public function setMethod($method) return $this; } - public function getMethod() + public function getMethod(): string { return $this->method; } + /** + * @param mixed $entity + * @return FormRenderer + */ public static function render($entity = null): FormRenderer { return new FormRenderer($entity); @@ -51,14 +62,14 @@ public static function render($entity = null): FormRenderer public function toResponse($request) { - return inertia('Bread::Form') + return Inertia::render('Bread::Form') ->with([ 'title' => $this->title, 'entity' => $this->entity, - 'fields' => optional($this->fields)->toArray() ?? [], + 'fields' => $this->fields ? $this->fields->toArray() : [], 'action' => $this->action, 'method' => $this->getMethod(), - 'routes' => optional($this->routes)->toArray() ?? [], + 'routes' => $this->routes ? $this->routes->toArray() : [], 'resource' => $this->guessResource(), 'classes' => $this->getClasses(), 'components' => $this->getComponents(), @@ -66,7 +77,7 @@ public function toResponse($request) ->toResponse($request); } - public function getClasses() + public function getClasses(): array { return $this->classes; } diff --git a/src/Renderers/IndexRenderer.php b/src/Renderers/IndexRenderer.php index b70f10a..88334a3 100644 --- a/src/Renderers/IndexRenderer.php +++ b/src/Renderers/IndexRenderer.php @@ -4,16 +4,23 @@ namespace Libaro\Bread\Renderers; +use Illuminate\Http\JsonResponse; +use Inertia\Inertia; use Libaro\Bread\Contracts\Renderer; use Libaro\Bread\Filters\Filters; use Libaro\Bread\Headers\Headers; +use Symfony\Component\HttpFoundation\Response; final class IndexRenderer extends Renderer { protected string $title = ''; protected ?Headers $headers = null; protected ?Filters $filters = null; - protected $actions = []; + protected array $actions = []; + + /** + * @var mixed + */ protected $items = []; public static function render(): IndexRenderer @@ -21,23 +28,27 @@ public static function render(): IndexRenderer return new static(); } - public function items($items) + /** + * @param mixed $items + * @return $this + */ + public function items($items): self { $this->items = $items; return $this; } - public function toResponse($request) + public function toResponse($request): JsonResponse|Response { - return inertia('Bread::Index') + return Inertia::render('Bread::Index') ->with([ - 'headers' => $this->headers->toArray(), - 'filters' => optional($this->filters)->toArray() ?? [], + 'headers' => $this->headers ? $this->headers->toArray() : [], + 'filters' => $this->filters ? $this->filters->toArray() : [], 'actions' => $this->actions, 'items' => $this->items, 'title' => $this->title, - 'routes' => optional($this->routes)->toArray() ?? [], + 'routes' => $this->routes ? $this->routes->toArray() : [], 'resource' => $this->guessResource(), 'deleteMessage' => $this->deleteMessage, 'components' => $this->getComponents(), diff --git a/src/Routes/Create.php b/src/Routes/Create.php index a4daab2..6aabbac 100644 --- a/src/Routes/Create.php +++ b/src/Routes/Create.php @@ -8,7 +8,7 @@ final class Create extends Route { - public function __construct($name) + public function __construct(string $name) { $this->name = $name; } diff --git a/src/Routes/Destroy.php b/src/Routes/Destroy.php index 789e0f9..9cb7eed 100644 --- a/src/Routes/Destroy.php +++ b/src/Routes/Destroy.php @@ -8,15 +8,15 @@ final class Destroy extends Route { - protected $identifier = 'id'; + protected string $identifier = 'id'; - public function __construct($name, $identifier = 'id') + public function __construct(string $name, string $identifier = 'id') { $this->name = $name; $this->identifier = $identifier; } - public static function make($name, $identifier = 'id') + public static function make(string $name, string $identifier = 'id'): self { return new static($name, $identifier); } diff --git a/src/Routes/Edit.php b/src/Routes/Edit.php index 0bc3900..09486b6 100644 --- a/src/Routes/Edit.php +++ b/src/Routes/Edit.php @@ -8,15 +8,15 @@ final class Edit extends Route { - protected $identifier = 'id'; + protected string $identifier = 'id'; - public function __construct($name, $identifier = 'id') + public function __construct(string $name, string $identifier = 'id') { $this->name = $name; $this->identifier = $identifier; } - public static function make($name, $identifier = 'id') + public static function make(string $name, string $identifier = 'id'): self { return new static($name, $identifier); } diff --git a/src/Routes/Index.php b/src/Routes/Index.php index 8dfdc7f..a40ee03 100644 --- a/src/Routes/Index.php +++ b/src/Routes/Index.php @@ -8,7 +8,7 @@ final class Index extends Route { - public function __construct($name) + public function __construct(string $name) { $this->name = $name; } diff --git a/src/Routes/Routes.php b/src/Routes/Routes.php index 6bb88a9..fe4fc2b 100644 --- a/src/Routes/Routes.php +++ b/src/Routes/Routes.php @@ -9,6 +9,9 @@ final class Routes { + /** + * @var Collection + */ protected $routes; public function __construct() @@ -16,7 +19,7 @@ public function __construct() $this->routes = new Collection(); } - public static function add(...$routes) + public static function add(Route ...$routes): self { $class = new self(); @@ -27,7 +30,7 @@ public static function add(...$routes) return $class; } - public function push(Route $header) + public function push(Route $header): self { $this->routes->push($header); @@ -39,7 +42,7 @@ public function get(): Collection return $this->routes; } - public function toArray() + public function toArray(): array { return $this->routes->mapWithKeys(function (Route $route) { return [ diff --git a/src/Routes/Store.php b/src/Routes/Store.php index a6bd1c5..6ecd1bb 100644 --- a/src/Routes/Store.php +++ b/src/Routes/Store.php @@ -8,15 +8,15 @@ final class Store extends Route { - protected $identifier = 'id'; + protected string $identifier = 'id'; - public function __construct($name, $identifier = 'id') + public function __construct(string $name, string $identifier = 'id') { $this->name = $name; $this->identifier = $identifier; } - public static function make($name, $identifier = 'id') + public static function make(string $name, string $identifier = 'id'): self { return new static($name, $identifier); } diff --git a/src/Routes/Update.php b/src/Routes/Update.php index d69e57a..c99e596 100644 --- a/src/Routes/Update.php +++ b/src/Routes/Update.php @@ -8,15 +8,15 @@ final class Update extends Route { - protected $identifier = 'id'; + protected string $identifier = 'id'; - public function __construct($name, $identifier = 'id') + public function __construct(string $name, string $identifier = 'id') { $this->name = $name; $this->identifier = $identifier; } - public static function make($name, $identifier = 'id') + public static function make(string $name, string $identifier = 'id'): self { return new static($name, $identifier); } diff --git a/src/Services/CreateCustomService.php b/src/Services/CreateCustomService.php index 3ada92c..8959d8f 100644 --- a/src/Services/CreateCustomService.php +++ b/src/Services/CreateCustomService.php @@ -8,22 +8,32 @@ class CreateCustomService { - public static function isNameValid(string $name): bool + /** + * @param string $name + * @return bool + */ + public static function isNameValid($name): bool { - return preg_match( + $isValid = preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $name ); + + return (bool) $isValid; } - public static function transformName(string $name): string + /** + * @param string $name + * @return string + */ + public static function transformName($name): string { return Str::ucfirst(Str::camel($name)); } public static function copyFiles(string $name, int $type): array { - [$pathPhp, $pathVue] = Types::getPaths($type); + [$pathPhp, $pathVue] = Types::getPaths($type) ?? [] ; [$stubPhp, $stubVue] = Types::getStubs($type); self::copyStub($pathPhp, $stubPhp, $name, 'php'); @@ -32,14 +42,14 @@ public static function copyFiles(string $name, int $type): array return [$pathPhp, $pathVue]; } - protected static function copyStub($path, $stub, $name, $extension) + protected static function copyStub(string $path, string $stub, string $name, string $extension): void { File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); File::put("$path/$name.$extension", self::getContent($stub, $name)); } - protected static function getContent($stub, $name): string + protected static function getContent(string $stub, string $name): string { $contents = File::get($stub); diff --git a/src/Services/FilterService.php b/src/Services/FilterService.php index 4a29d84..50b6614 100644 --- a/src/Services/FilterService.php +++ b/src/Services/FilterService.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Support\Collection; +use Libaro\Bread\Contracts\Filter; use Libaro\Bread\Filters\Filters; final class FilterService @@ -24,7 +25,7 @@ public function __invoke(Builder $builder, Filters $filters): Builder { $this->builder = $builder; $this->filters = $filters->get(); - $requestFilters = $this->request->query('filters', []); + $requestFilters = (array) $this->request->query('filters', []); foreach ($requestFilters as $field => $value) { $this->applyFilter($field, $value); } @@ -33,8 +34,8 @@ public function __invoke(Builder $builder, Filters $filters): Builder } /** - * @param $field - * @param $value + * @param mixed $field + * @param mixed $value * @return void */ private function applyFilter($field, $value): void @@ -43,6 +44,7 @@ private function applyFilter($field, $value): void return $filter->getField() === $field; }); + /** @var Filter $filter */ $filterMethods = $filter->getFilterMethods(); if ($filterMethods->count() > 0) { foreach ($filterMethods as $method => $params) { diff --git a/src/Services/SortingService.php b/src/Services/SortingService.php index c722ae3..fa59dca 100644 --- a/src/Services/SortingService.php +++ b/src/Services/SortingService.php @@ -16,12 +16,22 @@ public function __construct(Request $request) $this->request = $request; } - public function __invoke(Builder $builder, $defaultColumn, $defaultDirection): Builder + /** + * @param Builder $builder + * @param mixed $defaultColumn + * @param string $defaultDirection + * @return Builder + */ + public function __invoke(Builder $builder, $defaultColumn, string $defaultDirection): Builder { if ($request = $this->request->get('sort')) { - $builder->orderBy($request['column'], $request['direction']); + if (is_array($request)) { + $builder->orderBy($request['column'], $request['direction']); + } } else { - $builder->orderBy($defaultColumn, $defaultDirection); + if (is_string($defaultColumn) && is_string($defaultDirection)) { + $builder->orderBy($defaultColumn, $defaultDirection); + } } return $builder;