From 4d0ddae9e6aa38dbd7d4ec351bac730c8e58a0a1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 5 Mar 2020 20:35:58 +0100 Subject: [PATCH] fixes Orange-Management/phpOMS#224 and fixes Orange-Management/phpOMS#212 --- Controller/ApiController.php | 5 +++-- Models/NewsArticle.php | 27 ++++++++++++++++----------- Models/NewsArticleMapper.php | 2 +- Models/NullNewsArticle.php | 11 +++++++++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 07f3188..85324d3 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -14,11 +14,12 @@ declare(strict_types=1); namespace Modules\News\Controller; +use Modules\Admin\Models\NullAccount; use Modules\News\Models\NewsArticle; use Modules\News\Models\NewsArticleMapper; use Modules\News\Models\NewsStatus; -use Modules\News\Models\NewsType; +use Modules\News\Models\NewsType; use phpOMS\Account\Account; use phpOMS\Localization\ISO639x1Enum; use phpOMS\Message\NotificationLevel; @@ -153,7 +154,7 @@ final class ApiController extends Controller private function createNewsArticleFromRequest(RequestAbstract $request) : NewsArticle { $newsArticle = new NewsArticle(); - $newsArticle->setCreatedBy($request->getHeader()->getAccount()); + $newsArticle->setCreatedBy(new NullAccount($request->getHeader()->getAccount())); $newsArticle->setPublish(new \DateTime((string) ($request->getData('publish') ?? 'now'))); $newsArticle->setTitle((string) ($request->getData('title') ?? '')); $newsArticle->setPlain($request->getData('plain') ?? ''); diff --git a/Models/NewsArticle.php b/Models/NewsArticle.php index f416b1b..0175f96 100644 --- a/Models/NewsArticle.php +++ b/Models/NewsArticle.php @@ -14,6 +14,9 @@ declare(strict_types=1); namespace Modules\News\Models; +use Modules\Admin\Models\Account; +use Modules\Admin\Models\NullAccount; +use Modules\Tag\Models\Tag; use phpOMS\Contract\ArrayableInterface; use phpOMS\Localization\ISO639x1Enum; use phpOMS\Stdlib\Base\Exception\InvalidEnumValue; @@ -95,10 +98,10 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable /** * Creator. * - * @var int|\Modules\Admin\Models\Account + * @var Account * @since 1.0.0 */ - private $createdBy = 0; + private Account $createdBy; /** * Publish. @@ -119,7 +122,7 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable /** * Badge. * - * @var array + * @var Tag[] * @since 1.0.0 */ private array $badges = []; @@ -131,14 +134,16 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable */ public function __construct() { + $this->createdBy = new NullAccount(); $this->createdAt = new \DateTime('now'); $this->publish = new \DateTime('now'); + $this->account = new NullAccount(); } /** * Get badges * - * @return array + * @return Tag[] * * @since 1.0.0 */ @@ -150,13 +155,13 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable /** * Add badge * - * @param mixed $badge Badge to add + * @param Tag $badge Badge to add * * @return void * * @since 1.0.0 */ - public function addBadge($badge) : void + public function addBadge(Tag $badge) : void { $this->badges[] = $badge; } @@ -296,11 +301,11 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable /** * Get created by * - * @return int|\phpOMS\Account\Account + * @return Account * * @since 1.0.0 */ - public function getCreatedBy() + public function getCreatedBy() : Account { return $this->createdBy; } @@ -308,15 +313,15 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable /** * Set created by * - * @param int $id Created by + * @param Account $account Created by * * @return void * * @since 1.0.0 */ - public function setCreatedBy($id) : void + public function setCreatedBy(Account $account) : void { - $this->createdBy = $id; + $this->createdBy = $account; } /** diff --git a/Models/NewsArticleMapper.php b/Models/NewsArticleMapper.php index 74f1bfd..43d5eb3 100644 --- a/Models/NewsArticleMapper.php +++ b/Models/NewsArticleMapper.php @@ -35,7 +35,7 @@ final class NewsArticleMapper extends DataMapperAbstract */ protected static array $columns = [ 'news_id' => ['name' => 'news_id', 'type' => 'int', 'internal' => 'id'], - 'news_created_by' => ['name' => 'news_created_by', 'type' => 'int', 'internal' => 'createdBy'], + 'news_created_by' => ['name' => 'news_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], 'news_publish' => ['name' => 'news_publish', 'type' => 'DateTime', 'internal' => 'publish'], 'news_title' => ['name' => 'news_title', 'type' => 'string', 'internal' => 'title'], 'news_plain' => ['name' => 'news_plain', 'type' => 'string', 'internal' => 'plain'], diff --git a/Models/NullNewsArticle.php b/Models/NullNewsArticle.php index 2200261..94fbb3f 100644 --- a/Models/NullNewsArticle.php +++ b/Models/NullNewsArticle.php @@ -24,4 +24,15 @@ namespace Modules\News\Models; */ final class NullNewsArticle extends NewsArticle { + /** + * Constructor + * + * @param int $id Model id + * + * @since 1.0.0 + */ + public function __construct(int $id = 0) + { + $this->id = $id; + } }