src/Products/NotificationsBundle/Entity/Student.php line 47

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity;
  3. use App\Entity\Shared\DiscardableInterface;
  4. use App\Entity\Shared\DiscardableTrait;
  5. use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableInterface;
  6. use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableTrait;
  7. use Cms\TenantBundle\Entity\TenantedEntity;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\Common\Collections\Criteria;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Products\NotificationsBundle\Util\ReachabilityTrait;
  13. /**
  14.  * Class Student
  15.  * @package Products\NotificationsBundle\Entity
  16.  *
  17.  * @ORM\Entity(
  18.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\StudentRepository",
  19.  * )
  20.  * @ORM\Table(
  21.  *     name = "notis__student",
  22.  *     indexes = {
  23.  *         @ORM\Index(
  24.  *             name = "idx__onerosterId",
  25.  *             columns = {
  26.  *                 "onerosterId",
  27.  *             },
  28.  *         ),
  29.  *         @ORM\Index(
  30.  *             name = "idx__discarded",
  31.  *             columns = {
  32.  *                 "discarded",
  33.  *             },
  34.  *         ),
  35.  *         @ORM\Index(
  36.  *             name = "idx__discardedAt",
  37.  *             columns = {
  38.  *                 "discardedAt",
  39.  *             },
  40.  *         ),
  41.  *     },
  42.  * )
  43.  */
  44. class Student extends TenantedEntity implements OneRosterableInterfaceDiscardableInterface
  45. {
  46.     use ReachabilityTrait;
  47.     use OneRosterableTrait;
  48.     use DiscardableTrait;
  49.     /**
  50.      * @var string|null
  51.      *
  52.      * @ORM\Column(
  53.      *     type = "string",
  54.      *     nullable = false,
  55.      * )
  56.      */
  57.     protected ?string $firstName null;
  58.     /**
  59.      * @var string|null
  60.      *
  61.      * @ORM\Column(
  62.      *     type = "string",
  63.      *     nullable = true,
  64.      * )
  65.      */
  66.     protected ?string $lastName null;
  67.     /**
  68.      * @var Collection|ProfileRelationship[]
  69.      *
  70.      * @ORM\OneToMany(
  71.      *     targetEntity = "Products\NotificationsBundle\Entity\ProfileRelationship",
  72.      *     mappedBy = "student",
  73.      * )
  74.      */
  75.     protected Collection $relationships;
  76.     /**
  77.      * @var array|null
  78.      *
  79.      * @ORM\Column(
  80.      *     type = "json",
  81.      *     nullable = true,
  82.      * )
  83.      */
  84.     protected ?array $metadata = [];
  85.     /**
  86.      *
  87.      */
  88.     public function __construct()
  89.     {
  90.         $this->relationships = new ArrayCollection();
  91.     }
  92.     /**
  93.      * @return array
  94.      */
  95.     public function getMetadata(): array
  96.     {
  97.         return $this->metadata ?? [];
  98.     }
  99.     /**
  100.      * @param array|null $metadata
  101.      * @return $this
  102.      */
  103.     public function setMetadata(?array $metadata): self
  104.     {
  105.         $this->metadata $metadata ?: null;
  106.         return $this;
  107.     }
  108.     /**
  109.      * @return string|null
  110.      */
  111.     public function getFirstName(): ?string
  112.     {
  113.         return $this->firstName;
  114.     }
  115.     /**
  116.      * @param string|null $firstName
  117.      * @return $this
  118.      */
  119.     public function setFirstName(?string $firstName): self
  120.     {
  121.         $this->firstName $firstName ?: null;
  122.         return $this;
  123.     }
  124.     /**
  125.      * @return string|null
  126.      */
  127.     public function getLastName(): ?string
  128.     {
  129.         return $this->lastName;
  130.     }
  131.     /**
  132.      * @param string|null $lastName
  133.      * @return $this
  134.      */
  135.     public function setLastName(?string $lastName): self
  136.     {
  137.         $this->lastName $lastName ?: null;
  138.         return $this;
  139.     }
  140.     /**
  141.      * @param Criteria|null $criteria
  142.      * @return Collection|ProfileRelationship[]
  143.      */
  144.     public function getRelationships(?Criteria $criteria null): Collection
  145.     {
  146.         if ( ! $this->relationships instanceof Collection) {
  147.             $this->relationships = new ArrayCollection();
  148.         }
  149.         if ( ! empty($criteria)) {
  150.             return $this->relationships->matching($criteria);
  151.         }
  152.         return $this->relationships;
  153.     }
  154.     /**
  155.      * @param Criteria|null $criteria
  156.      * @return Collection|Student[]
  157.      */
  158.     public function getProfiles(?Criteria $criteria null): Collection
  159.     {
  160.         return $this->getRelationships($criteria)->map(function (ProfileRelationship $relationship) {
  161.             return $relationship->getProfile();
  162.         });
  163.     }
  164.     /**
  165.      * @return string|null
  166.      */
  167.     public function getFullName(): ?string
  168.     {
  169.         return trim(sprintf(
  170.             '%s %s',
  171.             $this->firstName,
  172.             $this->lastName
  173.         )) ?? null;
  174.     }
  175.     /**
  176.      * @return string|null
  177.      */
  178.     public function getCensoredName(): ?string
  179.     {
  180.         return trim(sprintf(
  181.             '%s %s',
  182.             ($this->getFirstName()) ? strtoupper(substr($this->getFirstName(), 01)) : '',
  183.             $this->getLastName() ?: ''
  184.         )) ?: null;
  185.     }
  186.     /**
  187.      * @return string|null
  188.      */
  189.     public function getSortName(): ?string
  190.     {
  191.         return trim(sprintf(
  192.             '%s, %s',
  193.             $this->lastName,
  194.             $this->firstName
  195.         ), " \t\n\r\0\x0B,") ?? null;
  196.     }
  197.     /**
  198.      * @return array
  199.      */
  200.     public function getMetadataSchools(): array
  201.     {
  202.         $metadata $this->getMetadata();
  203.         return ($metadata && isset($metadata['_schools'])) ? $metadata['_schools'] : [];
  204.     }
  205.     /**
  206.      * @return string|null
  207.      */
  208.     public function getMetadataPrimarySchool(): ?string
  209.     {
  210.         return current($this->getMetadataSchools()) ?: null;
  211.     }
  212.     /**
  213.      * @return array
  214.      */
  215.     public function getMetadataGrades(): array
  216.     {
  217.         $metadata $this->getMetadata();
  218.         return ($metadata && isset($metadata['_grades'])) ? $metadata['_grades'] : [];
  219.     }
  220.     /**
  221.      * @return string|null
  222.      */
  223.     public function getMetadataPrimaryGrade(): ?string
  224.     {
  225.         return current($this->getMetadataGrades()) ?: null;
  226.     }
  227.     /**
  228.      * @return array
  229.      */
  230.     public function jsonSerialize(): array
  231.     {
  232.         return [
  233.             'metadata' => $this->getMetadata(),
  234.         ];
  235.     }
  236. }