src/Entity/GDA/Enfant.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity\GDA;
  3. use JsonSerializable;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use App\Entity\Commun\DateNaissanceTrait;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\GDA\EnfantRepository")
  9.  * @ORM\Table(name="efc.gda_enfant")
  10.  */
  11. class Enfant implements JsonSerializable
  12. {
  13.     // <editor-fold defaultstate="collapsed" desc="Attributs privés">
  14.     use DateNaissanceTrait;
  15.     /**
  16.      * @ORM\Column(type="integer")
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue(strategy="IDENTITY")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=50)
  23.      * @Assert\Regex(pattern="/^[A-Za-zÀ-ž ',-]{0,50}$/", message="Les caractères spéciaux ne sont pas acceptés.")
  24.      */
  25.     private $nom_famille;
  26.     /**
  27.      * @ORM\Column(type="string", length=50)
  28.      * @Assert\Regex(pattern="/^[A-Za-zÀ-ž ',-]{0,50}$/", message="Les caractères spéciaux ne sont pas acceptés.")
  29.      */
  30.     private $prenom;
  31.     /**
  32.      * @ORM\Column(type="string", length=1)
  33.      * @Assert\Regex(pattern="/^[FIM]$/", message="Feminin, Indetermine, Masculin")
  34.      */
  35.     private $sexe;
  36.     /**
  37.      * @ORM\OneToMany(targetEntity="Accueil", mappedBy="enfant")
  38.      * @ORM\OrderBy({"date_debut" = "DESC","date_fin" = "DESC"})
  39.      */
  40.     private $accueils;
  41.     /**
  42.      * @Assert\Valid
  43.      * @ORM\OneToMany(targetEntity="ResponsableLegal", mappedBy="enfant", cascade={"persist", "remove"})
  44.      * @ORM\OrderBy({"numero" = "ASC"})
  45.      */
  46.     private $responsables_legaux;
  47.     // </editor-fold>
  48.     // <editor-fold desc="Accesseurs">
  49.     function getId()
  50.     {
  51.         return $this->id;
  52.     }
  53.     function getNomFamille()
  54.     {
  55.         return $this->nom_famille;
  56.     }
  57.     function getPrenom()
  58.     {
  59.         return $this->prenom;
  60.     }
  61.     function getSexe()
  62.     {
  63.         return $this->sexe;
  64.     }
  65.     function getAccueils()
  66.     {
  67.         return $this->accueils;
  68.     }
  69.     function getResponsablesLegaux()
  70.     {
  71.         return $this->responsables_legaux;
  72.     }
  73.     public function setNomFamille($nomFamille)
  74.     {
  75.         // TODO factoriser dans trait personne
  76.         $this->nom_famille strtoupper($nomFamille);
  77.         return $this;
  78.     }
  79.     public function setPrenom($prenom)
  80.     {
  81.         // TODO factoriser dans trait personne
  82.         $prenomCapitalizeHyphenated implode('-'array_map('ucwords'explode('-'strtolower($prenom))));
  83.         $this->prenom $prenomCapitalizeHyphenated;
  84.         return $this;
  85.     }
  86.     public function setSexe($sexe)
  87.     {
  88.         $this->sexe $sexe;
  89.         return $this;
  90.     }
  91.     public function setAccueils($accueils)
  92.     {
  93.         $this->accueils $accueils;
  94.         return $this;
  95.     }
  96.     public function setResponsablesLegaux($responsablesLegaux)
  97.     {
  98.         $this->responsables_legaux $responsablesLegaux;
  99.         return $this;
  100.     }
  101.     /**
  102.      * Obtient le libellé pour le sexe de l'enfant
  103.      *
  104.      * @return void
  105.      */
  106.     function getSexeTexte()
  107.     {
  108.         switch ($this->sexe) {
  109.             case 'F':
  110.                 $sexeTexte "Féminin";
  111.                 break;
  112.             case 'M':
  113.                 $sexeTexte "Masculin";
  114.                 break;
  115.             default:
  116.                 $sexeTexte "Indéterminé";
  117.                 break;
  118.         }
  119.         return $sexeTexte;
  120.     }
  121.     /**
  122.      * Indique si l'enfant est de sexe féminin
  123.      *
  124.      * @return boolean
  125.      */
  126.     function isFeminin(){
  127.         return $this->sexe == 'F';
  128.     }
  129.     /**
  130.      * Obtient la liste des accueils actifs
  131.      * @return boolean
  132.      */
  133.     public function getAccueilsActif()
  134.     {
  135.         $accueilsActifs = array();
  136.         foreach ($this->accueils as $accueil) {
  137.             if ($accueil->getDateFin() === null || $accueil->getDateFin() >= new \DateTime()) {
  138.                 $accueilsActifs[] = $accueil;
  139.             }
  140.         }
  141.         return $accueilsActifs;
  142.     }
  143.     /*
  144.      *  Vérifier que le responsable légal n'est pas le dernier responsable lié à l'enfant
  145.      */
  146.     public function hasUnSeulResponsableLegal()
  147.     {
  148.         $responsables $this->getResponsablesLegaux();
  149.         return $responsables->count() <= 1;
  150.     }
  151.     // </editor-fold>
  152.     // <editor-fold desc="Méthodes publiques">
  153.     /**
  154.      * Constructor
  155.      */
  156.     public function __construct()
  157.     { }
  158.     /**
  159.      * Obtient une nouvelle instance d'Enfant, initialisée à partir du tableau associatif fourni en paramètre
  160.      *
  161.      * @param array $data
  162.      * @return Enfant
  163.      */
  164.     public static function createFromArray(array $data)
  165.     {
  166.         $newInstance = new Enfant();
  167.         return $newInstance->initFromArray($data);
  168.     }
  169.     /**
  170.      * mappe les propriétés sur l'instance à partir d'un tableau associatif
  171.      *
  172.      * @param array $data
  173.      * @return void
  174.      */
  175.     public function initFromArray(array $data)
  176.     {
  177.         foreach ($data as $prop => $value) {
  178.             $this->{$prop} = $value;
  179.         }
  180.         return $this;
  181.     }
  182.     public function __toString()
  183.     {
  184.         return json_encode($this->jsonSerialize());
  185.     }
  186.     /*
  187.     * Implémentation de JsonSerializable
  188.     * Ne liste que les champs modifiables par les utilisateurs
  189.     */
  190.     public function jsonSerialize()
  191.     {
  192.         return [
  193.             'id' => $this->id,
  194.             'nom_famille' => $this->nom_famille,
  195.             'prenom' => $this->prenom,
  196.             'date_naissance' => $this->date_naissance === null null $this->date_naissance->format('d-m-Y'),
  197.             'responsables_legaux' => array_map(function ($rl) {
  198.                 return $rl->jsonSerialize();
  199.             }, is_array($this->responsables_legaux) ? $this->responsables_legaux $this->responsables_legaux->toArray())
  200.         ];
  201.     }
  202.     // </editor-fold>
  203. }