/ app / Rules / ValidateScopeExists.php
ValidateScopeExists.php
 1  <?php
 2  
 3  namespace App\Rules;
 4  
 5  use App\Models\NormGroup;
 6  use App\Models\NormScope;
 7  use Illuminate\Contracts\Validation\InvokableRule;
 8  
 9  class ValidateScopeExists implements InvokableRule
10  {
11      public function __construct(public string $cc_version)
12      {
13          //
14      }
15      /**
16       * Run the validation rule.
17       *
18       * @param  string  $attribute
19       * @param  mixed  $value
20       * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
21       * @return void
22       */
23      public function __invoke($attribute, $value, $fail)
24      {
25          $norm = NormGroup
26              ::where('name', 'CC')->first()
27              ->norms
28              ->where('version', $this->cc_version)->first();
29  
30          if ($norm === null) {
31              $fail('no norm group CC with version '. $this->cc_version);
32          }
33          
34          $scope = $norm->scopes->where('name', $value)->first();
35          if ($scope === null) {
36              $fail('no scope '. $value);
37          }
38      }
39  }