<?php
namespace Products\NotificationsBundle\Entity;
use App\Entity\Shared\DiscardableInterface;
use App\Entity\Shared\DiscardableTrait;
use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableInterface;
use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableTrait;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Products\NotificationsBundle\Util\ReachabilityTrait;
/**
* Class Student
* @package Products\NotificationsBundle\Entity
*
* @ORM\Entity(
* repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\StudentRepository",
* )
* @ORM\Table(
* name = "notis__student",
* indexes = {
* @ORM\Index(
* name = "idx__onerosterId",
* columns = {
* "onerosterId",
* },
* ),
* @ORM\Index(
* name = "idx__discarded",
* columns = {
* "discarded",
* },
* ),
* @ORM\Index(
* name = "idx__discardedAt",
* columns = {
* "discardedAt",
* },
* ),
* },
* )
*/
class Student extends TenantedEntity implements OneRosterableInterface, DiscardableInterface
{
use ReachabilityTrait;
use OneRosterableTrait;
use DiscardableTrait;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $firstName = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $lastName = null;
/**
* @var Collection|ProfileRelationship[]
*
* @ORM\OneToMany(
* targetEntity = "Products\NotificationsBundle\Entity\ProfileRelationship",
* mappedBy = "student",
* )
*/
protected Collection $relationships;
/**
* @var array|null
*
* @ORM\Column(
* type = "json",
* nullable = true,
* )
*/
protected ?array $metadata = [];
/**
*
*/
public function __construct()
{
$this->relationships = new ArrayCollection();
}
/**
* @return array
*/
public function getMetadata(): array
{
return $this->metadata ?? [];
}
/**
* @param array|null $metadata
* @return $this
*/
public function setMetadata(?array $metadata): self
{
$this->metadata = $metadata ?: null;
return $this;
}
/**
* @return string|null
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @param string|null $firstName
* @return $this
*/
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName ?: null;
return $this;
}
/**
* @return string|null
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param string|null $lastName
* @return $this
*/
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName ?: null;
return $this;
}
/**
* @param Criteria|null $criteria
* @return Collection|ProfileRelationship[]
*/
public function getRelationships(?Criteria $criteria = null): Collection
{
if ( ! $this->relationships instanceof Collection) {
$this->relationships = new ArrayCollection();
}
if ( ! empty($criteria)) {
return $this->relationships->matching($criteria);
}
return $this->relationships;
}
/**
* @param Criteria|null $criteria
* @return Collection|Student[]
*/
public function getProfiles(?Criteria $criteria = null): Collection
{
return $this->getRelationships($criteria)->map(function (ProfileRelationship $relationship) {
return $relationship->getProfile();
});
}
/**
* @return string|null
*/
public function getFullName(): ?string
{
return trim(sprintf(
'%s %s',
$this->firstName,
$this->lastName
)) ?? null;
}
/**
* @return string|null
*/
public function getCensoredName(): ?string
{
return trim(sprintf(
'%s %s',
($this->getFirstName()) ? strtoupper(substr($this->getFirstName(), 0, 1)) : '',
$this->getLastName() ?: ''
)) ?: null;
}
/**
* @return string|null
*/
public function getSortName(): ?string
{
return trim(sprintf(
'%s, %s',
$this->lastName,
$this->firstName
), " \t\n\r\0\x0B,") ?? null;
}
/**
* @return array
*/
public function getMetadataSchools(): array
{
$metadata = $this->getMetadata();
return ($metadata && isset($metadata['_schools'])) ? $metadata['_schools'] : [];
}
/**
* @return string|null
*/
public function getMetadataPrimarySchool(): ?string
{
return current($this->getMetadataSchools()) ?: null;
}
/**
* @return array
*/
public function getMetadataGrades(): array
{
$metadata = $this->getMetadata();
return ($metadata && isset($metadata['_grades'])) ? $metadata['_grades'] : [];
}
/**
* @return string|null
*/
public function getMetadataPrimaryGrade(): ?string
{
return current($this->getMetadataGrades()) ?: null;
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return [
'metadata' => $this->getMetadata(),
];
}
}