Exemplo n.º 1
0
 public function instantiate($dao)
 {
     // Instantiate a Slim application:
     $app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
     // Routes
     $app->get('/api/posts/{id}', function (ServerRequestInterface $request, ResponseInterface $response, $args) use($dao) {
         $params = $request->getQueryParams();
         $id = $args['id'];
         $post = $dao->getPost($id);
         if (!empty($post[0])) {
             $postResponse = ["post" => $post[0]];
         } else {
             // send an empty json list {} and not []
             $postResponse = ["post" => new ArrayObject()];
         }
         $response->getBody()->write(json_encode($postResponse, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE));
         $response = $response->withHeader('Content-type', 'application/json');
         return $response;
     });
     $app->get('/api/posts', function (ServerRequestInterface $request, ResponseInterface $response) use($dao) {
         $params = $request->getQueryParams();
         if (isset($params['author'])) {
             $author = $params['author'];
         } else {
             $author = null;
         }
         // TODO : move in settings
         $formatDate = 'Y-m-d';
         // Date checking
         if (isset($params['from']) || isset($params['to'])) {
             if (!DateHelper::validateDate($formatDate, $params['from']) || !DateHelper::validateDate($formatDate, $params['to'])) {
                 throw new Exception("'from' date parameter or 'to' date parameter is not correct or missing.");
             } else {
                 // pipe concatenation to set h-m-s at 00:00:00
                 $fromDate = DateTime::createFromFormat($formatDate . '|', $params['from']);
                 $toDate = DateTime::createFromFormat($formatDate . '|', $params['to']);
             }
         } else {
             $fromDate = null;
             $toDate = null;
         }
         $post = $dao->getPosts($author, $fromDate, $toDate);
         $postResponse = ["posts" => $post, "count" => count($post)];
         $response->getBody()->write(json_encode($postResponse, JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE));
         $response = $response->withHeader('Content-type', 'application/json');
         return $response;
     });
     return $app;
 }
Exemplo n.º 2
0
 public function testValidateGoodDate()
 {
     $format = 'Y-m-d';
     $this->assertTrue(DateHelper::validateDate($format, '2016-02-28'));
 }