init
This commit is contained in:
commit
417cf9dc86
126
Article.php
Normal file
126
Article.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class Article
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
3
ArticleDao.php
Normal file
3
ArticleDao.php
Normal file
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
|
72
User.php
Normal file
72
User.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class User
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
0
UserDao.php
Normal file
0
UserDao.php
Normal file
66
Vote.php
Normal file
66
Vote.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class Vote
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
74
VoteDao.php
Normal file
74
VoteDao.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class VoteDao
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Ladataan…
Viittaa uudesa ongelmassa