First commit
This commit is contained in:
43
app/Controllers/CategoriesController.php
Normal file
43
app/Controllers/CategoriesController.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Controllers;
|
||||
|
||||
use KamilCraftApi\App\Models\Category;
|
||||
use KamilCraftApi\Request\Request;
|
||||
use KamilCraftApi\Response;
|
||||
|
||||
class CategoriesController extends Controller
|
||||
{
|
||||
private Category $category;
|
||||
|
||||
public function __construct(Request $request) {
|
||||
parent::__construct();
|
||||
$this->category = new Category($request);
|
||||
}
|
||||
|
||||
public function showWhereName(string $slug): Response
|
||||
{
|
||||
if ( ! ($category = $this->category->getCategoryWhereName($slug)) ) {
|
||||
return (new Response())->json([
|
||||
'message' => 'Not found category'
|
||||
], 404);
|
||||
}
|
||||
return (new Response())->json($category);
|
||||
}
|
||||
|
||||
public function show(int $id): Response
|
||||
{
|
||||
if ( ! ($category = $this->category->getCategory($id)) ) {
|
||||
return (new Response())->json([
|
||||
'message' => 'Not found category'
|
||||
], 404);
|
||||
}
|
||||
return (new Response())->json($category);
|
||||
}
|
||||
|
||||
public function __invoke(): Response
|
||||
{
|
||||
$categories = $this->category;
|
||||
return (new Response())->json($categories());
|
||||
}
|
||||
}
|
21
app/Controllers/Controller.php
Normal file
21
app/Controllers/Controller.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Controllers;
|
||||
|
||||
use KamilCraftApi\Interfaces\ControllerInterface;
|
||||
use KamilCraftApi\Response;
|
||||
|
||||
class Controller implements ControllerInterface
|
||||
{
|
||||
private Response $response;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->response = new Response();
|
||||
}
|
||||
|
||||
protected function response(): Response
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
17
app/Controllers/HomeController.php
Normal file
17
app/Controllers/HomeController.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Controllers;
|
||||
|
||||
use KamilCraftApi\Interfaces\ControllerInterface;
|
||||
use KamilCraftApi\Request\Request;
|
||||
use KamilCraftApi\Response;
|
||||
|
||||
class HomeController implements ControllerInterface
|
||||
{
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
return (new Response())->json((object)[
|
||||
'message' => 'Hello World'
|
||||
]);
|
||||
}
|
||||
}
|
39
app/Controllers/ProjectController.php
Normal file
39
app/Controllers/ProjectController.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Controllers;
|
||||
|
||||
use KamilCraftApi\App\Models\Project;
|
||||
use KamilCraftApi\Response;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
private Project $project;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->project = new Project();
|
||||
}
|
||||
|
||||
public function showWhereCategory(string $category): Response
|
||||
{
|
||||
$projects = $this->project->getProjectWhereCategory($category);
|
||||
return (new Response())->json($projects);
|
||||
}
|
||||
|
||||
public function show(int $id): Response
|
||||
{
|
||||
if ( ! ($project = $this->project->getProject($id)) ) {
|
||||
return (new Response())->json([
|
||||
'message' => 'Not found project'
|
||||
], 404);
|
||||
}
|
||||
return (new Response())->json($project);
|
||||
}
|
||||
|
||||
public function __invoke(): Response
|
||||
{
|
||||
$projects = $this->project;
|
||||
return (new Response())->json($projects());
|
||||
}
|
||||
}
|
78
app/Models/Category.php
Normal file
78
app/Models/Category.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Models;
|
||||
|
||||
use KamilCraftApi\Request\Request;
|
||||
use KamilCraftApi\Response;
|
||||
use stdClass;
|
||||
|
||||
class Category
|
||||
{
|
||||
const JSON_PATH = ROOT_PATH . 'resources/json';
|
||||
private array $allCategory = [];
|
||||
|
||||
public function __construct(
|
||||
private Request $request
|
||||
) {
|
||||
$categories = file_get_contents(self::JSON_PATH . '/Categories.json');
|
||||
if ( $category = json_decode($categories) ) {
|
||||
$this->allCategory = $category;
|
||||
} else {
|
||||
$this->parseError();
|
||||
}
|
||||
}
|
||||
|
||||
public function getCategoryWhereName(string $slug): object|bool
|
||||
{
|
||||
if ( ( $key = array_search($slug, array_column($this->allCategory, 'slug') ) ) !== false ) {
|
||||
$category = $this->allCategory[$key];
|
||||
$category->default = $category->default ?? false;
|
||||
$category->visible = $category->visible ?? true;
|
||||
return $category;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getCategory(int $id): object|bool
|
||||
{
|
||||
if ( ( $key = array_search($id, array_column($this->allCategory, 'id') ) ) !== false ) {
|
||||
$category = $this->allCategory[$key];
|
||||
$category->default = $category->default ?? false;
|
||||
$category->visible = $category->visible ?? true;
|
||||
return $category;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function parseError(): void
|
||||
{
|
||||
if ( $this->request->getContentType() === 'application/json' ) {
|
||||
(new Response())->json((object)[
|
||||
'message' => 'Server error'
|
||||
], 500)->render();
|
||||
} else {
|
||||
(new Response())(
|
||||
file_get_contents(ROOT_PATH . '/errors/error500.html'),
|
||||
500
|
||||
)->render();
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
public function __invoke(): array
|
||||
{
|
||||
$tmp_categories = [];
|
||||
foreach ($this->allCategory as $category) {
|
||||
$tmp_category = new stdClass;
|
||||
$tmp_category->id = $category->id;
|
||||
$tmp_category->name = $category->name;
|
||||
$tmp_category->slug = $category->slug;
|
||||
$tmp_category->default = $category->default ?? false;
|
||||
$tmp_category->visible = $category->visible ?? true;
|
||||
|
||||
$tmp_categories[] = $tmp_category;
|
||||
}
|
||||
|
||||
return $tmp_categories;
|
||||
}
|
||||
}
|
72
app/Models/Project.php
Normal file
72
app/Models/Project.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace KamilCraftApi\App\Models;
|
||||
|
||||
use stdClass;
|
||||
|
||||
class Project
|
||||
{
|
||||
const JSON_PATH = ROOT_PATH . 'resources/json';
|
||||
const SHORT_PROJECT = [
|
||||
'id',
|
||||
'title',
|
||||
'image',
|
||||
'categories',
|
||||
'version'
|
||||
];
|
||||
private array $allProjects = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$projects = file_get_contents(self::JSON_PATH . '/Projects.json');
|
||||
if ( $projects = json_decode($projects) ) {
|
||||
$this->allProjects = $projects;
|
||||
}
|
||||
}
|
||||
|
||||
public function getProject(int $id): object|bool
|
||||
{
|
||||
if ( ( $key = array_search($id, array_column($this->allProjects, 'id')) ) !== false ) {
|
||||
return $this->allProjects[$key];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getProjectWhereCategory(string $category): array
|
||||
{
|
||||
$listWhereCategory = [];
|
||||
foreach ( array_column($this->allProjects, 'categories') as $key => $value ) {
|
||||
if ( in_array($category, $value) ) {
|
||||
$listWhereCategory[] = $this->shortProjectSchema($this->allProjects[$key]);
|
||||
}
|
||||
}
|
||||
return $listWhereCategory;
|
||||
}
|
||||
|
||||
private function shortProjectSchema(object $project): object
|
||||
{
|
||||
$tmp_project = new stdClass;
|
||||
foreach ($project as $key => $value) {
|
||||
if ( in_array($key, self::SHORT_PROJECT) ) {
|
||||
$tmp_project->{$key} = $value;
|
||||
}
|
||||
}
|
||||
$tmp_project->short_description = substr(
|
||||
explode("\n", $project->description)[0],
|
||||
0,
|
||||
255
|
||||
);
|
||||
|
||||
return $tmp_project;
|
||||
}
|
||||
|
||||
public function __invoke(): array
|
||||
{
|
||||
$arr_map = [];
|
||||
foreach ($this->allProjects as $project) {
|
||||
$arr_map[] = $this->shortProjectSchema($project);
|
||||
}
|
||||
|
||||
return $arr_map;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user