/** * Transform contact to transient data * * @param Contact $contact * @returns array */ public static function validate(Contact $contact) { $isEmpty = function ($value) { $value = trim($value); return empty($value); }; $errors = []; if ($isEmpty($contact->getTitle())) { $errors['title'] = 'Title required'; } if ($isEmpty($contact->getName())) { $errors['name'] = 'Name required'; } if ($isEmpty($contact->getEmail())) { $errors['email'] = 'Email required'; } elseif (filter_var($contact->getEmail(), FILTER_VALIDATE_EMAIL) === false) { $errors['email'] = 'Email is invalid'; } return $errors; }
/** * Transform contact to transient data * * @param Contact $contact * @returns array */ public static function toTransient(Contact $contact) { $supervisor = $contact->getSupervisor(); return ['id' => $contact->getId(), 'title' => $contact->getTitle(), 'name' => $contact->getName(), 'email' => $contact->getEmail(), 'supervisor' => $supervisor ? self::toTransient($supervisor) : null, 'supervised_contacts' => self::listToTransient($contact->getSupervisedContacts())]; }