Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
],
"require": {
"php": ">=8.1"
"php": ">=8.1",
"ext-simplexml": "*"
}
}
Empty file removed public/app/.gitignore
Empty file.
27 changes: 27 additions & 0 deletions public/app/blog-post/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Solid\App\BlogPost\Application\Edit\EditHandler;
use Solid\App\BlogPost\Application\Find\FindHandler;
use Solid\App\BlogPost\Domain\Serializer\ObjectSerializer;
use Solid\App\BlogPost\Infrastructure\Hydration\ObjectHydrator;
use Solid\App\BlogPost\Infrastructure\Storage\FileStorage;
use Solid\App\BlogPost\Presentation\Controller\EditAction;
use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer;

require __DIR__.'/../../../vendor/autoload.php';

$storage = new FileStorage(
__DIR__.'/../../../var/data/blog_post.dat',
new ObjectHydrator(),
new ObjectSerializer(),
);

$controller = new EditAction(
new FindHandler($storage),
new EditHandler($storage),
new HtmlRenderer(),
);
$response = $controller(intval($_GET['id'] ?? 0));
$response->send();
25 changes: 25 additions & 0 deletions public/app/blog-post/list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Solid\App\BlogPost\Application\Search\SearchHandler;
use Solid\App\BlogPost\Domain\Serializer\ObjectSerializer;
use Solid\App\BlogPost\Infrastructure\Hydration\ObjectHydrator;
use Solid\App\BlogPost\Infrastructure\Storage\FileStorage;
use Solid\App\BlogPost\Presentation\Controller\ListAction;
use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer;

require __DIR__.'/../../../vendor/autoload.php';

$storage = new FileStorage(
__DIR__.'/../../../var/data/blog_post.dat',
new ObjectHydrator(),
new ObjectSerializer(),
);

$controller = new ListAction(
new SearchHandler($storage),
new HtmlRenderer(),
);
$response = $controller();
$response->send();
25 changes: 25 additions & 0 deletions public/app/blog-post/new.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Solid\App\BlogPost\Application\Create\CreateHandler;
use Solid\App\BlogPost\Domain\Serializer\ObjectSerializer;
use Solid\App\BlogPost\Infrastructure\Hydration\ObjectHydrator;
use Solid\App\BlogPost\Infrastructure\Storage\FileStorage;
use Solid\App\BlogPost\Presentation\Controller\CreateAction;
use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer;

require __DIR__.'/../../../vendor/autoload.php';

$storage = new FileStorage(
__DIR__.'/../../../var/data/blog_post.dat',
new ObjectHydrator(),
new ObjectSerializer(),
);

$controller = new CreateAction(
new CreateHandler($storage),
new HtmlRenderer(),
);
$response = $controller(intval($_GET['id'] ?? 0));
$response->send();
40 changes: 40 additions & 0 deletions public/app/blog-post/show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

use Solid\App\BlogPost\Application\Find\FindHandler;
use Solid\App\BlogPost\Domain\Serializer\ObjectSerializer;
use Solid\App\BlogPost\Infrastructure\Hydration\ObjectHydrator;
use Solid\App\BlogPost\Infrastructure\Storage\FileStorage;
use Solid\App\BlogPost\Presentation\Controller\ShowAction;
use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer;
use Solid\App\BlogPost\Presentation\Renderer\JsonRenderer;
use Solid\App\BlogPost\Presentation\Renderer\RendererStrategy;
use Solid\App\BlogPost\Presentation\Renderer\XmlRenderer;

require __DIR__.'/../../../vendor/autoload.php';

try {
$renderer = (new RendererStrategy([
'html' => new HtmlRenderer(),
'json' => new JsonRenderer(new ObjectSerializer()),
'xml' => new XmlRenderer(new ObjectSerializer()),
]))->find($_GET['format'] ?? 'html');
} catch (InvalidArgumentException $e) {
echo $e->getMessage();

return;
}

$storage = new FileStorage(
__DIR__.'/../../../var/data/blog_post.dat',
new ObjectHydrator(),
new ObjectSerializer(),
);

$controller = new ShowAction(
new FindHandler($storage),
$renderer,
);
$response = $controller(intval($_GET['id'] ?? 0));
$response->send();
Empty file removed src/App/.gitignore
Empty file.
35 changes: 35 additions & 0 deletions src/App/BlogPost/Application/Create/CreateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Application\Create;

use Solid\App\BlogPost\Domain\Entity\BlogPost;
use Solid\App\BlogPost\Domain\Storage\Storage;

class CreateHandler
{
public function __construct(private readonly Storage $storage)
{
}

public function handle(): BlogPost
{
$loripsum = explode("\n", file_get_contents('https://loripsum.net/api/1/long/headers'));

$blogPost = new BlogPost(random_int(1, 1000), strip_tags($loripsum[0]), $loripsum[2], 'John Doe');

$this->storage->save($blogPost);

return $blogPost;
}
}
34 changes: 34 additions & 0 deletions src/App/BlogPost/Application/Edit/EditHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Application\Edit;

use Solid\App\BlogPost\Domain\Entity\BlogPost;
use Solid\App\BlogPost\Domain\Storage\Storage;

class EditHandler
{
public function __construct(private readonly Storage $storage)
{
}

public function handle(BlogPost $blogPost): void
{
$loripsum = explode("\n", file_get_contents('https://loripsum.net/api/1/long/headers'));

$blogPost->setTitle(strip_tags($loripsum[0]));
$blogPost->setContent($loripsum[2]);

$this->storage->save($blogPost);
}
}
29 changes: 29 additions & 0 deletions src/App/BlogPost/Application/Find/FindHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Application\Find;

use Solid\App\BlogPost\Domain\Entity\BlogPost;
use Solid\App\BlogPost\Domain\Storage\Storage;

class FindHandler
{
public function __construct(private readonly Storage $storage)
{
}

public function handle(int $id): ?BlogPost
{
return $this->storage->find($id);
}
}
28 changes: 28 additions & 0 deletions src/App/BlogPost/Application/Search/SearchHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Application\Search;

use Solid\App\BlogPost\Domain\Storage\Storage;

class SearchHandler
{
public function __construct(private readonly Storage $storage)
{
}

public function handle(): array
{
return $this->storage->findAll();
}
}
78 changes: 78 additions & 0 deletions src/App/BlogPost/Domain/Entity/BlogPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Domain\Entity;

use DateTimeImmutable;

class BlogPost
{
private readonly DateTimeImmutable $created_at;
private ?DateTimeImmutable $updated_at = null;

public function __construct(
private readonly int $id,
private string $title,
private string $content,
private string $author,
) {
$this->created_at = new DateTimeImmutable();
}

public function getId(): int
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function getContent(): string
{
return $this->content;
}

public function getAuthor(): string
{
return $this->author;
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->created_at;
}

public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updated_at;
}

public function setTitle(string $title): void
{
$this->title = $title;
$this->updateAt();
}

public function setContent(string $content): void
{
$this->content = $content;
$this->updateAt();
}

private function updateAt(): void
{
$this->updated_at = new DateTimeImmutable();
}
}
31 changes: 31 additions & 0 deletions src/App/BlogPost/Domain/Serializer/ObjectSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Domain\Serializer;

use Solid\App\BlogPost\Domain\Entity\BlogPost;

class ObjectSerializer implements Serializer
{
public function serialize(BlogPost $blogPost): array
{
return [
'id' => $blogPost->getId(),
'author' => $blogPost->getAuthor(),
'title' => $blogPost->getTitle(),
'content' => $blogPost->getContent(),
'created_at' => $blogPost->getCreatedAt()->format('c'),
'updated_at' => $blogPost->getUpdatedAt()?->format('c'),
];
}
}
21 changes: 21 additions & 0 deletions src/App/BlogPost/Domain/Serializer/Serializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Second package.
*
* © Second <contact@scnd.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Solid\App\BlogPost\Domain\Serializer;

use Solid\App\BlogPost\Domain\Entity\BlogPost;

interface Serializer
{
public function serialize(BlogPost $blogPost): array;
}
Loading