/ app / Rules / UniqueInboxFileFolder.php
UniqueInboxFileFolder.php
 1  <?php
 2  
 3  namespace App\Rules;
 4  
 5  use App\Models\Dossier;
 6  use Illuminate\Contracts\Validation\InvokableRule;
 7  
 8  class UniqueInboxFileFolder implements InvokableRule
 9  {
10  
11      public function __construct(public int $dossierId)
12      {
13          //
14      }
15  
16      /**
17       * Run the validation rule.
18       *
19       * @param  string  $attribute
20       * @param  mixed  $value
21       * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
22       * @return void
23       */
24      public function __invoke($attribute, $value, $fail)
25      {
26          $folderName = pathinfo($value)['filename'];
27          $found = Dossier::find($this->dossierId)
28              ->folders
29              ->where('name',  __('documents.folders.external'))
30              ->first()
31              ->children
32              ->where('name', $folderName)
33              ->first();
34  
35          if (!is_null($found)) {
36              $fail(__('validation.inbox.filename.unique_in_dossier'));
37          }
38  
39          return true;
40      }
41  }