/ src / services / PartnersValidationService.php
PartnersValidationService.php
  1  <?php
  2  
  3  namespace ButA2SaeS3\services;
  4  
  5  use ButA2SaeS3\dto\AddDonorDto;
  6  use ButA2SaeS3\dto\AddPartnerDto;
  7  use ButA2SaeS3\dto\AddSubsidyDto;
  8  use ButA2SaeS3\validation\ValidationResult;
  9  
 10  class PartnersValidationService
 11  {
 12      public static function validateAddPartner(array $data): ValidationResult
 13      {
 14          $result = ValidationResult::empty();
 15  
 16          $name = trim($data['partner_name'] ?? '');
 17          $contact = trim($data['contact'] ?? '');
 18          $email = trim($data['email'] ?? '');
 19          $phone = trim($data['phone'] ?? '');
 20          $address = trim($data['address'] ?? '');
 21          $website = trim($data['website'] ?? '');
 22          $notes = trim($data['notes'] ?? '');
 23  
 24          if ($name === '') {
 25              $result->addMessage('partner_name', 'Le nom est requis');
 26          }
 27  
 28          if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
 29              $result->addMessage('email', 'Email invalide');
 30          }
 31  
 32          if ($website !== '' && !filter_var($website, FILTER_VALIDATE_URL)) {
 33              $result->addMessage('website', 'URL invalide');
 34          }
 35  
 36          if ($result->hasMessages()) {
 37              return $result;
 38          }
 39  
 40          $result->setValue(new AddPartnerDto(
 41              $name,
 42              $contact !== '' ? $contact : null,
 43              $email !== '' ? $email : null,
 44              $phone !== '' ? $phone : null,
 45              $address !== '' ? $address : null,
 46              $website !== '' ? $website : null,
 47              $notes !== '' ? $notes : null
 48          ));
 49  
 50          return $result;
 51      }
 52  
 53      public static function validateAddDonor(array $data): ValidationResult
 54      {
 55          $result = ValidationResult::empty();
 56  
 57          $name = trim($data['donor_name'] ?? '');
 58          $contact = trim($data['contact'] ?? '');
 59          $email = trim($data['email'] ?? '');
 60          $notes = trim($data['notes'] ?? '');
 61  
 62          if ($name === '') {
 63              $result->addMessage('donor_name', 'Le nom est requis');
 64          }
 65  
 66          if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
 67              $result->addMessage('email', 'Email invalide');
 68          }
 69  
 70          if ($result->hasMessages()) {
 71              return $result;
 72          }
 73  
 74          $result->setValue(new AddDonorDto(
 75              $name,
 76              $contact !== '' ? $contact : null,
 77              $email !== '' ? $email : null,
 78              $notes !== '' ? $notes : null
 79          ));
 80  
 81          return $result;
 82      }
 83  
 84      public static function validateAddSubsidy(array $data): ValidationResult
 85      {
 86          $result = ValidationResult::empty();
 87  
 88          $partnerId = $data['partner_id'] ?? null;
 89          $title = trim($data['title'] ?? '');
 90          $amount = $data['amount'] ?? null;
 91          $awardedAtStr = trim($data['awarded_at'] ?? '');
 92          $conditions = trim($data['conditions'] ?? '');
 93          $notes = trim($data['notes'] ?? '');
 94  
 95          if ($title === '') {
 96              $result->addMessage('title', 'Le titre est requis');
 97          }
 98  
 99          if ($amount === null || $amount === '' || !is_numeric($amount) || $amount < 0) {
100              $result->addMessage('amount', 'Le montant doit ĂȘtre un nombre');
101          }
102  
103          $awardedAt = null;
104          if ($awardedAtStr !== '') {
105              $parsed = strtotime($awardedAtStr);
106              if ($parsed === false) {
107                  $result->addMessage('awarded_at', "Date invalide");
108              } else {
109                  $awardedAt = $parsed;
110              }
111          }
112  
113          if ($result->hasMessages()) {
114              return $result;
115          }
116  
117          $pid = null;
118          if ($partnerId !== null && $partnerId !== '') {
119              $pid = (int)$partnerId;
120          }
121  
122          $result->setValue(new AddSubsidyDto(
123              $pid,
124              $title,
125              (int)round(((float)$amount) * 100),
126              $awardedAt,
127              $conditions !== '' ? $conditions : null,
128              $notes !== '' ? $notes : null
129          ));
130  
131          return $result;
132      }
133  }
134