Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desafio back-end finalizado #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Empty file added App/.gitkeep
Empty file.
14 changes: 14 additions & 0 deletions App/Controller/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Controller;

use App\Http\Response;

class IndexController
{
public function notFound()
{
$response = new Response ;
$response->status(400)->toJSON(['error' => "Error"]);
}
}
11 changes: 11 additions & 0 deletions App/Http/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Http;

class App
{
public static function run()
{

}
}
14 changes: 14 additions & 0 deletions App/Http/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http;

class Config
{
private static $config;

public static function get($id, $default = null)
{

return !empty(self::$config[$id])?self::$config[$id]:$default;
}
}
33 changes: 33 additions & 0 deletions App/Http/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http;

class Request
{
public $Params;
public $Method;
public $Type;

public function getFull()
{
$full = [];
foreach ($_POST as $ID => $value) {
$full[$ID] = filter_input(INPUT_POST, $ID, FILTER_SANITIZE_SPECIAL_CHARS);
}
return $full;
}

public function getJSON()
{
$full_inner = trim(file_get_contents("php://input"));
$full_cod = json_decode($full_inner);
return $full_cod;
}

public function __construct($Params = [])
{
$this->params = $Params;
$this->Method = trim($_SERVER['REQUEST_METHOD']);
$this->Type = !empty($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
}
}
21 changes: 21 additions & 0 deletions App/Http/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http;

class Response
{
private $status = 200;

public function status(int $code)
{
$this->status = $code;
return $this;
}

public function toJSON($data = [])
{
http_response_code($this->status);
header('Content-Type: application/json');
echo json_encode($data);
}
}
44 changes: 44 additions & 0 deletions App/Model/Conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php namespace App\Model;

use App\Http\Config;

class Conversion
{
private static $DATA = [];

public static function all()
{
return self::$DATA;
}

public static function add($value_o)
{
$value_o->moeda = count(self::$DATA) + 1;
self::$DATA[] = $value_o;
self::save();
return $value_o;
}

public static function findById( $id)
{
foreach (self::$DATA as $value_o) {

if ($value_o->moeda === $id) {
return $value_o;
}
}
return [];
}

public static function load()
{
$DB_PATH = Config::get('DB_PATH', __DIR__ . '/../../db.json');
self::$DATA = json_decode(file_get_contents($DB_PATH));
}

public static function save()
{
$DB_PATH = Config::get('DB_PATH', __DIR__ . '/../../db.json');
file_put_contents($DB_PATH, json_encode(self::$DATA, JSON_PRETTY_PRINT));
}
}
38 changes: 38 additions & 0 deletions App/Routes/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Routes;

use App\Controller\IndexController;
use App\Http\Request;
use App\Http\Response;

class Router
{
public static function get($ROOT, $CALL)
{
self::on($ROOT, $CALL);
}

public static function post($ROOT, $CALL)
{
self::on($ROOT, $CALL);
}

public static function on($RX, $BOOT)
{
$PARSET = $_SERVER['REQUEST_URI'];
$PARSET = (stripos($PARSET, "/") !== 0) ? "/" . $PARSET : $PARSET;
$RX = str_replace('/', '\/', $RX);
$is_match = preg_match('/^' . ($RX) . '$/', $PARSET, $MXA, PREG_OFFSET_CAPTURE);

if ($is_match) {
array_shift($MXA);
$PARSET = array_map(function ($ITEM) {
return $ITEM[0];
}, $MXA);
$BOOT(new Request($PARSET), new Response());
} else {
(new IndexController())->notFound();
}
}
}
10 changes: 3 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
"name": "apiki/back-end-challenge",
"description": "Desafio para candidatos a back-end.",
"type": "project",
"require": {
"php": ">= 7.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
"codeception/codeception": "^4.1",
"codeception/module-phpbrowser": "^1.0.0",
"codeception/module-rest": "^1.0.0"
"codeception/codeception": "^3.0"
},
"license": "MIT",
"scripts": {
"testOnWindows": "call vendor/bin/codecept run",
"test": "./vendor/bin/codecept run",
"lint": "./vendor/bin/phpcs src/",
"lint:fix": "./vendor/bin/phpcbf src/"
},
"autoload": {
"psr-4": {
"App\\": "src/"
"App\\": "App/"
}
}
}
Loading