1919namespace CloudCreativity \LaravelJsonApi \Api ;
2020
2121use Illuminate \Contracts \Routing \UrlGenerator as IlluminateUrlGenerator ;
22- use Neomerx \JsonApi \Contracts \Document \ LinkInterface ;
23- use Neomerx \JsonApi \Contracts \Schema \SchemaFactoryInterface ;
22+ use Neomerx \JsonApi \Contracts \Factories \ FactoryInterface ;
23+ use Neomerx \JsonApi \Contracts \Schema \LinkInterface ;
2424
2525/**
2626 * Class LinkGenerator
@@ -31,28 +31,28 @@ class LinkGenerator
3131{
3232
3333 /**
34- * @var SchemaFactoryInterface
34+ * @var FactoryInterface
3535 */
36- private $ factory ;
36+ private FactoryInterface $ factory ;
3737
3838 /**
3939 * @var UrlGenerator
4040 */
41- private $ urls ;
41+ private UrlGenerator $ urls ;
4242
4343 /**
4444 * @var IlluminateUrlGenerator
4545 */
46- private $ generator ;
46+ private IlluminateUrlGenerator $ generator ;
4747
4848 /**
4949 * LinkGenerator constructor.
5050 *
51- * @param SchemaFactoryInterface $factory
51+ * @param FactoryInterface $factory
5252 * @param UrlGenerator $urls
5353 * @param IlluminateUrlGenerator $generator
5454 */
55- public function __construct (SchemaFactoryInterface $ factory , UrlGenerator $ urls , IlluminateUrlGenerator $ generator )
55+ public function __construct (FactoryInterface $ factory , UrlGenerator $ urls , IlluminateUrlGenerator $ generator )
5656 {
5757 $ this ->factory = $ factory ;
5858 $ this ->urls = $ urls ;
@@ -74,7 +74,7 @@ public function current($meta = null, array $queryParams = [])
7474 $ url .= '? ' . http_build_query ($ queryParams );
7575 }
7676
77- return $ this ->factory -> createLink ($ url , $ meta , true );
77+ return $ this ->createLink ($ url , $ meta , true );
7878 }
7979
8080 /**
@@ -87,7 +87,7 @@ public function current($meta = null, array $queryParams = [])
8787 */
8888 public function index ($ resourceType , $ meta = null , array $ queryParams = [])
8989 {
90- return $ this ->factory -> createLink (
90+ return $ this ->createLink (
9191 $ this ->urls ->index ($ resourceType , $ queryParams ),
9292 $ meta ,
9393 true
@@ -104,7 +104,7 @@ public function index($resourceType, $meta = null, array $queryParams = [])
104104 */
105105 public function create ($ resourceType , $ meta = null , array $ queryParams = [])
106106 {
107- return $ this ->factory -> createLink (
107+ return $ this ->createLink (
108108 $ this ->urls ->create ($ resourceType , $ queryParams ),
109109 $ meta ,
110110 true
@@ -122,7 +122,7 @@ public function create($resourceType, $meta = null, array $queryParams = [])
122122 */
123123 public function read ($ resourceType , $ id , $ meta = null , array $ queryParams = [])
124124 {
125- return $ this ->factory -> createLink (
125+ return $ this ->createLink (
126126 $ this ->urls ->read ($ resourceType , $ id , $ queryParams ),
127127 $ meta ,
128128 true
@@ -140,7 +140,7 @@ public function read($resourceType, $id, $meta = null, array $queryParams = [])
140140 */
141141 public function update ($ resourceType , $ id , $ meta = null , array $ queryParams = [])
142142 {
143- return $ this ->factory -> createLink (
143+ return $ this ->createLink (
144144 $ this ->urls ->update ($ resourceType , $ id , $ queryParams ),
145145 $ meta ,
146146 true
@@ -158,7 +158,7 @@ public function update($resourceType, $id, $meta = null, array $queryParams = []
158158 */
159159 public function delete ($ resourceType , $ id , $ meta = null , array $ queryParams = [])
160160 {
161- return $ this ->factory -> createLink (
161+ return $ this ->createLink (
162162 $ this ->urls ->delete ($ resourceType , $ id , $ queryParams ),
163163 $ meta ,
164164 true
@@ -177,7 +177,7 @@ public function delete($resourceType, $id, $meta = null, array $queryParams = []
177177 */
178178 public function relatedResource ($ resourceType , $ id , $ relationshipKey , $ meta = null , array $ queryParams = [])
179179 {
180- return $ this ->factory -> createLink (
180+ return $ this ->createLink (
181181 $ this ->urls ->relatedResource ($ resourceType , $ id , $ relationshipKey , $ queryParams ),
182182 $ meta ,
183183 true
@@ -196,7 +196,7 @@ public function relatedResource($resourceType, $id, $relationshipKey, $meta = nu
196196 */
197197 public function readRelationship ($ resourceType , $ id , $ relationshipKey , $ meta = null , array $ queryParams = [])
198198 {
199- return $ this ->factory -> createLink (
199+ return $ this ->createLink (
200200 $ this ->urls ->readRelationship ($ resourceType , $ id , $ relationshipKey , $ queryParams ),
201201 $ meta ,
202202 true
@@ -215,7 +215,7 @@ public function readRelationship($resourceType, $id, $relationshipKey, $meta = n
215215 */
216216 public function replaceRelationship ($ resourceType , $ id , $ relationshipKey , $ meta = null , array $ queryParams = [])
217217 {
218- return $ this ->factory -> createLink (
218+ return $ this ->createLink (
219219 $ this ->urls ->replaceRelationship ($ resourceType , $ id , $ relationshipKey , $ queryParams ),
220220 $ meta ,
221221 true
@@ -234,7 +234,7 @@ public function replaceRelationship($resourceType, $id, $relationshipKey, $meta
234234 */
235235 public function addRelationship ($ resourceType , $ id , $ relationshipKey , $ meta = null , array $ queryParams = [])
236236 {
237- return $ this ->factory -> createLink (
237+ return $ this ->createLink (
238238 $ this ->urls ->addRelationship ($ resourceType , $ id , $ relationshipKey , $ queryParams ),
239239 $ meta ,
240240 true
@@ -249,15 +249,35 @@ public function addRelationship($resourceType, $id, $relationshipKey, $meta = nu
249249 * @param $relationshipKey
250250 * @param array|object|null $meta
251251 * @param array $queryParams
252- * @return string
252+ * @return LinkInterface
253253 */
254254 public function removeRelationship ($ resourceType , $ id , $ relationshipKey , $ meta = null , array $ queryParams = [])
255255 {
256- return $ this ->factory -> createLink (
256+ return $ this ->createLink (
257257 $ this ->urls ->removeRelationship ($ resourceType , $ id , $ relationshipKey , $ queryParams ),
258258 $ meta ,
259259 true
260260 );
261261 }
262262
263+ /**
264+ * Create a link.
265+ *
266+ * This method uses the old method signature for creating a link via the Neomerx factory, and converts
267+ * it to a call to the new factory method signature.
268+ *
269+ * @param string $subHref
270+ * @param array|object|null $meta
271+ * @param bool $treatAsHref
272+ * @return LinkInterface
273+ */
274+ private function createLink (string $ subHref , $ meta = null , bool $ treatAsHref = false ): LinkInterface
275+ {
276+ return $ this ->factory ->createLink (
277+ false === $ treatAsHref ,
278+ $ subHref ,
279+ !is_null ($ meta ),
280+ $ meta ,
281+ );
282+ }
263283}
0 commit comments