* initial commit
This commit is contained in:
180
src/App/Entity/Article.php
Normal file
180
src/App/Entity/Article.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="article")
|
||||
*/
|
||||
class Article implements JsonSerializable
|
||||
{
|
||||
use Traits\GetterSetter;
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\GeneratedValue(strategy="IDENTITY")
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="title", type="string", length=255)
|
||||
* @var string
|
||||
*/
|
||||
private $title = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="content", type="string", length=65535)
|
||||
* @var string
|
||||
*/
|
||||
private $content = null;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="articles")
|
||||
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
|
||||
* @var User
|
||||
*/
|
||||
private $author = null;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity="Comment", mappedBy="author")
|
||||
* @ORM\JoinColumn(name="id", referencedColumnName="author_id", nullable=false)
|
||||
* @var Comment[]
|
||||
*/
|
||||
private $comments;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="visible", type="boolean")
|
||||
* @var bool
|
||||
*/
|
||||
private $visible = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->comments = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User
|
||||
*/
|
||||
public function getAuthor()
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function getComments()
|
||||
{
|
||||
return $this->comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getVisible()
|
||||
{
|
||||
return $this->visible;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Article
|
||||
*/
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Article
|
||||
*/
|
||||
public function setTitle(string $title)
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return Article
|
||||
*/
|
||||
public function setContent(string $content)
|
||||
{
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $author
|
||||
* @return Article
|
||||
*/
|
||||
public function setAuthor(User $author)
|
||||
{
|
||||
$this->author = $author;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Comment[] $comments
|
||||
* @return Article
|
||||
*/
|
||||
public function setComments(array $comments)
|
||||
{
|
||||
$this->comments = $comments;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $visible
|
||||
* @return Article
|
||||
*/
|
||||
public function setVisible(bool $visible)
|
||||
{
|
||||
$this->visible = $visible;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'author' => $this->author,
|
||||
'comments' => $this->comments,
|
||||
'visible' => $this->visible,
|
||||
];
|
||||
}
|
||||
}
|
||||
178
src/App/Entity/Comment.php
Normal file
178
src/App/Entity/Comment.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use DateTime;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="comment")
|
||||
*/
|
||||
class Comment implements JsonSerializable
|
||||
{
|
||||
use Traits\GetterSetter;
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\GeneratedValue(strategy="IDENTITY")
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="content", type="string", length=65535)
|
||||
* @var string
|
||||
*/
|
||||
private $content = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="created_at", type="datetime")
|
||||
* @var DateTime
|
||||
*/
|
||||
private $createdAt = null;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
|
||||
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
|
||||
* @var Article
|
||||
*/
|
||||
private $article = null;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="comments")
|
||||
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
|
||||
* @var User
|
||||
*/
|
||||
private $author = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="visible", type="boolean")
|
||||
* @var bool
|
||||
*/
|
||||
private $visible = true;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getCreatedAt()
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Article
|
||||
*/
|
||||
public function getArticle()
|
||||
{
|
||||
return $this->article;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User
|
||||
*/
|
||||
public function getAuthor()
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getVisible()
|
||||
{
|
||||
return $this->visible;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Comment
|
||||
*/
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return Comment
|
||||
*/
|
||||
public function setContent(string $content)
|
||||
{
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime $createdAt
|
||||
* @return Comment
|
||||
*/
|
||||
public function setCreatedAt(DateTime $createdAt)
|
||||
{
|
||||
$this->createdAt = clone $createdAt;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Article $article
|
||||
* @return Comment
|
||||
*/
|
||||
public function setArticle(Article $article)
|
||||
{
|
||||
$this->article = $article;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $author
|
||||
* @return Comment
|
||||
*/
|
||||
public function setAuthor(User $author)
|
||||
{
|
||||
$this->author = $author;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $visible
|
||||
* @return Comment
|
||||
*/
|
||||
public function setVisible(bool $visible)
|
||||
{
|
||||
$this->visible = $visible;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'content' => $this->content,
|
||||
'createdAt' => $this->createdAt,
|
||||
'author' => $this->author,
|
||||
'article' => $this->article,
|
||||
'visible' => $this->visible,
|
||||
];
|
||||
}
|
||||
}
|
||||
52
src/App/Entity/Traits/GetterSetter.php
Normal file
52
src/App/Entity/Traits/GetterSetter.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity\Traits;
|
||||
|
||||
Trait GetterSetter
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns the getter name for a field
|
||||
*
|
||||
* @param string $field
|
||||
* @return string
|
||||
*/
|
||||
protected function getterName($field)
|
||||
{
|
||||
return sprintf('get%s', ucfirst(
|
||||
str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setter name for a field
|
||||
*
|
||||
* @param string $field
|
||||
* @return string
|
||||
*/
|
||||
protected function setterName($field)
|
||||
{
|
||||
return sprintf('set%s', ucfirst(
|
||||
str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate entity with the given data.
|
||||
* The set* method will be used to set the data.
|
||||
*
|
||||
* @param array $data
|
||||
* @return boolean
|
||||
*/
|
||||
public function populate(array $data = [])
|
||||
{
|
||||
foreach ($data as $field => $value) {
|
||||
$setter = $this->setterName($field);
|
||||
if (method_exists($this, $setter)) {
|
||||
$this->{$setter}($value);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
184
src/App/Entity/User.php
Normal file
184
src/App/Entity/User.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="user")
|
||||
*/
|
||||
class User implements JsonSerializable
|
||||
{
|
||||
use Traits\GetterSetter;
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\GeneratedValue(strategy="IDENTITY")
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="name", type="string", length=150)
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="email", type="string", length=255)
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity="Article", mappedBy="author")
|
||||
* @ORM\JoinColumn(name="id", referencedColumnName="author_id", nullable=false)
|
||||
* @var Article[]
|
||||
*/
|
||||
private $articles;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity="Comment", mappedBy="author")
|
||||
* @ORM\JoinColumn(name="id", referencedColumnName="author_id", nullable=false)
|
||||
* @var Comment[]
|
||||
*/
|
||||
private $comments;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="active", type="boolean")
|
||||
* @var bool
|
||||
*/
|
||||
private $active = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->articles = new ArrayCollection();
|
||||
$this->comments = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Article[]
|
||||
*/
|
||||
public function getArticles()
|
||||
{
|
||||
return $this->articles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function getComments()
|
||||
{
|
||||
return $this->comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getActive()
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return User
|
||||
*/
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return User
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return User
|
||||
*/
|
||||
public function setEmail(string $email)
|
||||
{
|
||||
$this->email = $email;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Article[] $articles
|
||||
* @return User
|
||||
*/
|
||||
public function setArticles(array $articles)
|
||||
{
|
||||
$this->articles = $articles;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Comment[] $comments
|
||||
* @return User
|
||||
*/
|
||||
public function setComments(array $comments)
|
||||
{
|
||||
$this->comments = $comments;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $active
|
||||
* @return User
|
||||
*/
|
||||
public function setActive(bool $active)
|
||||
{
|
||||
$this->active = $active;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'articles' => $this->articles,
|
||||
'comments' => $this->comments,
|
||||
'active' => $this->active,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user