/ src / services / ContributionValidationService.php
ContributionValidationService.php
 1  <?php
 2  
 3  namespace ButA2SaeS3\services;
 4  
 5  use ButA2SaeS3\dto\AddContributionDto;
 6  use ButA2SaeS3\validation\ValidationResult;
 7  
 8  class ContributionValidationService
 9  {
10      public static function validateAddContribution(array $data): ValidationResult
11      {
12          $result = ValidationResult::empty();
13  
14          $adherents_id = $data['adherents_id'] ?? null;
15          $amount = $data['amount'] ?? null;
16          $method = trim($data['method'] ?? '');
17          $reference = trim($data['reference'] ?? '');
18          $notes = trim($data['notes'] ?? '');
19  
20          if (empty($adherents_id)) {
21              $result->addMessage('adherents_id', 'Sélectionner un adhérent');
22          }
23  
24          if ($amount === null || $amount === '' || !is_numeric($amount) || $amount <= 0) {
25              $result->addMessage('amount', 'Le montant doit être un nombre positif');
26          }
27  
28          if ($method === '') {
29              $result->addMessage('method', 'Le moyen de paiement est requis');
30          }
31  
32          if ($result->hasMessages()) {
33              return $result;
34          }
35  
36          $dto = new AddContributionDto(
37              (int)$adherents_id,
38              (int)round(((float)$amount) * 100),
39              $method,
40              $reference !== '' ? $reference : null,
41              $notes !== '' ? $notes : null
42          );
43  
44          $result->setValue($dto);
45          return $result;
46      }
47  }