Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Attributes/DataValidationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\LaravelData\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
class DataValidationContext
{
public function __construct(
public string $context,
) {}
}
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Accepted.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Accepted extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'accepted';
Expand Down
4 changes: 3 additions & 1 deletion src/Attributes/Validation/AcceptedIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class AcceptedIf extends StringValidationAttribute

public function __construct(
string|FieldReference $field,
protected string|bool|int|float|BackedEnum|ExternalReference $value
protected string|bool|int|float|BackedEnum|ExternalReference $value,
array|string|null $context = null,
) {
$this->field = $this->parseFieldReference($field);
$this->context = $context;
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/ActiveUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class ActiveUrl extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'active_url';
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/After.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class After extends StringValidationAttribute
{
public function __construct(protected string|DateTimeInterface|FieldReference|ExternalReference $date)
{
public function __construct(
protected string|DateTimeInterface|FieldReference|ExternalReference $date,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/AfterOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class AfterOrEqual extends StringValidationAttribute
{
public function __construct(protected string|DateTimeInterface|ExternalReference|FieldReference $date)
{
public function __construct(
protected string|DateTimeInterface|ExternalReference|FieldReference $date,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Alpha extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'alpha';
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/AlphaDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class AlphaDash extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'alpha_dash';
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/AlphaNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class AlphaNumeric extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'alpha_num';
Expand Down
10 changes: 7 additions & 3 deletions src/Attributes/Validation/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class ArrayType extends StringValidationAttribute
{
protected array $keys;

public function __construct(array|string|ExternalReference ...$keys)
{
$this->keys = Arr::flatten($keys);
public function __construct(
array|string|ExternalReference ...$keys,
) {
$extracted = $this->extractContextFromVariadicValues($keys);

$this->keys = Arr::flatten($extracted['values']);
$this->context = $extracted['context'];
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Bail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Bail extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'bail';
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/Before.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Before extends StringValidationAttribute
{
public function __construct(protected string|DateTimeInterface|ExternalReference|FieldReference $date)
{
public function __construct(
protected string|DateTimeInterface|ExternalReference|FieldReference $date,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/BeforeOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class BeforeOrEqual extends StringValidationAttribute
{
public function __construct(protected string|DateTimeInterface|ExternalReference|FieldReference $date)
{
public function __construct(
protected string|DateTimeInterface|ExternalReference|FieldReference $date,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
8 changes: 6 additions & 2 deletions src/Attributes/Validation/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Between extends StringValidationAttribute
{
public function __construct(protected int|float|ExternalReference $min, protected int|float|ExternalReference $max)
{
public function __construct(
protected int|float|ExternalReference $min,
protected int|float|ExternalReference $max,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class BooleanType extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'boolean';
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Confirmed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Confirmed extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'confirmed';
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/CurrentPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class CurrentPassword extends StringValidationAttribute
{
public function __construct(protected null|string|DummyBackedEnum|ExternalReference $guard = null)
{
public function __construct(
protected null|string|DummyBackedEnum|ExternalReference $guard = null,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Date extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'date';
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/DateEquals.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class DateEquals extends StringValidationAttribute
{
public function __construct(protected string|DateTimeInterface|ExternalReference $date)
{
public function __construct(
protected string|DateTimeInterface|ExternalReference $date,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
10 changes: 7 additions & 3 deletions src/Attributes/Validation/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class DateFormat extends StringValidationAttribute
{
protected string|array $format;

public function __construct(string|array|ExternalReference ...$format)
{
$this->format = Arr::flatten($format);
public function __construct(
string|array|ExternalReference ...$format,
) {
$extracted = $this->extractContextFromVariadicValues($format);

$this->format = Arr::flatten($extracted['values']);
$this->context = $extracted['context'];
}

public static function keyword(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Attributes/Validation/Declined.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Declined extends StringValidationAttribute
{
public function __construct(
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
{
return 'declined';
Expand Down
2 changes: 2 additions & 0 deletions src/Attributes/Validation/DeclinedIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class DeclinedIf extends StringValidationAttribute
public function __construct(
string|FieldReference $field,
protected string|bool|int|float|BackedEnum|ExternalReference $value,
array|string|null $context = null,
) {
$this->field = $this->parseFieldReference($field);
$this->context = $context;
}

public static function keyword(): string
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/Different.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ class Different extends StringValidationAttribute
{
protected FieldReference $field;

public function __construct(string|FieldReference $field)
{
public function __construct(
string|FieldReference $field,
array|string|null $context = null,
) {
$this->field = $this->parseFieldReference($field);
$this->context = $context;
}

public static function keyword(): string
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/Digits.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Digits extends StringValidationAttribute
{
public function __construct(protected int|ExternalReference $value)
{
public function __construct(
protected int|ExternalReference $value,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
8 changes: 6 additions & 2 deletions src/Attributes/Validation/DigitsBetween.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class DigitsBetween extends StringValidationAttribute
{
public function __construct(protected int|ExternalReference $min, protected int|ExternalReference $max)
{
public function __construct(
protected int|ExternalReference $min,
protected int|ExternalReference $max,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
6 changes: 4 additions & 2 deletions src/Attributes/Validation/Dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function __construct(
protected null|float|string|ExternalReference $ratio = null,
protected null|int|ExternalReference $width = null,
protected null|int|ExternalReference $height = null,
protected null|BaseDimensions $rule = null,
protected ?BaseDimensions $rule = null,
array|string|null $context = null,
) {
if (
$minWidth === null
Expand All @@ -34,6 +35,7 @@ public function __construct(
) {
throw CannotBuildValidationRule::create('You must specify one of width, height, minWidth, minHeight, maxWidth, maxHeight, ratio or a dimensions rule.');
}
$this->context = $context;
}

public function getRule(ValidationPath $path): object|string
Expand All @@ -50,7 +52,7 @@ public function getRule(ValidationPath $path): object|string
$width = $this->normalizePossibleExternalReferenceParameter($this->width);
$height = $this->normalizePossibleExternalReferenceParameter($this->height);

$rule = new BaseDimensions();
$rule = new BaseDimensions;

if ($minWidth !== null) {
$rule->minWidth($minWidth);
Expand Down
8 changes: 6 additions & 2 deletions src/Attributes/Validation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
class Distinct extends StringValidationAttribute
{
public const Strict = 'strict';

public const IgnoreCase = 'ignore_case';

public function __construct(protected null|string|ExternalReference $mode = null)
{
public function __construct(
protected null|string|ExternalReference $mode = null,
array|string|null $context = null,
) {
$this->context = $context;
}

public static function keyword(): string
Expand Down
Loading
Loading