Skip to content

Commit

Permalink
Refactor News to News/Index & add jsonSerialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 2, 2024
1 parent 54a0dc5 commit 003fc59
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 59 deletions.
6 changes: 3 additions & 3 deletions src/Controllers/News.php → src/Controllers/News/Index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace BNETDocs\Controllers;
namespace BNETDocs\Controllers\News;

use \BNETDocs\Libraries\User\User;

class News extends Base
class Index extends \BNETDocs\Controllers\Base
{
public const NEWS_PER_PAGE = 5;

Expand All @@ -13,7 +13,7 @@ class News extends Base
*/
public function __construct()
{
$this->model = new \BNETDocs\Models\News();
$this->model = new \BNETDocs\Models\News\Index();
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Libraries/Core/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use \OutOfBoundsException;

class Pagination
class Pagination implements \JsonSerializable
{
private array $dataset;
private int $limit;
Expand Down Expand Up @@ -43,6 +43,16 @@ public function getPage(): array
return $set;
}

public function jsonSerialize(): mixed
{
return [
'current_page' => $this->currentPage(),
'page_count' => $this->pageCount(),
'items_per_page' => $this->limit,
'page_items' => $this->getPage(),
];
}

public function nextPage(): int
{
if ($this->page >= $this->pageCount())
Expand Down
10 changes: 0 additions & 10 deletions src/Models/News.php

This file was deleted.

19 changes: 19 additions & 0 deletions src/Models/News/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace BNETDocs\Models\News;

class Index extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
{
public bool $acl_allowed = false;
public ?array $news_posts = null;
public ?\BNETDocs\Libraries\Core\Pagination $pagination = null;

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'acl_allowed' => $this->acl_allowed,
'news_posts' => $this->news_posts,
'pagination' => $this->pagination,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates;
namespace BNETDocs\Templates\News;
use \BNETDocs\Libraries\User\User;
use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Pair;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace BNETDocs\Templates;
namespace BNETDocs\Templates\News;

use \CarlBennett\MVC\Libraries\Common;

Expand Down
17 changes: 17 additions & 0 deletions src/Views/News/IndexHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BNETDocs\Views\News;

class IndexHtml extends \BNETDocs\Views\Base\Html
{
public static function invoke(\BNETDocs\Interfaces\Model $model): void
{
if (!$model instanceof \BNETDocs\Models\News\Index)
{
throw new \BNETDocs\Exceptions\InvalidModelException($model);
}

(new \BNETDocs\Libraries\Core\Template($model, 'News/Index'))->invoke();
$model->_responseHeaders['Content-Type'] = self::mimeType();
}
}
17 changes: 17 additions & 0 deletions src/Views/News/IndexRSS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BNETDocs\Views\News;

class IndexRSS extends \BNETDocs\Views\Base\RSS
{
public static function invoke(\BNETDocs\Interfaces\Model $model): void
{
if (!$model instanceof \BNETDocs\Models\News\Index)
{
throw new \BNETDocs\Exceptions\InvalidModelException($model);
}

(new \BNETDocs\Libraries\Core\Template($model, 'News/Index.rss'))->invoke();
$model->_responseHeaders['Content-Type'] = self::mimeType();
}
}
17 changes: 0 additions & 17 deletions src/Views/NewsHtml.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Views/NewsRSS.php

This file was deleted.

19 changes: 10 additions & 9 deletions src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function main(): void
];
} else {
Router::$routes = [
['#^/$#', 'Core\\Legacy', ['Core\\LegacyHtml']],
['#^/\.well-known/change-password$#', 'Core\\RedirectSoft', ['Core\\RedirectSoftHtml', 'Core\\RedirectSoftJson', 'Core\\RedirectSoftPlain'], '/user/changepassword'],
['#^/$#', 'Core\\Legacy', ['Core\\LegacyHtml']],
['#^/comment/create/?$#', 'Comment\\Create', ['Comment\\CreateJson']],
['#^/comment/delete/?$#', 'Comment\\Delete', ['Comment\\DeleteHtml']],
['#^/comment/edit/?$#', 'Comment\\Edit', ['Comment\\EditHtml']],
Expand All @@ -63,39 +63,40 @@ function main(): void
['#^/document/create/?$#', 'Document\\Create', ['Document\\CreateHtml']],
['#^/document/delete/?$#', 'Document\\Delete', ['Document\\DeleteHtml']],
['#^/document/edit/?$#', 'Document\\Edit', ['Document\\EditHtml']],
['#^/document/index/?$#', 'Document\\Index', ['Document\\IndexHtml', 'Document\\IndexJson']],
['#^/document/index\.html?$#', 'Document\\Index', ['Document\\IndexHtml']],
['#^/document/index\.json$#', 'Document\\Index', ['Document\\IndexJson']],
['#^/document/index/?$#', 'Document\\Index', ['Document\\IndexHtml', 'Document\\IndexJson']],
['#^/donate/?$#', 'Donate', ['DonateHtml']],
['#^/eventlog/index/?$#', 'EventLog\\Index', ['EventLog\\IndexHtml']],
['#^/eventlog/view/?$#', 'EventLog\\View', ['EventLog\\ViewHtml']],
['#^/legal/?$#', 'Legal', ['LegalHtml', 'LegalPlain']],
['#^/legal\.html?$#', 'Legal', ['LegalHtml']],
['#^/legal\.txt$#', 'Legal', ['LegalPlain']],
['#^/legal/?$#', 'Legal', ['LegalHtml', 'LegalPlain']],
['#^/news/?$#', 'News', ['NewsHtml'], false],
['#^/news/?$#', 'News\\Index', ['News\\IndexHtml', 'News\\IndexRSS'], false],
['#^/news/(\d+)/?.*\.html?$#', 'News\\View', ['News\\ViewHtml']],
['#^/news/(\d+)/?.*\.json$#', 'News\\View', ['News\\ViewJson']],
['#^/news/(\d+)/?.*\.txt$#', 'News\\View', ['News\\ViewPlain']],
['#^/news/(\d+)/?#', 'News\\View', ['News\\ViewHtml', 'News\\ViewJson', 'News\\ViewPlain']],
['#^/news\.rss$#', 'News', ['NewsRSS'], true],
['#^/news/create/?$#', 'News\\Create', ['News\\CreateHtml']],
['#^/news/edit/?$#', 'News\\Edit', ['News\\EditHtml']],
['#^/news/delete/?$#', 'News\\Delete', ['News\\DeleteHtml']],
['#^/news/edit/?$#', 'News\\Edit', ['News\\EditHtml']],
['#^/news\.html?$#', 'News\\Index', ['News\\IndexHtml'], false],
['#^/news\.rss$#', 'News\\Index', ['News\\IndexRSS'], true],
['#^/packet/(\d+)/?.*\.html?$#', 'Packet\\View', ['Packet\\ViewHtml']],
['#^/packet/(\d+)/?.*\.json$#', 'Packet\\View', ['Packet\\ViewJson']],
['#^/packet/(\d+)/?.*\.txt$#', 'Packet\\View', ['Packet\\ViewPlain']],
['#^/packet/(\d+)/?#', 'Packet\\View', ['Packet\\ViewHtml', 'Packet\\ViewJson', 'Packet\\ViewPlain']],
['#^/packet/create/?$#', 'Packet\\Create', ['Packet\\CreateHtml']],
['#^/packet/delete/?$#', 'Packet\\Delete', ['Packet\\DeleteHtml']],
['#^/packet/edit/?$#', 'Packet\\Edit', ['Packet\\EditHtml']],
['#^/packet/index/?$#', 'Packet\\Index', ['Packet\\IndexHtml', 'Packet\\IndexJson'], false],
['#^/packet/index\.c(?:pp)?$#', 'Packet\\Index', ['Packet\\IndexCpp'], true],
['#^/packet/index\.go$#', 'Packet\\Index', ['Packet\\IndexGo'], true],
['#^/packet/index\.html?$#', 'Packet\\Index', ['Packet\\IndexHtml'], false],
['#^/packet/index\.java$#', 'Packet\\Index', ['Packet\\IndexJava'], true],
['#^/packet/index\.json$#', 'Packet\\Index', ['Packet\\IndexJson'], false],
['#^/packet/index\.php$#', 'Packet\\Index', ['Packet\\IndexPhp'], true],
['#^/packet/index\.vb$#', 'Packet\\Index', ['Packet\\IndexVb'], true],
['#^/packet/index/?$#', 'Packet\\Index', ['Packet\\IndexHtml', 'Packet\\IndexJson'], false],
['#^/phpinfo/?$#', 'Core\\PhpInfo', ['Core\\PhpInfoHtml']],
['#^/privacy(?:/|\.html?)?$#', 'PrivacyPolicy', ['PrivacyPolicyHtml']],
['#^/robots\.txt$#', 'Core\\Robotstxt', ['Core\\Robotstxt']],
Expand All @@ -107,12 +108,12 @@ function main(): void
['#^/server/delete/?$#', 'Server\\Delete', ['Server\\DeleteHtml']],
['#^/server/edit/?$#', 'Server\\Edit', ['Server\\EditHtml']],
['#^/server/updatejob\.json$#', 'Server\\UpdateJob', ['Server\\UpdateJobJson']],
['#^/servers/?$#', 'Server\\Index', ['Server\\IndexHtml', 'Server\\IndexJson']],
['#^/servers\.html?$#', 'Server\\Index', ['Server\\IndexHtml']],
['#^/servers\.json$#', 'Server\\Index', ['Server\\IndexJson']],
['#^/servers/?$#', 'Server\\Index', ['Server\\IndexHtml', 'Server\\IndexJson']],
['#^/status/?$#', 'Core\\Status', ['Core\\StatusJson', 'Core\\StatusPlain']],
['#^/status\.json$#', 'Core\\Status', ['Core\\StatusJson']],
['#^/status\.txt$#', 'Core\\Status', ['Core\\StatusPlain']],
['#^/status/?$#', 'Core\\Status', ['Core\\StatusJson', 'Core\\StatusPlain']],
['#^/user/(\d+)/?.*\.html?$#', 'User\\View', ['User\\ViewHtml']],
['#^/user/(\d+)/?.*\.json$#', 'User\\View', ['User\\ViewJson']],
['#^/user/(\d+)/?#', 'User\\View', ['User\\ViewHtml', 'User\\ViewJson']],
Expand Down

0 comments on commit 003fc59

Please sign in to comment.