Skip to content

Commit

Permalink
Make sure object rules are not created in their constructor to fix ca…
Browse files Browse the repository at this point in the history
…ching issues
  • Loading branch information
rubenvanassche committed Feb 16, 2024
1 parent afead66 commit a84a126
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 142 deletions.
50 changes: 25 additions & 25 deletions src/Attributes/Validation/Dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,16 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Dimensions extends ObjectValidationAttribute
{
protected BaseDimensions $rule;

public function __construct(
null|int|RouteParameterReference $minWidth = null,
null|int|RouteParameterReference $minHeight = null,
null|int|RouteParameterReference $maxWidth = null,
null|int|RouteParameterReference $maxHeight = null,
null|float|string|RouteParameterReference $ratio = null,
null|int|RouteParameterReference $width = null,
null|int|RouteParameterReference $height = null,
null|BaseDimensions $rule = null,
protected null|int|RouteParameterReference $minWidth = null,
protected null|int|RouteParameterReference $minHeight = null,
protected null|int|RouteParameterReference $maxWidth = null,
protected null|int|RouteParameterReference $maxHeight = null,
protected null|float|string|RouteParameterReference $ratio = null,
protected null|int|RouteParameterReference $width = null,
protected null|int|RouteParameterReference $height = null,
protected null|BaseDimensions $rule = null,
) {
$minWidth = $this->normalizePossibleRouteReferenceParameter($minWidth);
$minHeight = $this->normalizePossibleRouteReferenceParameter($minHeight);
$maxWidth = $this->normalizePossibleRouteReferenceParameter($maxWidth);
$maxHeight = $this->normalizePossibleRouteReferenceParameter($maxHeight);
$ratio = $this->normalizePossibleRouteReferenceParameter($ratio);
$width = $this->normalizePossibleRouteReferenceParameter($width);
$height = $this->normalizePossibleRouteReferenceParameter($height);

if (
$minWidth === null
&& $minHeight === null
Expand All @@ -44,8 +34,23 @@ public function __construct(
) {
throw CannotBuildValidationRule::create('You must specify one of width, height, minWidth, minHeight, maxWidth, maxHeight, ratio or a dimensions rule.');
}
}

public function getRule(ValidationPath $path): object|string
{
if($this->rule) {
return $this->rule;
}

$minWidth = $this->normalizePossibleRouteReferenceParameter($this->minWidth);
$minHeight = $this->normalizePossibleRouteReferenceParameter($this->minHeight);
$maxWidth = $this->normalizePossibleRouteReferenceParameter($this->maxWidth);
$maxHeight = $this->normalizePossibleRouteReferenceParameter($this->maxHeight);
$ratio = $this->normalizePossibleRouteReferenceParameter($this->ratio);
$width = $this->normalizePossibleRouteReferenceParameter($this->width);
$height = $this->normalizePossibleRouteReferenceParameter($this->height);

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

if ($minWidth !== null) {
$rule->minWidth($minWidth);
Expand Down Expand Up @@ -75,12 +80,7 @@ public function __construct(
$rule->ratio($ratio);
}

$this->rule = $rule;
}

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
return $this->rule = $rule;
}

public static function keyword(): string
Expand Down
19 changes: 11 additions & 8 deletions src/Attributes/Validation/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Enum extends ObjectValidationAttribute
{
protected EnumRule $rule;

public function __construct(string|EnumRule|RouteParameterReference $enum)
{
$this->rule = $enum instanceof EnumRule
? $enum
: new EnumRule((string)$enum);
public function __construct(
protected string|EnumRule|RouteParameterReference $enum,
protected ?EnumRule $rule = null,
) {
}

public static function keyword(): string
Expand All @@ -26,7 +23,13 @@ public static function keyword(): string

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
if ($this->rule) {
return $this->rule;
}

return $this->rule = $this->enum instanceof EnumRule
? $this->enum
: new EnumRule((string) $this->enum);
}

public static function create(string ...$parameters): static
Expand Down
48 changes: 24 additions & 24 deletions src/Attributes/Validation/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,33 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Exists extends ObjectValidationAttribute
{
protected BaseExists $rule;

public function __construct(
null|string|RouteParameterReference $table = null,
null|string|RouteParameterReference $column = 'NULL',
null|string|RouteParameterReference $connection = null,
bool|RouteParameterReference $withoutTrashed = false,
string|RouteParameterReference $deletedAtColumn = 'deleted_at',
?Closure $where = null,
?BaseExists $rule = null,
protected null|string|RouteParameterReference $table = null,
protected null|string|RouteParameterReference $column = 'NULL',
protected null|string|RouteParameterReference $connection = null,
protected bool|RouteParameterReference $withoutTrashed = false,
protected string|RouteParameterReference $deletedAtColumn = 'deleted_at',
protected ?Closure $where = null,
protected ?BaseExists $rule = null,
) {
$table = $this->normalizePossibleRouteReferenceParameter($table);
$column = $this->normalizePossibleRouteReferenceParameter($column);
$connection = $this->normalizePossibleRouteReferenceParameter($connection);
$withoutTrashed = $this->normalizePossibleRouteReferenceParameter($withoutTrashed);
$deletedAtColumn = $this->normalizePossibleRouteReferenceParameter($deletedAtColumn);

if ($rule === null && $table === null) {
throw new Exception('Could not make exists rule since a table or rule is required');
}
}

public function getRule(ValidationPath $path): object|string
{
if($this->rule) {
return $this->rule;
}

$table = $this->normalizePossibleRouteReferenceParameter($this->table);
$column = $this->normalizePossibleRouteReferenceParameter($this->column);
$connection = $this->normalizePossibleRouteReferenceParameter($this->connection);
$withoutTrashed = $this->normalizePossibleRouteReferenceParameter($this->withoutTrashed);
$deletedAtColumn = $this->normalizePossibleRouteReferenceParameter($this->deletedAtColumn);

$rule ??= new BaseExists(
$rule = new BaseExists(
$connection ? "{$connection}.{$table}" : $table,
$column
);
Expand All @@ -42,16 +47,11 @@ public function __construct(
$rule->withoutTrashed($deletedAtColumn);
}

if ($where) {
$rule->where($where);
if ($this->where) {
$rule->where($this->where);
}

$this->rule = $rule;
}

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
return $this->rule = $rule;
}

public static function keyword(): string
Expand Down
31 changes: 18 additions & 13 deletions src/Attributes/Validation/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class In extends ObjectValidationAttribute
{
protected BaseIn $rule;
protected ?BaseIn $rule = null;

public function __construct(array|string|BaseIn|RouteParameterReference ...$values)
private array $values;

public function __construct(
array|string|BaseIn|RouteParameterReference ...$values
) {
$this->values = $values;
}

public function getRule(ValidationPath $path): object|string
{
if (count($values) === 1 && $values[0] instanceof BaseIn) {
$this->rule = $values[0];
if ($this->rule) {
return $this->rule;
}

return;
if (count($this->values) === 1 && $this->values[0] instanceof BaseIn) {
return $this->rule = $this->values[0];
}

$values = array_map(
$this->values = array_map(
fn (string|RouteParameterReference $value) => $this->normalizePossibleRouteReferenceParameter($value),
Arr::flatten($values)
Arr::flatten($this->values)
);

$this->rule = new BaseIn($values);
}

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
return $this->rule = new BaseIn($this->values);
}

public static function keyword(): string
Expand Down
28 changes: 16 additions & 12 deletions src/Attributes/Validation/NotIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class NotIn extends ObjectValidationAttribute
{
protected BaseNotIn $rule;
protected ?BaseNotIn $rule = null;

protected array $values;

public function __construct(array|string|BaseNotIn|RouteParameterReference ...$values)
{
if (count($values) === 1 && $values[0] instanceof BaseNotIn) {
$this->rule = $values[0];
$this->values = $values;
}

public function getRule(ValidationPath $path): object|string
{
if($this->rule) {
return $this->rule;
}

return;
if (count($this->values) === 1 && $this->values[0] instanceof BaseNotIn) {
return $this->rule = $this->values[0];
}

$values = array_map(
$this->values = array_map(
fn (string|RouteParameterReference $value) => $this->normalizePossibleRouteReferenceParameter($value),
Arr::flatten($values)
Arr::flatten($this->values)
);

$this->rule = new BaseNotIn($values);
}

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
return $this->rule = new BaseNotIn($this->values);
}

public static function keyword(): string
Expand Down
62 changes: 31 additions & 31 deletions src/Attributes/Validation/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,40 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class Password extends ObjectValidationAttribute
{
protected BasePassword $rule;

public function __construct(
int|RouteParameterReference $min = 12,
bool|RouteParameterReference $letters = false,
bool|RouteParameterReference $mixedCase = false,
bool|RouteParameterReference $numbers = false,
bool|RouteParameterReference $symbols = false,
bool|RouteParameterReference $uncompromised = false,
int|RouteParameterReference $uncompromisedThreshold = 0,
bool|RouteParameterReference $default = false,
?BasePassword $rule = null,
protected int|RouteParameterReference $min = 12,
protected bool|RouteParameterReference $letters = false,
protected bool|RouteParameterReference $mixedCase = false,
protected bool|RouteParameterReference $numbers = false,
protected bool|RouteParameterReference $symbols = false,
protected bool|RouteParameterReference $uncompromised = false,
protected int|RouteParameterReference $uncompromisedThreshold = 0,
protected bool|RouteParameterReference $default = false,
protected ?BasePassword $rule = null,
) {
$min = $this->normalizePossibleRouteReferenceParameter($min);
$letters = $this->normalizePossibleRouteReferenceParameter($letters);
$mixedCase = $this->normalizePossibleRouteReferenceParameter($mixedCase);
$numbers = $this->normalizePossibleRouteReferenceParameter($numbers);
$symbols = $this->normalizePossibleRouteReferenceParameter($symbols);
$uncompromised = $this->normalizePossibleRouteReferenceParameter($uncompromised);
$uncompromisedThreshold = $this->normalizePossibleRouteReferenceParameter($uncompromisedThreshold);
$default = $this->normalizePossibleRouteReferenceParameter($default);

if ($default && $rule === null) {
$this->rule = BasePassword::default();

return;

}

public function getRule(ValidationPath $path): object|string
{
if ($this->rule) {
return $this->rule;
}

$rule ??= BasePassword::min($min);
$min = $this->normalizePossibleRouteReferenceParameter($this->min);
$letters = $this->normalizePossibleRouteReferenceParameter($this->letters);
$mixedCase = $this->normalizePossibleRouteReferenceParameter($this->mixedCase);
$numbers = $this->normalizePossibleRouteReferenceParameter($this->numbers);
$symbols = $this->normalizePossibleRouteReferenceParameter($this->symbols);
$uncompromised = $this->normalizePossibleRouteReferenceParameter($this->uncompromised);
$uncompromisedThreshold = $this->normalizePossibleRouteReferenceParameter($this->uncompromisedThreshold);
$default = $this->normalizePossibleRouteReferenceParameter($this->default);

if ($default && $this->rule === null) {
return $this->rule = BasePassword::default();
}

$rule = BasePassword::min($min);

if ($letters) {
$rule->letters();
Expand All @@ -61,12 +66,7 @@ public function __construct(
$rule->uncompromised($uncompromisedThreshold);
}

$this->rule = $rule;
}

public function getRule(ValidationPath $path): object|string
{
return $this->rule;
return $this->rule = $rule;
}

public static function keyword(): string
Expand Down
Loading

0 comments on commit a84a126

Please sign in to comment.