public static function getInstance() { if (self::$uri_dispatcher_singleton == null) { self::$uri_dispatcher_singleton = new URIDispatcher(); } return self::$uri_dispatcher_singleton; }
} public function getByOwner($owner) { $currentUser = parent::authenticateUser(); $startDate = $this->request->getStartDate(); $endDate = $this->request->getEndDate(); $stocks = $this->stockDAO->findByOwnerAndFilter($owner, $startDate, $endDate); if ($stocks == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "The defined interval time not contains stocks"; return; } foreach ($stocks as $stock) { if ($stock->getOwner()->getLogin() != $currentUser->getLogin()) { header($this->server->getServerProtocol() . ' 403 Forbidden'); echo "you are not the owner of this stock"; return; } } $stock_array = []; foreach ($stocks as $stock) { array_push($stock_array, ["idStock" => $stock->getIdStock(), "date" => $stock->getDate(), "total" => $stock->getTotal(), "owner" => $stock->getOwner()->getLogin()]); } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($stock_array); } } $stockRest = new StockRest(); URIDispatcher::getInstance()->map("GET", "/stocks/\$1", array($stockRest, "getByOwner"))->map("POST", "/stocks", array($stockRest, "create"))->map("PUT", "/stocks/\$1", array($stockRest, "update"))->map("DELETE", "/stocks/\$1", array($stockRest, "delete"));
break; case 'chart': $revenues = $this->revenueDAO->findByOwnerAndFilter($owner, $startDate, $endDate); if ($revenues == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "The defined interval time not contains revenues"; return; } foreach ($revenues as $revenue) { if ($revenue->getOwner()->getLogin() != $currentUser->getLogin()) { header($this->server->getServerProtocol() . ' 403 Forbidden'); echo "you are not the owner of this revenue"; return; } } $revenues_array = []; foreach ($revenues as $revenue) { array_push($revenues_array, ["idRevenue" => $revenue->getIdRevenue(), "date" => $revenue->getDate(), "name" => $revenue->getName(), "quantity" => $revenue->getQuantity(), "owner" => $currentUser->getLogin()]); } break; default: break; } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($revenues_array); } } $revenueRest = new RevenueRest(); URIDispatcher::getInstance()->map("GET", "/revenues/\$1/\$2", [$revenueRest, "getByOwner"])->map("POST", "/revenues", [$revenueRest, "create"])->map("PUT", "/revenues/\$1", [$revenueRest, "update"])->map("DELETE", "/revenues/\$1", [$revenueRest, "delete"]);
break; case 'chart': $spendings = $this->spendingDAO->findByOwnerAndFilter($owner, $startDate, $endDate); if ($spendings == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "The defined interval time not contains spendings"; return; } foreach ($spendings as $spending) { if ($spending->getOwner()->getLogin() != $currentUser->getLogin()) { header($this->server->getServerProtocol() . ' 403 Forbidden'); echo "you are not the owner of this spending"; return; } } $spendings_array = []; foreach ($spendings as $spending) { array_push($spendings_array, ["idSpending" => $spending->getIdSpending(), "date" => $spending->getDate(), "name" => $spending->getName(), "quantity" => $spending->getQuantity(), "owner" => $currentUser->getLogin()]); } break; default: break; } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($spendings_array); } } $spendingRest = new SpendingRest(); URIDispatcher::getInstance()->map("GET", "/spendings/\$1/\$2", [$spendingRest, "getByOwner"])->map("POST", "/spendings", [$spendingRest, "create"])->map("PUT", "/spendings/\$1/", [$spendingRest, "update"])->map("DELETE", "/spendings/\$1", [$spendingRest, "delete"]);
if ($login != $currentUser->getLogin()) { header($this->server->getServerProtocol() . ' 403 Forbidden'); echo "You are not authorized to access this resource"; return; } $user = $this->userDAO->findByID($login); if ($user == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "User with login " . $login . " not found"; return; } $user_array = array("login" => $user->getLogin(), "password" => $user->getPassword(), "fullname" => $user->getFullname(), "email" => $user->getEmail(), "phone" => $user->getPhone(), "country" => $user->getCountry()); header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($user_array); } public function login($login) { $currentLogged = parent::authenticateUser(); if ($currentLogged->getLogin() != $login) { header($this->server->getServerProtocol() . ' 403 Forbidden'); echo "You are not authorized to login as anyone but you"; } else { header($this->server->getServerProtocol() . ' 200 Ok'); echo "Hello " . $login; } } } $userRest = new UserRest(); \URIDispatcher::getInstance()->map("GET", "/users/\$1", array($userRest, "get"))->map("POST", "/users/login/\$1", array($userRest, "login"))->map("POST", "/users", array($userRest, "create"))->map("PUT", "/users/\$1/\$2", array($userRest, "update"))->map("DELETE", "/users/\$1", array($userRest, "delete"));
<?php // Simple REST router require_once dirname(__FILE__) . "/URIDispatcher.php"; // dinamically include Rest files (*Rest.php) in this directory $files_in_script_dir = scandir(__DIR__); foreach ($files_in_script_dir as $filename) { // if filename ends with *Rest.php if (preg_match('/.*REST\\.PHP/', strtoupper($filename))) { include_once __DIR__ . "/" . $filename; } } try { // error_reporting(E_ERROR); $dispatched = URIDispatcher::getInstance()->dispatchRequest(); if (!$dispatched) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad request'); die("no dispatcher found for this request"); } } catch (Exception $ex) { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal server error'); die($ex->getMessage()); } // debug //print_r($_SERVER); //print_r($_GET);
} $this->postMapper->delete($post); header($_SERVER['SERVER_PROTOCOL'] . ' 200 Ok'); } public function createComment($postId, $data) { $currentUser = parent::authenticateUser(); $post = $this->postMapper->findById($postId); if ($post == NULL) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad request'); echo "Post with id " . $postId . " not found"; } $comment = new Comment(); $comment->setContent($data->content); $comment->setAuthor($currentUser); $comment->setPost($post); try { $comment->checkIsValidForCreate(); // if it fails, ValidationException $this->commentMapper->save($comment); header($_SERVER['SERVER_PROTOCOL'] . ' 201 Created'); } catch (ValidationException $e) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad request'); echo json_encode($e->getErrors()); } } } // URI-MAPPING for this Rest endpoint $postRest = new PostRest(); URIDispatcher::getInstance()->map("GET", "/post", array($postRest, "getPosts"))->map("GET", "/post/\$1", array($postRest, "readPost"))->map("POST", "/post", array($postRest, "createPost"))->map("POST", "/post/\$1/comment", array($postRest, "createComment"))->map("PUT", "/post/\$1", array($postRest, "updatePost"))->map("DELETE", "/post/\$1", array($postRest, "deletePost"));
$this->userMapper = new UserMapper(); } public function postUser($data) { $user = new User($data->username, $data->password); try { $user->checkIsValidForRegister(); $this->userMapper->save($user); header($_SERVER['SERVER_PROTOCOL'] . ' 201 Created'); header("Location: " . $_SERVER['REQUEST_URI'] . "/" . $data->username); } catch (ValidationException $e) { http_response_code(400); echo json_encode($e->getErrors()); } } public function login($username) { $currentLogged = parent::authenticateUser(); if ($currentLogged->getUsername() != $username) { header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); echo "You are not authorized to login as anyone but you"; } else { header($_SERVER['SERVER_PROTOCOL'] . ' 200 Ok'); echo "Hello " . $username; } } } // URI-MAPPING for this Rest endpoint $userRest = new UserRest(); URIDispatcher::getInstance()->map("GET", "/user/\$1", array($userRest, "login"))->map("POST", "/user", array($userRest, "postUser"));
* cliente en función del intervalo de fechas proporcionado por el usuario. * * @author acfernandez4 <*****@*****.**> */ class PercentSpendingRest extends BaseRest { private $typeDAO; public function __construct() { parent::__construct(); $this->typeDAO = new TypeDAO(); } public function getPercents($owner) { $currentUser = parent::authenticateUser(); $startDate = $this->request->getStartDate(); $endDate = $this->request->getEndDate(); $types = $this->typeDAO->findByOwnerAndFilterWithPercents($owner, $startDate, $endDate); if ($types == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "The defined interval time not contains percents"; return; } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($types); } } $percentSpendingRest = new PercentSpendingRest(); URIDispatcher::getInstance()->map("GET", "/percents/\$1", array($percentSpendingRest, "getPercents"));
$quantityRevenues += $revenue->getQuantity(); } } else { if ($revenue->getDate() <= $topMonth->format("Y-m-d")) { $quantityRevenues += $revenue->getQuantity(); } } } if ($stockRef != NULL) { $total = $stockRef->getTotal() + $quantityRevenues - $quantitySpendings; } else { $total = $quantityRevenues - $quantitySpendings; } $stockChart = new Stock(); $stockChart->setTotal($total); $stockChart->setDate($dt->format("Y-m-d")); array_push($stocksChart, $stockChart); $quantitySpendings = 0; $quantityRevenues = 0; } foreach ($stocksChart as $stock) { array_push($stocks_array, ["date" => $stock->getDate(), "total" => $stock->getTotal()]); } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($stocks_array); } } $positionsRest = new PositionRest(); URIDispatcher::getInstance()->map("GET", "/positions/\$1", array($positionsRest, "getPositions"));