Compare commits

..

2 Commits

Author SHA1 Message Date
Danyi Dávid
7753739d8b * PersonService switched to exception handled error methods
* exception messages are now returnes when an error happens
* Person entity is fixed to have nullable fields
2016-08-16 20:49:56 +02:00
Danyi Dávid
bcf87d3cdc * removed test items, and added "person" 2016-08-15 23:30:13 +02:00
45 changed files with 669 additions and 1524 deletions

View File

@ -21,7 +21,7 @@ return [
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class,
'doctrine.hydrator' => \App\Hydrator\DoctrineObjectFactory::class,
\App\Service\Article\ArticleService::class => \App\Service\Article\ArticleServiceFactory::class,
\App\Service\Person\PersonService::class => \App\Service\Person\PersonServiceFactory::class,
],
],
];

View File

@ -8,11 +8,11 @@ return [
],
'factories' => [
App\Action\HomePageAction::class => App\Action\HomePageFactory::class,
App\Action\Article\ListAction::class => App\Action\Article\ListFactory::class,
App\Action\Article\GetAction::class => App\Action\Article\GetFactory::class,
App\Action\Article\PostAction::class => App\Action\Article\PostFactory::class,
App\Action\Article\PutAction::class => App\Action\Article\PutFactory::class,
App\Action\Article\DeleteAction::class => App\Action\Article\DeleteFactory::class,
App\Action\Person\ListAction::class => App\Action\Person\ListFactory::class,
App\Action\Person\GetAction::class => App\Action\Person\GetFactory::class,
App\Action\Person\PostAction::class => App\Action\Person\PostFactory::class,
App\Action\Person\PutAction::class => App\Action\Person\PutFactory::class,
App\Action\Person\DeleteAction::class => App\Action\Person\DeleteFactory::class,
],
],
'routes' => [
@ -23,33 +23,33 @@ return [
'allowed_methods' => ['GET'],
],
[
'name' => 'api.article.list',
'path' => '/api/article',
'middleware' => App\Action\Article\ListAction::class,
'name' => 'api.person.list',
'path' => '/api/person',
'middleware' => App\Action\Person\ListAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'api.article.get',
'path' => '/api/article/{id:\d+}',
'middleware' => App\Action\Article\GetAction::class,
'name' => 'api.person.get',
'path' => '/api/person/{id:\d+}',
'middleware' => App\Action\Person\GetAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'api.article.add',
'path' => '/api/article',
'middleware' => App\Action\Article\PostAction::class,
'name' => 'api.person.add',
'path' => '/api/person',
'middleware' => App\Action\Person\PostAction::class,
'allowed_methods' => ['POST'],
],
[
'name' => 'api.article.update',
'path' => '/api/article/{id:\d+}',
'middleware' => App\Action\Article\PutAction::class,
'name' => 'api.person.update',
'path' => '/api/person/{id:\d+}',
'middleware' => App\Action\Person\PutAction::class,
'allowed_methods' => ['PUT'],
],
[
'name' => 'api.article.delete',
'path' => '/api/article/{id:\d+}',
'middleware' => App\Action\Article\DeleteAction::class,
'name' => 'api.person.delete',
'path' => '/api/person/{id:\d+}',
'middleware' => App\Action\Person\DeleteAction::class,
'allowed_methods' => ['DELETE'],
],
[

View File

@ -1,37 +0,0 @@
<?php
namespace App\Action\Article;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class DeleteAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$result = $this->entityService->delete($id);
$return = new JsonResponse($result);
if (false === $result) {
return $return->withStatus(500, 'Failed to delete record.');
}
return $return;
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\Article\DeleteAction;
use App\Service\Article\ArticleService;
use Interop\Container\ContainerInterface;
class DeleteFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(ArticleService::class);
return new DeleteAction($entityService);
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Action\Article;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class GetAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$entity = $this->entityService->get($id);
if (null === $entity) {
$ret = new JsonResponse(false);
return $ret->withStatus(404);
}
return new JsonResponse($entity);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\Article\GetAction;
use App\Service\Article\ArticleService;
use Interop\Container\ContainerInterface;
class GetFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(ArticleService::class);
return new GetAction($entityService);
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace App\Action\Article;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class ListAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new JsonResponse($this->entityService->getList());
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\Article\ListAction;
use App\Service\Article\ArticleService;
use Interop\Container\ContainerInterface;
class ListFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(ArticleService::class);
return new ListAction($entityService);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class PostAction extends AbstractFormAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$data = $this->getRequestData($request);
$result = $this->entityService->create($data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to insert record.');
}
return new JsonResponse($result);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\Article\PostAction;
use App\Service\Article\ArticleService;
use Interop\Container\ContainerInterface;
class PostFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(ArticleService::class);
return new PostAction($entityService);
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class PutAction extends AbstractFormAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id', false);
$data = $this->getRequestData($request);
$result = $this->entityService->modify($id, $data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to update record.');
}
return new JsonResponse($result);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Article;
use App\Action\Article\PutAction;
use App\Service\Article\ArticleService;
use Interop\Container\ContainerInterface;
class PutFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(ArticleService::class);
return new PutAction($entityService);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class DeleteAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$result = $this->entityService->delete($id);
$return = new JsonResponse($result);
if (false === $result) {
return $return->withStatus(500, 'Failed to delete record.');
}
return $return;
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\Article\DeleteAction;
use App\Service\Article\CommentService;
use Interop\Container\ContainerInterface;
class DeleteFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(CommentService::class);
return new DeleteAction($entityService);
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class GetAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$entity = $this->entityService->get($id);
if (null === $entity) {
$ret = new JsonResponse(false);
return $ret->withStatus(404);
}
return new JsonResponse($entity);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\Article\GetAction;
use App\Service\Article\CommentService;
use Interop\Container\ContainerInterface;
class GetFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(CommentService::class);
return new GetAction($entityService);
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class ListAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new JsonResponse($this->entityService->getList());
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\Article\ListAction;
use App\Service\Article\CommentService;
use Interop\Container\ContainerInterface;
class ListFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(CommentService::class);
return new ListAction($entityService);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\Article\PostAction;
use App\Service\Article\CommentService;
use Interop\Container\ContainerInterface;
class PostFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(CommentService::class);
return new PostAction($entityService);
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class PutAction extends AbstractFormAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id', false);
$data = $this->getRequestData($request);
$result = $this->entityService->modify($id, $data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to update record.');
}
return new JsonResponse($result);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Action\Comment;
use App\Action\Article\PutAction;
use App\Service\Article\CommentService;
use Interop\Container\ContainerInterface;
class PutFactory
{
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(CommentService::class);
return new PutAction($entityService);
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Action\Person;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\ORMInvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class DeleteAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
try {
$this->entityService->delete($id);
} catch (EntityNotFoundException $exception) {
$ret = new JsonResponse([
'message' => $exception->getMessage(),
]);
return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id));
} catch (ORMInvalidArgumentException $exception) {
$res = new JsonResponse([
'message' => $exception->getMessage(),
]);
return $res->withStatus(500, 'Failed to delete record.');
}
return new JsonResponse(true);
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\Article\DeleteAction;
use App\Service\Article\UserService;
use App\Action\Person\DeleteAction;
use App\Service\Person\PersonService;
use Interop\Container\ContainerInterface;
class DeleteFactory
@ -11,7 +11,7 @@ class DeleteFactory
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(UserService::class);
$entityService = $container->get(PersonService::class);
return new DeleteAction($entityService);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Action\Person;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class GetAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
try {
$entity = $this->entityService->get($id);
} catch (NoResultException $ex) {
$ret = new JsonResponse([
'message' => $ex->getMessage(),
]);
return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id));
} catch (NonUniqueResultException $ex) {
$ret = new JsonResponse([
'message' => $ex->getMessage(),
]);
return $ret->withStatus(500, 'Duplicate key in database.');
}
return new JsonResponse($entity);
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\Article\GetAction;
use App\Service\Article\UserService;
use App\Action\Person\GetAction;
use App\Service\Person\PersonService;
use Interop\Container\ContainerInterface;
class GetFactory
@ -11,7 +11,7 @@ class GetFactory
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(UserService::class);
$entityService = $container->get(PersonService::class);
return new GetAction($entityService);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;

View File

@ -1,9 +1,9 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\Article\ListAction;
use App\Service\Article\UserService;
use App\Action\Person\ListAction;
use App\Service\Person\PersonService;
use Interop\Container\ContainerInterface;
class ListFactory
@ -11,7 +11,7 @@ class ListFactory
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(UserService::class);
$entityService = $container->get(PersonService::class);
return new ListAction($entityService);
}
}

View File

@ -1,9 +1,10 @@
<?php
namespace App\Action\Comment;
namespace App\Action\Person;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
@ -25,11 +26,13 @@ class PostAction extends AbstractFormAction
{
$data = $this->getRequestData($request);
$result = $this->entityService->create($data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to insert record.');
try {
$result = $this->entityService->create($data);
} catch (NotNullConstraintViolationException $exception) {
$res = new JsonResponse([
'message' => $exception->getMessage(),
]);
return $res->withStatus(500, 'Failed to insert record. A required field was not set.');
}
return new JsonResponse($result);

View File

@ -1,9 +1,9 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\Article\PostAction;
use App\Service\Article\UserService;
use App\Action\Person\PostAction;
use App\Service\Person\PersonService;
use Interop\Container\ContainerInterface;
class PostFactory
@ -11,7 +11,7 @@ class PostFactory
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(UserService::class);
$entityService = $container->get(PersonService::class);
return new PostAction($entityService);
}
}

View File

@ -1,9 +1,10 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\EntityNotFoundException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
@ -26,11 +27,13 @@ class PutAction extends AbstractFormAction
$id = $request->getAttribute('id', false);
$data = $this->getRequestData($request);
$result = $this->entityService->modify($id, $data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to update record.');
try {
$result = $this->entityService->modify($id, $data);
} catch (EntityNotFoundException $exception) {
$res = new JsonResponse([
'message' => $exception->getMessage(),
]);
return $res->withStatus(404, sprintf('Failed to update record with id `%s`', $id));
}
return new JsonResponse($result);

View File

@ -1,9 +1,9 @@
<?php
namespace App\Action\User;
namespace App\Action\Person;
use App\Action\Article\PutAction;
use App\Service\Article\UserService;
use App\Action\Person\PutAction;
use App\Service\Person\PersonService;
use Interop\Container\ContainerInterface;
class PutFactory
@ -11,7 +11,7 @@ class PutFactory
public function __invoke(ContainerInterface $container)
{
$entityService = $container->get(UserService::class);
$entityService = $container->get(PersonService::class);
return new PutAction($entityService);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace App\Action\User;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class DeleteAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$result = $this->entityService->delete($id);
$return = new JsonResponse($result);
if (false === $result) {
return $return->withStatus(500, 'Failed to delete record.');
}
return $return;
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Action\User;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class GetAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$id = $request->getAttribute('id');
$entity = $this->entityService->get($id);
if (null === $entity) {
$ret = new JsonResponse(false);
return $ret->withStatus(404);
}
return new JsonResponse($entity);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace App\Action\User;
use App\Action\AbstractFormAction;
use App\Service\EntityServiceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class PostAction extends AbstractFormAction
{
/**
* @var EntityServiceInterface
*/
private $entityService;
public function __construct(EntityServiceInterface $entityService)
{
$this->entityService = $entityService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$data = $this->getRequestData($request);
$result = $this->entityService->create($data);
if (false === $result) {
$result = new JsonResponse(false);
return $result->withStatus(500, 'Failed to insert record.');
}
return new JsonResponse($result);
}
}

View File

@ -1,182 +0,0 @@
<?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="article")
* @ORM\JoinColumn(name="id", referencedColumnName="article_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,
];
}
}

View File

@ -1,180 +0,0 @@
<?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,
];
}
}

387
src/App/Entity/Person.php Normal file
View File

@ -0,0 +1,387 @@
<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
/**
* @ORM\Entity
* @ORM\Table(name="person")
*/
class Person implements JsonSerializable
{
use Traits\GetterSetter;
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
* @var int
*/
private $id;
/**
* @ORM\Column(name="person_id", type="string", length=7, nullable=false)
* @var string
*/
private $personId;
/**
* @ORM\Column(name="first_name", type="string", length=150, nullable=false)
* @var string
*/
private $firstName;
/**
* @ORM\Column(name="middle_name", type="string", length=150, nullable=true)
* @var string
*/
private $middleName;
/**
* @ORM\Column(name="last_name", type="string", length=150, nullable=false)
* @var string
*/
private $lastName;
/**
* @ORM\Column(name="gender", type="smallint", nullable=false)
* @var string
*/
private $gender;
/**
* @ORM\Column(name="date_of_birth", type="date", nullable=true)
* @var string
*/
private $dateOfBirth;
/**
* @ORM\Column(name="place_of_birth", type="string", length=150, nullable=true)
* @var string
*/
private $placeOfBirth;
/**
* @ORM\Column(name="primary_language", type="string", length=150, nullable=true)
* @var string
*/
private $primaryLanguage;
/**
* @ORM\Column(name="address", type="string", length=255, nullable=true)
* @var string
*/
private $address;
/**
* @ORM\ManyToOne(targetEntity="Person")
* @ORM\JoinColumn(name="mother_id", referencedColumnName="id", nullable=true)
*/
private $mother;
/**
* @ORM\ManyToOne(targetEntity="Person")
* @ORM\JoinColumn(name="father_id", referencedColumnName="id", nullable=true)
*/
private $father;
/**
* @ORM\Column(name="email", type="string", length=150, nullable=true)
* @var string
*/
private $email;
/**
* @ORM\Column(name="active", type="boolean")
* @var bool
*/
private $active = true;
/*
public function __construct()
{
$this->schools = new ArrayCollection();
$this->schools = new ArrayCollection();
}
*/
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getPersonId()
{
return $this->personId;
}
/**
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @return string
*/
public function getMiddleName()
{
return $this->middleName;
}
/**
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @return int
*/
public function getGender()
{
return $this->gender;
}
/**
* @return DateTime
*/
public function getDateOfBirth()
{
return $this->dateOfBirth;
}
/**
* @return string
*/
public function getPlaceOfBirth()
{
return $this->placeOfBirth;
}
/**
* @return string
*/
public function getPrimaryLanguage()
{
return $this->primaryLanguage;
}
/**
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* @return Person
*/
public function getMorher()
{
return $this->mother;
}
/**
* @return Person
*/
public function getFather()
{
return $this->father;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @return bool
*/
public function getActive()
{
return $this->active;
}
/**
* @param int $id
* @return Person
*/
public function setId(int $id)
{
$this->id = $id;
return $this;
}
/**
* @param string $personId
* @return Person
*/
public function setPersonId(string $personId)
{
$this->personId = $personId;
return $this;
}
/**
* @param string $firstName
* @return Person
*/
public function setFirstName(string $firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* @param string $middleName
* @return Person
*/
public function setMiddleName(string $middleName)
{
$this->middleName = $middleName;
return $this;
}
/**
* @param string $lastName
* @return Person
*/
public function setLastName(string $lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* @param int $gender
* @return Person
*/
public function setGender(int $gender)
{
$this->gender = $gender;
return $this;
}
/**
* @param datetime $dateOfBirth
* @return Person
*/
public function setDateOfBirth(DateTime $dateOfBirth)
{
$this->dateOfBirth = $dateOfBirth;
return $this;
}
/**
* @param string $placeOfBirth
* @return Person
*/
public function setPlaceOfBirth(string $placeOfBirth)
{
$this->placeOfBirth = $placeOfBirth;
return $this;
}
/**
* @param string $primaryLanguage
* @return Person
*/
public function setPrimaryLanguage(string $primaryLanguage)
{
$this->primaryLanguage = $primaryLanguage;
return $this;
}
/**
* @param string $address
* @return Person
*/
public function setAddress(string $address)
{
$this->address = $address;
return $this;
}
/**
* @param Person $mother
* @return Person
*/
public function setMother(Person $mother)
{
$this->mother = $mother;
return $this;
}
/**
* @param Person $father
* @return Person
*/
public function setFather(Person $father)
{
$this->father = $father;
return $this;
}
/**
* @param string $email
* @return Person
*/
public function setEmail(string $email)
{
$this->email = $email;
return $this;
}
/**
* @param bool $active
* @return Person
*/
public function setActive(bool $active)
{
$this->active = $active;
return $this;
}
/**
* @return array
*/
public function jsonSerialize()
{
return [
'id' => $this->id,
'personId' => $this->personId,
'firstName' => $this->firstName,
'middleName' => $this->middleName,
'lastName' => $this->lastName,
'gender' => $this->gender,
'dateOfBirth' => $this->dateOfBirth,
'placeOfBirth' => $this->placeOfBirth,
'primaryLanguage' => $this->primaryLanguage,
'address' => $this->address,
'mother' => $this->mother,
'father' => $this->father,
'email' => $this->email,
'active' => $this->active,
];
}
}

View File

@ -1,186 +0,0 @@
<?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,
];
}
}

View File

@ -1,99 +0,0 @@
<?php
namespace App\Service\Article;
use App\Entity\Article;
use App\Hydrator\DoctrineObject;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Exception;
class ArticleService implements EntityServiceInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @var DoctrineObject
*/
private $hydrator;
public function __construct(EntityManager $em, DoctrineObject $hydrator)
{
$this->em = $em;
$this->hydrator = $hydrator;
}
public function getList()
{
$qb = $this->em->createQueryBuilder();
$entities = $qb->select('a,u,c')
->from(Article::class, 'a')
->leftJoin('a.author', 'u')
->leftJoin('a.comments', 'c')
->getQuery()
->getArrayResult();
return $entities;
}
public function get($id)
{
$qb = $this->em->createQueryBuilder();
$entity = $qb->select('a,u,c')
->from(Article::class, 'a')
->leftJoin('a.author', 'u')
->leftJoin('a.comments', 'c')
->where('a.id = :aid')
->setParameter('aid', $id)
->getQuery()
->getOneOrNullResult(Query::HYDRATE_ARRAY);
return $entity;
}
public function create($data)
{
$entity = $this->hydrator->hydrate($data, new Article());
try {
$this->em->persist($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return $entity;
}
public function modify($id, $data)
{
if (null === ($entity = $this->em->find(Article::class, $id))) {
return false;
}
$entity = $this->hydrator->hydrate($data, $entity);
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
public function delete($id)
{
if (null === ($entity = $this->em->find(Article::class, $id))) {
return false;
}
try {
$this->em->remove($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return true;
}
}

View File

@ -1,16 +0,0 @@
<?php
namespace App\Service\Article;
use Interop\Container\ContainerInterface;
class ArticleServiceFactory extends \App\Service\AbstractEntityServiceFactory
{
public function __invoke(ContainerInterface $container)
{
$em = $this->getEntityManager($container);
$hydrator = $this->getDoctrineHydrator($container);
return new ArticleService($em, $hydrator);
}
}

View File

@ -1,99 +0,0 @@
<?php
namespace App\Service\Comment;
use App\Entity\Comment;
use App\Hydrator\DoctrineObject;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Exception;
class CommentService implements EntityServiceInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @var DoctrineObject
*/
private $hydrator;
public function __construct(EntityManager $em, DoctrineObject $hydrator)
{
$this->em = $em;
$this->hydrator = $hydrator;
}
public function getList()
{
$qb = $this->em->createQueryBuilder();
$entities = $qb->select('c,u,a')
->from(Comment::class, 'c')
->leftJoin('c.author', 'u')
->leftJoin('c.article', 'a')
->getQuery()
->getArrayResult();
return $entities;
}
public function get($id)
{
$qb = $this->em->createQueryBuilder();
$entity = $qb->select('c,u,a')
->from(Comment::class, 'c')
->leftJoin('c.author', 'u')
->leftJoin('c.article', 'a')
->where('c.id = :cid')
->setParameter('cid', $id)
->getQuery()
->getOneOrNullResult(Query::HYDRATE_ARRAY);
return $entity;
}
public function create($data)
{
$entity = $this->hydrator->hydrate($data, new Comment());
try {
$this->em->persist($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return $entity;
}
public function modify($id, $data)
{
if (null === ($entity = $this->em->find(Comment::class, $id))) {
return false;
}
$entity = $this->hydrator->hydrate($data, $entity);
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
public function delete($id)
{
if (null === ($entity = $this->em->find(Comment::class, $id))) {
return false;
}
try {
$this->em->remove($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return true;
}
}

View File

@ -1,16 +0,0 @@
<?php
namespace App\Service\Comment;
use Interop\Container\ContainerInterface;
class ArticleServiceFactory extends \App\Service\AbstractEntityServiceFactory
{
public function __invoke(ContainerInterface $container)
{
$em = $this->getEntityManager($container);
$hydrator = $this->getDoctrineHydrator($container);
return new CommentService($em, $hydrator);
}
}

View File

@ -0,0 +1,126 @@
<?php
namespace App\Service\Person;
use App\Entity\Person;
use App\Hydrator\DoctrineObject;
use App\Service\EntityServiceInterface;
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\ORMInvalidArgumentException;
use Doctrine\ORM\Query;
class PersonService implements EntityServiceInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @var DoctrineObject
*/
private $hydrator;
/**
* @param EntityManager $em
* @param DoctrineObject $hydrator
*/
public function __construct(EntityManager $em, DoctrineObject $hydrator)
{
$this->em = $em;
$this->hydrator = $hydrator;
}
/**
*
* @return mixed[]
*/
public function getList()
{
$qb = $this->em->createQueryBuilder();
$entities = $qb->select('p')
->from(Person::class, 'p')
->getQuery()
->getArrayResult();
return $entities;
}
/**
*
* @param int $id
* @return mixed
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function get($id)
{
$qb = $this->em->createQueryBuilder();
$entity = $qb->select('p')
->from(Person::class, 'p')
->where('p.id = :id')
->setParameter('id', $id)
->getQuery()
->getSingleResult(Query::HYDRATE_ARRAY);
return $entity;
}
/**
*
* @param type $data
* @return type
* @throws NotNullConstraintViolationException
*/
public function create($data)
{
$entity = $this->hydrator->hydrate($data, new Person());
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
/**
*
* @param int $id
* @param mixed $data
* @return Person
* @throws EntityNotFoundException
*/
public function modify($id, $data)
{
if (null === ($entity = $this->em->find(Person::class, $id))) {
throw new EntityNotFoundException('No result was found for query although at least one row was expected.');
}
$entity = $this->hydrator->hydrate($data, $entity);
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
/**
*
* @param int $id
* @return boolean
* @throws EntityNotFoundException
* @throws ORMInvalidArgumentException
*/
public function delete($id)
{
if (null === ($entity = $this->em->find(Person::class, $id))) {
throw new EntityNotFoundException('No result was found for query although at least one row was expected.');
}
$this->em->remove($entity);
$this->em->flush();
return true;
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace App\Service\User;
namespace App\Service\Person;
use Interop\Container\ContainerInterface;
class UserServiceFactory extends \App\Service\AbstractEntityServiceFactory
class PersonServiceFactory extends \App\Service\AbstractEntityServiceFactory
{
public function __invoke(ContainerInterface $container)
{
$em = $this->getEntityManager($container);
$hydrator = $this->getDoctrineHydrator($container);
return new userService($em, $hydrator);
return new PersonService($em, $hydrator);
}
}

View File

@ -1,99 +0,0 @@
<?php
namespace App\Service\User;
use App\Entity\User;
use App\Hydrator\DoctrineObject;
use App\Service\EntityServiceInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Exception;
class UserService implements EntityServiceInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @var DoctrineObject
*/
private $hydrator;
public function __construct(EntityManager $em, DoctrineObject $hydrator)
{
$this->em = $em;
$this->hydrator = $hydrator;
}
public function getList()
{
$qb = $this->em->createQueryBuilder();
$entities = $qb->select('u,a,c')
->from(User::class, 'u')
->leftJoin('u.articles', 'a')
->leftJoin('u.comments', 'c')
->getQuery()
->getArrayResult();
return $entities;
}
public function get($id)
{
$qb = $this->em->createQueryBuilder();
$entity = $qb->select('u,a,c')
->from(User::class, 'u')
->leftJoin('u.articles', 'a')
->leftJoin('u.comments', 'c')
->where('u.id = :uid')
->setParameter('uid', $id)
->getQuery()
->getOneOrNullResult(Query::HYDRATE_ARRAY);
return $entity;
}
public function create($data)
{
$entity = $this->hydrator->hydrate($data, new User());
try {
$this->em->persist($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return $entity;
}
public function modify($id, $data)
{
if (null === ($entity = $this->em->find(User::class, $id))) {
return false;
}
$entity = $this->hydrator->hydrate($data, $entity);
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
public function delete($id)
{
if (null === ($entity = $this->em->find(User::class, $id))) {
return false;
}
try {
$this->em->remove($entity);
$this->em->flush();
} catch (Exception $ex) {
return false;
}
return true;
}
}