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