refactor: derive sibling key in content-type rules without regex
Both ContentTypeMatchesPlatform and ContentTypeMatchesPostPlatform
were using preg_replace to swap the leaf segment of a dotted attribute
path ("platforms.0.content_type" -> "platforms.0.social_account_id").
Cleaner with Str::beforeLast: derive the parent path once, then look
up the sibling under it. Same intent, no regex, and the rule no longer
encodes its own attribute name.
This commit is contained in:
parent
c1418c9d21
commit
6949ee4ec3
2 changed files with 8 additions and 6 deletions
|
|
@ -9,6 +9,7 @@
|
|||
use Closure;
|
||||
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Cross-validates platforms[].content_type against platforms[].social_account_id
|
||||
|
|
@ -34,10 +35,10 @@ public function setData(array $data): static
|
|||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
// attribute is e.g. "platforms.0.content_type" — the sibling
|
||||
// social_account_id lives at "platforms.0.social_account_id".
|
||||
$accountKey = preg_replace('/\.content_type$/', '.social_account_id', $attribute);
|
||||
$accountId = data_get($this->data, $accountKey);
|
||||
// attribute is e.g. "platforms.0.content_type" — peel off the leaf
|
||||
// and look up the sibling social_account_id under the same parent.
|
||||
$parentKey = Str::beforeLast($attribute, '.');
|
||||
$accountId = data_get($this->data, $parentKey.'.social_account_id');
|
||||
|
||||
if (! $accountId) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
use Closure;
|
||||
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Cross-validates platforms[].content_type against the platform of the
|
||||
|
|
@ -32,8 +33,8 @@ public function setData(array $data): static
|
|||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
$idKey = preg_replace('/\.content_type$/', '.id', $attribute);
|
||||
$postPlatformId = data_get($this->data, $idKey);
|
||||
$parentKey = Str::beforeLast($attribute, '.');
|
||||
$postPlatformId = data_get($this->data, $parentKey.'.id');
|
||||
|
||||
if (! $postPlatformId) {
|
||||
return;
|
||||
|
|
|
|||
Loading…
Reference in a new issue