From 417cf9dc86d9205876755f5a380094d8ac4ef5d9 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 14 May 2016 08:59:19 +0200 Subject: [PATCH] init --- Article.php | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ ArticleDao.php | 3 ++ User.php | 72 ++++++++++++++++++++++++++++ UserDao.php | 0 Vote.php | 66 ++++++++++++++++++++++++++ VoteDao.php | 74 +++++++++++++++++++++++++++++ 6 files changed, 341 insertions(+) create mode 100644 Article.php create mode 100644 ArticleDao.php create mode 100644 User.php create mode 100644 UserDao.php create mode 100644 Vote.php create mode 100644 VoteDao.php diff --git a/Article.php b/Article.php new file mode 100644 index 0000000..add4452 --- /dev/null +++ b/Article.php @@ -0,0 +1,126 @@ + + */ +class Article +{ + /** + * @var integer + */ + protected $id; + + /** + * @var integer + */ + protected $title; + + /** + * @var integer + */ + protected $content; + + /** + * @var array $votes; + */ + protected $votes = []; + + + /** + * @param int $id + * @return Article + */ + public function setId($id) + { + $this->id = (int) $id; + + return $this; + } + + /** + * @return int $id + */ + public function getId() + { + return $this->id; + } + + /** + * @param string $title + * @return Article + */ + public function setTitle($title) + { + $this->title = (string) $title; + + return $this; + } + + /** + * @return string $title + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $content + * @return Article + */ + public function setContent($content) + { + $this->content = (string) $content; + + return $this; + } + + /** + * @return string $content + */ + public function getContent() + { + return $this->content; + } + + + /** + * @param Vote $vote + * @return Article + */ + public function addVote(Vote $vote) + { + $this->votes[] = $vote; + + return $this; + } + + /** + * @param array $votes + * @return Article + */ + public function setVotes(array $votes) + { + foreach ($votes as $vote) { + $vote->setArticle($this); + } + + $this->votes = $votes; + + return $this; + } + + /** + * @return array $votes + */ + public function getVotes() + { + if (empty($this->votes)) { + $this->votes = VoteDao::create()->findByArticle($this); + } + + return $this->votes; + } +} + diff --git a/ArticleDao.php b/ArticleDao.php new file mode 100644 index 0000000..50cce95 --- /dev/null +++ b/ArticleDao.php @@ -0,0 +1,3 @@ + + */ +class User +{ + /** + * @var integer + */ + protected $id; + + /** + * @var array $votes; + */ + protected $votes = []; + + /** + * @param int $id + * @return User + */ + public function setId($id) + { + $this->id = (int) $id; + + return $this; + } + + /** + * @return int $id + */ + public function getId() + { + return $this->id; + } + + /** + * @param Vote $vote + * @return Article + */ + public function addVote(Vote $vote) + { + $this->votes[] = $vote; + + return $this; + } + + /** + * @param array $votes + * @return Article + */ + public function setVotes(array $votes) + { + foreach ($votes as $vote) { + $vote->setUser($this); + } + + $this->votes = $votes; + + return $this; + } + + /** + * @return array $votes + */ + public function getVotes() + { + return $this->votes; + } +} + diff --git a/UserDao.php b/UserDao.php new file mode 100644 index 0000000..e69de29 diff --git a/Vote.php b/Vote.php new file mode 100644 index 0000000..9d9cd89 --- /dev/null +++ b/Vote.php @@ -0,0 +1,66 @@ + + */ +class Vote +{ + /** + * @var Article + */ + protected $article; + + /** + * @var User + */ + protected $user; + + + /** + * @param Article $article + * @return + */ + public function setArticle($article) + { + $this->article = $article; + + if ($article) { + $this->article->addVote($this); + } + + return $this; + } + + /** + * @return Article $article + */ + public function getArticle() + { + return $this->article; + } + + /** + * @param User $user + * @return + */ + public function setUser($user) + { + $this->user = $user; + + if ($user) { + $this->user->addVote($this); + } + + return $this; + } + + /** + * @return User $user + */ + public function getUser() + { + return $this->user; + } +} + diff --git a/VoteDao.php b/VoteDao.php new file mode 100644 index 0000000..496bbdd --- /dev/null +++ b/VoteDao.php @@ -0,0 +1,74 @@ + + */ +class VoteDao +{ + protected $pdo; + + public function __construct() + { + $this->pdo = new Pdo('mysql:dbname=test;host=127.0.0.1', 'root', 'root'); + } + + public static function create() + { + return new static(); + } + + public function findByArticle(Article $article) + { + $query = $this->pdo->prepare('select * from vote where article_id=:article_id'); + + $query->execute([ + ':article_id' => $article->getId(), + ]); + + $results = $query->fetchAll(); + $datas = []; + + foreach ($results as $result) { + $datas[] = $this->hydrate($result); + } + + return $datas; + } + + public function findById(Article $article) + { + $query = $this->pdo->prepare('select * from vote where article_id=:article_id'); + + $query->execute([ + ':article_id' => $article->getId(), + ]); + + $results = $query->fetch(); + + if (!empty($results)) { + return $this->hydrate($result); + + } + } + + public function hydrate(array $datas) + { + $vote = new Vote(); +zR + if (isset($datas['id'])) { + $vote->setId($datas['id']); + } + + if (isset($datas['article_id'])) { + $vote->setArticle(ArticleDao::create()->findById($datas['article_id'])); + } + + if (isset($datas['user_id'])) { + $vote->setUser(UserDao::create()->findById($datas['article_id'])); + } + + return $vote; + } +} +