Skip to content

Commit

Permalink
Allow to pass multiple formats to Laravel's date_format rule
Browse files Browse the repository at this point in the history
  • Loading branch information
riesjart committed Mar 28, 2024
1 parent a5df67e commit c6f8aed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Attributes/Validation/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class DateFormat extends StringValidationAttribute
{
public function __construct(protected string $format)
protected string|array $format;

public function __construct(string|array|RouteParameterReference ...$format)
{
$this->format = Arr::flatten($format);
}

public static function keyword(): string
Expand Down
24 changes: 19 additions & 5 deletions tests/Datasets/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function fixature(
yield from betweenAttributes();
yield from currentPasswordAttributes();
yield from dateEqualsAttributes();
yield from dateFormatAttributes();
yield from dimensionsAttributes();
yield from distinctAttributes();
yield from doesntEndWithAttributes();
Expand Down Expand Up @@ -195,11 +196,6 @@ function fixature(
expected: 'date',
);

yield fixature(
attribute: new DateFormat('Y-m-d'),
expected: 'date_format:Y-m-d',
);

yield fixature(
attribute: new Declined(),
expected: 'declined',
Expand Down Expand Up @@ -584,6 +580,24 @@ function dateEqualsAttributes(): Generator
);
}

function dateFormatAttributes(): Generator
{
yield fixature(
attribute: new DateFormat('Y-m-d'),
expected: 'date_format:Y-m-d',
);

yield fixature(
attribute: new DateFormat(['Y-m-d', 'Y-m-d H:i:s']),
expected: 'date_format:Y-m-d,Y-m-d H:i:s',
);

yield fixature(
attribute: new DateFormat('Y-m-d', 'Y-m-d H:i:s'),
expected: 'date_format:Y-m-d,Y-m-d H:i:s',
);
}

function dimensionsAttributes(): Generator
{
yield fixature(
Expand Down

0 comments on commit c6f8aed

Please sign in to comment.