-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Lomkit/feature/more-constraints-on-relations
✨ constraints on relations
- Loading branch information
Showing
12 changed files
with
574 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Lomkit\Rest\Concerns\Makeable; | ||
|
||
class ProhibitedRelationOnCreation implements ValidationRule, DataAwareRule | ||
{ | ||
use Makeable; | ||
|
||
/** | ||
* Indicates whether the rule should be implicit. | ||
* | ||
* @var bool | ||
*/ | ||
public $implicit = true; | ||
|
||
/** | ||
* The resource related to. | ||
* | ||
* @var resource | ||
*/ | ||
protected $resource = null; | ||
|
||
/** | ||
* All of the data under validation. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected $data = []; | ||
|
||
/** | ||
* Set the data under validation. | ||
* | ||
* @param array<string, mixed> $data | ||
*/ | ||
public function setData(array $data): static | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the resource related to. | ||
* | ||
* @param mixed $resource | ||
* | ||
* @return $this | ||
*/ | ||
public function resource($resource) | ||
{ | ||
$this->resource = $resource; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Validate the attribute. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param \Closure $fail | ||
* | ||
* @return void | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$arrayDot = Arr::dot($this->data); | ||
|
||
if ( | ||
isset($arrayDot[Str::of($attribute)->beforeLast('.')->beforeLast('.')->append('.operation')->toString()]) && | ||
$arrayDot[Str::of($attribute)->beforeLast('.')->beforeLast('.')->append('.operation')->toString()] === 'create' && | ||
(isset($arrayDot[$attribute.'.0.operation']) || isset($arrayDot[$attribute.'.operation'])) | ||
) { | ||
$fail('This relation is prohibited on creation'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Lomkit\Rest\Concerns\Makeable; | ||
|
||
class ProhibitedRelationOnUpdate implements ValidationRule, DataAwareRule | ||
{ | ||
use Makeable; | ||
|
||
/** | ||
* Indicates whether the rule should be implicit. | ||
* | ||
* @var bool | ||
*/ | ||
public $implicit = true; | ||
|
||
/** | ||
* The resource related to. | ||
* | ||
* @var resource | ||
*/ | ||
protected $resource = null; | ||
|
||
/** | ||
* All of the data under validation. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected $data = []; | ||
|
||
/** | ||
* Set the data under validation. | ||
* | ||
* @param array<string, mixed> $data | ||
*/ | ||
public function setData(array $data): static | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the resource related to. | ||
* | ||
* @param mixed $resource | ||
* | ||
* @return $this | ||
*/ | ||
public function resource($resource) | ||
{ | ||
$this->resource = $resource; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Validate the attribute. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param \Closure $fail | ||
* | ||
* @return void | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$arrayDot = Arr::dot($this->data); | ||
|
||
if ( | ||
isset($arrayDot[Str::of($attribute)->beforeLast('.')->beforeLast('.')->append('.operation')->toString()]) && | ||
$arrayDot[Str::of($attribute)->beforeLast('.')->beforeLast('.')->append('.operation')->toString()] === 'update' && | ||
(isset($arrayDot[$attribute.'.0.operation']) || isset($arrayDot[$attribute.'.operation'])) | ||
) { | ||
$fail('This relation is prohibited on creation'); | ||
} | ||
} | ||
} |
Oops, something went wrong.