1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
2
|
|
|
|
3
|
|
|
use Doctrine\ORM\EntityManager; |
4
|
|
|
use Doctrine\ORM\EntityRepository; |
5
|
|
|
use Pz\Doctrine\Rest\Contracts\JsonApiResource; |
6
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
7
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
8
|
|
|
|
9
|
|
|
class RestRepository extends EntityRepository |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $alias; |
15
|
|
|
|
16
|
|
|
protected $resourceKey; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param EntityManager $em |
20
|
|
|
* @param string $class |
21
|
|
|
* |
22
|
|
|
* @return RestRepository |
23
|
|
|
*/ |
24
|
7 |
|
public static function create(EntityManager $em, $class) |
25
|
|
|
{ |
26
|
7 |
|
return new RestRepository($em, $em->getClassMetadata($class)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return \Doctrine\ORM\EntityManager |
31
|
|
|
*/ |
32
|
11 |
|
public function getEntityManager() |
33
|
|
|
{ |
34
|
11 |
|
return parent::getEntityManager(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
39
|
|
|
*/ |
40
|
11 |
|
public function sourceQueryBuilder() |
41
|
|
|
{ |
42
|
11 |
|
return $this->createQueryBuilder($this->alias()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string|null |
47
|
|
|
*/ |
48
|
14 |
|
public function getResourceKey() |
49
|
|
|
{ |
50
|
14 |
|
if (isset(class_implements($this->getClassName())[JsonApiResource::class])) { |
51
|
13 |
|
return call_user_func("{$this->getClassName()}::getResourceKey"); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Base root alias for queries. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
11 |
|
public function alias() |
63
|
|
|
{ |
64
|
11 |
|
if ($this->alias === null) { |
65
|
11 |
|
$this->alias = strtolower( |
66
|
11 |
|
preg_replace( |
67
|
11 |
|
'/(?<!^)[A-Z]/', '_$0', |
68
|
11 |
|
$this->getClassMetadata()->getReflectionClass()->getShortName() |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
return $this->alias; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $id |
78
|
|
|
* |
79
|
|
|
* @return JsonApiResource |
80
|
|
|
* @throws RestException |
81
|
|
|
*/ |
82
|
13 |
|
public function findById($id) |
83
|
|
|
{ |
84
|
13 |
|
if (null === ($entity = $this->find($id))) { |
85
|
1 |
|
throw RestException::createNotFound($id, $this->getResourceKey(), sprintf( |
86
|
1 |
|
'Entity of type `%s` not found.', $this->getClassName() |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
12 |
|
if (!$entity instanceof JsonApiResource) { |
91
|
1 |
|
throw RestException::notJsonApiResource($entity); |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
return $entity; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param RestRequestContract $request |
99
|
|
|
* @param JsonApiResource $resource |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
2 |
|
public function linkJsonApiResource(RestRequestContract $request, JsonApiResource $resource) |
104
|
|
|
{ |
105
|
2 |
|
return sprintf('%s/%s/%s', $request->getBaseUrl(), $resource->getResourceKey(), $resource->getId()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|