oms-News/Models/NewsArticle.php
Dennis Eichhorn a8f4009521 update
2023-08-28 22:06:35 +00:00

389 lines
6.6 KiB
PHP
Executable File

<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package Modules\News\Models
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\News\Models;
use Modules\Admin\Models\Account;
use Modules\Admin\Models\NullAccount;
use Modules\Comments\Models\CommentList;
use Modules\Media\Models\Media;
use Modules\Tag\Models\NullTag;
use Modules\Tag\Models\Tag;
use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
/**
* News article class.
*
* @package Modules\News\Models
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
class NewsArticle implements \JsonSerializable
{
/**
* Article ID.
*
* @var int
* @since 1.0.0
*/
public int $id = 0;
/**
* Title.
*
* @var string
* @since 1.0.0
*/
public string $title = '';
/**
* Content.
*
* @var string
* @since 1.0.0
*/
public string $content = '';
/**
* Unparsed.
*
* @var string
* @since 1.0.0
*/
public string $plain = '';
/**
* News type.
*
* @var int
* @since 1.0.0
*/
public int $type = NewsType::ARTICLE;
/**
* News status.
*
* @var int
* @since 1.0.0
*/
public int $status = NewsStatus::DRAFT;
/**
* Language.
*
* @var string
* @since 1.0.0
*/
public string $language = ISO639x1Enum::_EN;
/**
* Unit
*
* @var null|int
* @since 1.0.0
*/
public ?int $unit = null;
/**
* Application
*
* @var null|int
* @since 1.0.0
*/
public ?int $app = null;
/**
* Created.
*
* @var \DateTimeImmutable
* @since 1.0.0
*/
public \DateTimeImmutable $createdAt;
/**
* Creator.
*
* @var Account
* @since 1.0.0
*/
public Account $createdBy;
/**
* Publish.
*
* @var \DateTime
* @since 1.0.0
*/
public \DateTime $publish;
/**
* Featured.
*
* @var bool
* @since 1.0.0
*/
public bool $isFeatured = false;
/**
* Tags.
*
* @var Tag[]
* @since 1.0.0
*/
private array $tags = [];
/**
* Comments
*
* @var null|CommentList
* @since 1.0.0
*/
public ?CommentList $comments = null;
/**
* Media files
*
* @var array
* @since 1.0.0
*/
protected array $media = [];
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->createdBy = new NullAccount();
$this->createdAt = new \DateTimeImmutable('now');
$this->publish = new \DateTime('now');
}
/**
* Get news language
*
* @return string
*
* @since 1.0.0
*/
public function getLanguage() : string
{
return $this->language;
}
/**
* Set language
*
* @param string $language News article language
*
* @return void
*
* @throws InvalidEnumValue
*
* @since 1.0.0
*/
public function setLanguage(string $language) : void
{
if (!ISO639x1Enum::isValidValue($language)) {
throw new InvalidEnumValue($language);
}
$this->language = $language;
}
/**
* Get news article type
*
* @return int
*
* @since 1.0.0
*/
public function getType() : int
{
return $this->type;
}
/**
* Set news article type
*
* @param int $type News article type
*
* @return void
*
* @throws InvalidEnumValue
*
* @since 1.0.0
*/
public function setType(int $type) : void
{
if (!NewsType::isValidValue($type)) {
throw new InvalidEnumValue((string) $type);
}
$this->type = $type;
}
/**
* Get news article status
*
* @return int
*
* @since 1.0.0
*/
public function getStatus() : int
{
return $this->status;
}
/**
* @param int $status News status
*
* @return void
*
* @throws InvalidEnumValue
*
* @since 1.0.0
*/
public function setStatus(int $status) : void
{
if (!NewsStatus::isValidValue($status)) {
throw new InvalidEnumValue((string) $status);
}
$this->status = $status;
}
/**
* Adding new tag.
*
* @param Tag $tag Tag
*
* @return int
*
* @since 1.0.0
*/
public function addTag(Tag $tag) : int
{
$this->tags[] = $tag;
\end($this->tags);
$key = (int) \key($this->tags);
\reset($this->tags);
return $key;
}
/**
* Remove Tag from list.
*
* @param int $id Tag
*
* @return bool
*
* @since 1.0.0
*/
public function removeTag($id) : bool
{
if (isset($this->tags[$id])) {
unset($this->tags[$id]);
return true;
}
return false;
}
/**
* Get task elements.
*
* @param int $id Element id
*
* @return Tag
*
* @since 1.0.0
*/
public function getTag(int $id) : Tag
{
return $this->tags[$id] ?? new NullTag();
}
/**
* Get task elements.
*
* @return Tag[]
*
* @since 1.0.0
*/
public function getTags() : array
{
return $this->tags;
}
/**
* Get all media
*
* @return Media[]
*
* @since 1.0.0
*/
public function getMedia() : array
{
return $this->media;
}
/**
* Add media
*
* @param Media $media Media to add
*
* @return void
*
* @since 1.0.0
*/
public function addMedia(Media $media) : void
{
$this->media[] = $media;
}
/**
* {@inheritdoc}
*/
public function toArray() : array
{
return [
'id' => $this->id,
'title' => $this->title,
'plain' => $this->plain,
'content' => $this->content,
'type' => $this->type,
'status' => $this->status,
'isFeatured' => $this->isFeatured,
'publish' => $this->publish,
'createdAt' => $this->createdAt,
'createdBy' => $this->createdBy,
];
}
/**
* {@inheritdoc}
*/
public function jsonSerialize() : mixed
{
return $this->toArray();
}
}