44
55namespace ApiSkeletons \Laravel \HAL \Doctrine ;
66
7+ use ApiSkeletons \Laravel \HAL \Doctrine \NamingStrategy \NamingStrategyInterface ;
78use ApiSkeletons \Laravel \HAL \Hydrator ;
89use ApiSkeletons \Laravel \HAL \Resource ;
910use DateTime ;
10- use Doctrine \Inflector \Inflector ;
11- use Doctrine \Inflector \InflectorFactory ;
1211use Doctrine \ORM \EntityManager ;
1312use Doctrine \ORM \Mapping \MappingException ;
1413use Exception ;
1514use Illuminate \Foundation \Application ;
16- use ReflectionClass ;
1715
1816use function array_diff_key ;
1917use function array_flip ;
@@ -28,7 +26,7 @@ class DoctrineHydrator extends Hydrator
2826 protected array $ config = [];
2927 protected string $ configurationSection = 'default ' ;
3028 protected EntityManager $ entityManager ;
31- protected Inflector $ inflector ;
29+ protected NamingStrategyInterface $ namingStrategy ;
3230
3331 public function __construct (Application $ application )
3432 {
@@ -50,8 +48,8 @@ public function __construct(Application $application)
5048
5149 // @codeCoverageIgnoreEnd
5250
53- $ this ->entityManager = $ application ->get ($ this ->config ['entityManager ' ]);
54- $ this ->inflector = InflectorFactory:: create ()-> build ( );
51+ $ this ->entityManager = $ application ->get ($ this ->config ['entityManager ' ]);
52+ $ this ->namingStrategy = $ application -> get ( $ this -> config [ ' namingStrategy ' ] );
5553 }
5654
5755 public function extract (mixed $ entity ): Resource
@@ -108,14 +106,14 @@ public function extract(mixed $entity): Resource
108106
109107 if ($ entityMetadata ->isAssociationInverseSide ($ associationName )) {
110108 $ resource ->addLink (
111- $ associationName ,
109+ $ this -> namingStrategy -> association ( $ associationName) ,
112110 route ($ associationRouteName , [
113111 'filter ' => [$ associationMapping ['mappedBy ' ] => $ identifier ],
114112 ])
115113 );
116114 } else {
117115 $ resource ->addLink (
118- $ associationName ,
116+ $ this -> namingStrategy -> association ( $ associationName) ,
119117 route ($ associationRouteName , [
120118 'filter ' => [$ associationMapping ['inversedBy ' ] => $ identifier ],
121119 ])
@@ -128,13 +126,13 @@ public function extract(mixed $entity): Resource
128126
129127 $ associationRouteName = $ this ->getRouteName ($ associationMapping ['targetEntity ' ], 'entity ' );
130128 $ resource ->addLink (
131- $ associationName ,
129+ $ this -> namingStrategy -> association ( $ associationName) ,
132130 route ($ associationRouteName , $ identifier )
133131 );
134132 } else {
135133 // For 1:1 relationships, only embed the owning side
136134 $ resource ->addEmbeddedResource (
137- $ associationName ,
135+ $ this -> namingStrategy -> association ( $ associationName) ,
138136 $ data [$ associationName ]
139137 );
140138 }
@@ -149,7 +147,7 @@ protected function getRouteName(string $entityName, string $routeType): string
149147 return $ this ->config ['entities ' ][$ entityName ]['routeNames ' ][$ routeType ] ??
150148 str_replace (
151149 '{entityName} ' ,
152- $ this ->inflector -> urlize (( new ReflectionClass ( $ entityName))-> getShortName () ),
150+ $ this ->namingStrategy -> route ( $ entityName ),
153151 $ this ->config ['routeNamePatterns ' ][$ routeType ]
154152 );
155153 }
0 commit comments