Skip to content

Commit 856d83e

Browse files
committed
[Feature] Get an encoder for a named API
An encoder can now be obtained for a named API using the JSON API service's `encoder` method. This contributes towards Issue #36
1 parent 8e5c5d5 commit 856d83e

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. This projec
44

55
## Unreleased
66

7+
### Added
8+
- An encoder can now be obtained for a named API via the JSON API service `encoder` method.
9+
710
### Removed
811
- Removed the validator error factory interface from this library as the one provided by `cloudcreativity/json-api`
912
has the additional methods on it.

src/Api/Repository.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
namespace CloudCreativity\LaravelJsonApi\Api;
2020

2121
use CloudCreativity\JsonApi\Contracts\Http\ApiInterface;
22+
use CloudCreativity\JsonApi\Encoder\Encoder;
2223
use CloudCreativity\JsonApi\Exceptions\RuntimeException;
2324
use CloudCreativity\LaravelJsonApi\Factories\Factory;
2425
use Illuminate\Contracts\Config\Repository as Config;
2526
use Illuminate\Support\Arr;
27+
use Neomerx\JsonApi\Encoder\EncoderOptions;
2628

2729
/**
2830
* Class Repository
@@ -97,6 +99,22 @@ public function retrieveDefinition($apiName)
9799
return $this->definitions[$apiName] = $definition;
98100
}
99101

102+
/**
103+
* @param $apiName
104+
* @param string|null $host
105+
* @param int $options
106+
* @param int $depth
107+
* @return Encoder
108+
*/
109+
public function retrieveEncoder($apiName, $host = null, $options = 0, $depth = 512)
110+
{
111+
$definition = $this->retrieveDefinition($apiName);
112+
$schemas = $this->factory->createContainer($definition->getResources()->getSchemas());
113+
$url = $this->mergeHostAndUrlPrefix($host, $definition->getUrlPrefix());
114+
115+
return $this->factory->createEncoder($schemas, new EncoderOptions($options, $url, $depth));
116+
}
117+
100118
/**
101119
* @param $apiName
102120
* @return ResourceProviders

src/Services/JsonApiService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
use CloudCreativity\JsonApi\Contracts\Http\Requests\RequestInterpreterInterface;
2525
use CloudCreativity\JsonApi\Contracts\Http\Responses\ErrorResponseInterface;
2626
use CloudCreativity\JsonApi\Contracts\Utils\ErrorReporterInterface;
27+
use CloudCreativity\JsonApi\Encoder\Encoder;
2728
use CloudCreativity\JsonApi\Exceptions\RuntimeException;
29+
use CloudCreativity\LaravelJsonApi\Api\Repository;
2830
use CloudCreativity\LaravelJsonApi\Routing\ResourceRegistrar;
2931
use Exception;
3032
use Illuminate\Contracts\Container\Container;
@@ -65,6 +67,21 @@ public function api($apiName, array $options, Closure $routes)
6567
$registrar->api($apiName, $options, $routes);
6668
}
6769

70+
/**
71+
* @param $apiName
72+
* @param string|null $host
73+
* @param int $options
74+
* @param int $depth
75+
* @return Encoder
76+
*/
77+
public function encoder($apiName, $host = null, $options = 0, $depth = 512)
78+
{
79+
/** @var Repository $repository */
80+
$repository = $this->container->make(Repository::class);
81+
82+
return $repository->retrieveEncoder($apiName, $host, $options, $depth);
83+
}
84+
6885
/**
6986
* @inheritdoc
7087
*/

0 commit comments

Comments
 (0)