due = new \DateTime('now'); $this->due->modify('+1 day'); $this->createdAt = new \DateTimeImmutable('now'); $this->createdBy = new NullAccount(); } /** * Get to * * @return RelationAbstract[] * * @since 1.0.0 */ public function getTo() : array { $to = []; foreach ($this->accRelation as $acc) { if ($acc->getDuty() === DutyType::TO) { $to[] = $acc; } } foreach ($this->grpRelation as $grp) { if ($grp->getDuty() === DutyType::TO) { $to[] = $grp; } } return $to; } /** * Check if user is in to * * @param int $id User id * * @return bool * * @since 1.0.0 */ public function isToAccount(int $id) : bool { foreach ($this->accRelation as $acc) { if ($acc->getDuty() === DutyType::TO && $acc->getRelation()->id === $id ) { return true; } } return false; } /** * Check if group is in to * * @param int $id Group id * * @return bool * * @since 1.0.0 */ public function isToGroup(int $id) : bool { foreach ($this->grpRelation as $grp) { if ($grp->getDuty() === DutyType::TO && $grp->getRelation()->id === $id ) { return true; } } return false; } /** * Check if user is in cc * * @param int $id User id * * @return bool * * @since 1.0.0 */ public function isCCAccount(int $id) : bool { foreach ($this->accRelation as $acc) { if ($acc->getDuty() === DutyType::CC && $acc->getRelation()->id === $id ) { return true; } } return false; } /** * Check if group is in cc * * @param int $id Group id * * @return bool * * @since 1.0.0 */ public function isCCGroup(int $id) : bool { foreach ($this->grpRelation as $grp) { if ($grp->getDuty() === DutyType::CC && $grp->getRelation()->id === $id ) { return true; } } return false; } /** * Add to * * @param Group|Account $to To * * @return void * * @since 1.0.0 */ public function addTo($to) : void { if ($to instanceof Group) { $this->addGroupTo($to); } elseif (($to instanceof Account)) { $this->addAccountTo($to); } } /** * Add group as to * * @param Group $group Group * * @return void * * @since 1.0.0 */ public function addGroupTo(Group $group) : void { $groupId = $group->id; foreach ($this->grpRelation as $grp) { $grpId = $grp->getRelation()->id; if ($grpId === $groupId && $grp->getDuty() === DutyType::TO) { return; } } $this->grpRelation[] = new GroupRelation($group, DutyType::TO); } /** * Add account as to * * @param Account $account Account * * @return void * * @since 1.0.0 */ public function addAccountTo(Account $account) : void { $accountId = $account->id; foreach ($this->accRelation as $acc) { $accId = $acc->getRelation()->id; if ($accId === $accountId && $acc->getDuty() === DutyType::TO) { return; } } $this->accRelation[] = new AccountRelation($account, DutyType::TO); } /** * Get cc * * @return RelationAbstract[] * * @since 1.0.0 */ public function getCC() : array { $cc = []; foreach ($this->accRelation as $acc) { if ($acc->getDuty() === DutyType::CC) { $cc[] = $acc; } } foreach ($this->grpRelation as $grp) { if ($grp->getDuty() === DutyType::CC) { $cc[] = $grp; } } return $cc; } /** * Add cc * * @param Group|Account $cc CC * * @return void * * @since 1.0.0 */ public function addCC($cc) : void { if ($cc instanceof Group) { $this->addGroupCC($cc); } elseif (($cc instanceof Account)) { $this->addAccountCC($cc); } } /** * Add group as cc * * @param Group $group Group * * @return void * * @since 1.0.0 */ public function addGroupCC(Group $group) : void { $groupId = $group->id; foreach ($this->grpRelation as $grp) { $grpId = $grp->getRelation()->id; if ($grpId === $groupId && $grp->getDuty() === DutyType::CC) { return; } } $this->grpRelation[] = new GroupRelation($group, DutyType::CC); } /** * Add account as cc * * @param Account $account Account * * @return void * * @since 1.0.0 */ public function addAccountCC(Account $account) : void { $accountId = $account->id; foreach ($this->accRelation as $acc) { $accId = $acc->getRelation()->id; if ($accId === $accountId && $acc->getDuty() === DutyType::CC) { return; } } $this->accRelation[] = new AccountRelation($account, DutyType::CC); } /** * {@inheritdoc} */ public function toArray() : array { return [ 'id' => $this->id, 'task' => $this->task, 'createdBy' => $this->createdBy, 'createdAt' => $this->createdAt, 'description' => $this->description, 'descriptionRaw' => $this->descriptionRaw, 'status' => $this->status, 'to' => $this->getTo(), 'cc' => $this->getCC(), 'due' => $this->due, ]; } /** * {@inheritdoc} */ public function jsonSerialize() : mixed { return $this->toArray(); } use \Modules\Media\Models\MediaListTrait; }