Exemplo n.º 1
0
 /**
  * Creates an Article object based on a DB row.
  *
  * @param array $row The DB row containing Article data.
  * @return \MicroCMS\Domain\Article
  */
 private function buildArticle(array $row)
 {
     $article = new Article();
     $article->setId($row['art_id']);
     $article->setTitle($row['art_title']);
     $article->setContent($row['art_content']);
     return $article;
 }
Exemplo n.º 2
0
 /**
  * Creates an Article object based on a DB row.
  *
  * @param array $row The DB row containing Article data.
  * @return \MicroCMS\Domain\Article
  */
 protected function buildDomainObject($row)
 {
     $article = new Article();
     $article->setId($row['art_id']);
     $article->setTitle($row['art_title']);
     $article->setContent($row['art_content']);
     return $article;
 }
Exemplo n.º 3
0
 public function upload()
 {
     // Si jamais il n'y a pas de fichier (champ facultatif)
     if (null === $this->file) {
         return;
     }
     // Si on avait un ancien fichier, on le supprime
     if (null !== $this->tempFilename) {
         $oldFile = $this->getUploadRootDir() . '/' . $this->id . '.' . $this->tempFilename;
         if (file_exists($oldFile)) {
             unlink($oldFile);
         }
     }
     // On déplace le fichier envoyé dans le répertoire de notre choix
     $this->file->move($this->getUploadRootDir(), $this->id . '.' . $this->url);
 }
Exemplo n.º 4
0
 /**
  * Saves an article into the database.
  *
  * @param \MicroCMS\Domain\Article $article The article to save
  */
 public function save(Article $article)
 {
     $articleData = array('art_title' => $article->getTitle(), 'art_content' => $article->getContent());
     if ($article->getId()) {
         // The article has already been saved : update it
         $this->getDb()->update('t_article', $articleData, array('art_id' => $article->getId()));
     } else {
         // The article has never been saved : insert it
         $this->getDb()->insert('t_article', $articleData);
         // Get the id of the newly created article and set it on the entity.
         $id = $this->getDb()->lastInsertId();
         $article->setId($id);
     }
 }
Exemplo n.º 5
0
 /**
  * Converts an Article object into an associative array for JSON encoding
  *
  * @param Article $article Article object
  *
  * @return array Associative array whose fields are the article properties.
  */
 private function buildArticleArray(Article $article)
 {
     $data = array('id' => $article->getId(), 'title' => $article->getTitle(), 'content' => $article->getContent());
     return $data;
 }
Exemplo n.º 6
0
    // Convert an object ($article) into an associative array ($responseData)
    $responseData = array('id' => $article->getId(), 'title' => $article->getTitle(), 'content' => $article->getContent());
    // Create and return a JSON response
    return $app->json($responseData);
})->bind('api_article');
// API : create a new article
$app->post('/api/article', function (Request $request) use($app) {
    // Check request parameters
    if (!$request->request->has('title')) {
        return $app->json('Missing required parameter: title', 400);
    }
    if (!$request->request->has('content')) {
        return $app->json('Missing required parameter: content', 400);
    }
    // Build and save the new article
    $article = new Article();
    $article->setTitle($request->request->get('title'));
    $article->setContent($request->request->get('content'));
    $app['dao.article']->save($article);
    // Convert an object ($article) into an associative array ($responseData)
    $responseData = array('id' => $article->getId(), 'title' => $article->getTitle(), 'content' => $article->getContent());
    return $app->json($responseData, 201);
    // 201 = Created
})->bind('api_article_add');
// API : delete an existing article
$app->delete('/api/article/{id}', function ($id, Request $request) use($app) {
    // Delete all associated comments
    $app['dao.comment']->deleteAllByArticle($id);
    // Delete the article
    $app['dao.article']->delete($id);
    return $app->json('No Content', 204);