|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Drest package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Lee Davis |
|
9
|
|
|
* @copyright Copyright (c) Lee Davis <@leedavis81> |
|
10
|
|
|
* @link https://github.com/leedavis81/drest/blob/master/LICENSE |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT X License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Drest\Service\Action; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\ORM; |
|
16
|
|
|
use DrestCommon\Response\Response; |
|
17
|
|
|
|
|
18
|
|
|
class GetElement extends AbstractAction |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
3 |
|
public function execute() |
|
22
|
|
|
{ |
|
23
|
3 |
|
$classMetaData = $this->getMatchedRoute()->getClassMetaData(); |
|
24
|
3 |
|
$elementName = $classMetaData->getEntityAlias(); |
|
25
|
|
|
|
|
26
|
3 |
|
$em = $this->getEntityManager(); |
|
27
|
3 |
|
$qb = $em->createQueryBuilder()->from($classMetaData->getClassName(), $elementName); |
|
28
|
3 |
|
$this->registerExposeFromMetaData($qb, $em->getClassMetadata($classMetaData->getClassName())); |
|
29
|
|
|
|
|
30
|
3 |
|
foreach ($this->getMatchedRoute()->getRouteParams() as $key => $value) { |
|
31
|
2 |
|
$qb->andWhere($elementName . '.' . $key . ' = :' . $key); |
|
32
|
2 |
|
$qb->setParameter($key, $value); |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
3 |
|
$resultArray = $qb->getQuery()->getSingleResult(ORM\Query::HYDRATE_ARRAY); |
|
37
|
1 |
|
if ($this->getMatchedRoute()->hasHandleCall()) |
|
38
|
1 |
|
{ |
|
39
|
|
|
$className = $this->getMatchedRoute()->getClassMetaData()->getClassName(); |
|
40
|
|
|
$handleMethod = $this->getMatchedRoute()->getHandleCall(); |
|
41
|
|
|
$resultArray = $className::$handleMethod($resultArray, $this->getRequest()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
$resultSet = $this->createResultSet($resultArray); |
|
45
|
3 |
|
} catch (\Exception $e) { |
|
46
|
2 |
|
return $this->handleError($e, Response::STATUS_CODE_404); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
return $resultSet; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|