Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: lint
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
paths:
- "**.php"

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: test
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
paths:
- "**.php"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ If you used v1 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) on h
```php
use alsvanzelf\jsonapi\ResourceDocument;

$document = new ResourceDocument($type='user', $id=42);
$document = new ResourceDocument(type: 'user', id: 42);
$document->add('name', 'Zaphod Beeblebrox');
$document->add('heads', 2);
$document->sendResponse();
Expand Down
2 changes: 1 addition & 1 deletion examples/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$collection = [];

foreach ($users as $user) {
$resource = ResourceObject::fromObject($user, $type='user', $user->id);
$resource = ResourceObject::fromObject($user, type: 'user', id: $user->id);

if ($user->id == 42) {
$ship = new ResourceObject('ship', 5);
Expand Down
2 changes: 1 addition & 1 deletion examples/collection_canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}

$document->setSelfLink('http://example.com/articles');
$document->setPaginationLinks($previous=null, $next='http://example.com/articles?page[offset]=2', $first=null, $last='http://example.com/articles?page[offset]=10');
$document->setPaginationLinks(previousHref: null, nextHref: 'http://example.com/articles?page[offset]=2', firstHref: null, lastHref: 'http://example.com/articles?page[offset]=10');
$document->unsetJsonapiObject();

/**
Expand Down
4 changes: 2 additions & 2 deletions examples/cursor_pagination_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
$document = CollectionDocument::fromResources($user1, $user2, $user42);
$document->applyProfile($profile);

$profile->setCount($document, $exactTotal=3, $bestGuessTotal=10);
$profile->setLinksFirstPage($document, $currentUrl='/users?sort=42&page[size]=10', $lastCursor='zaphod');
$profile->setCount($document, exactTotal: 3, bestGuessTotal: 10);
$profile->setLinksFirstPage($document, baseOrCurrentUrl: '/users?sort=42&page[size]=10', lastCursor: 'zaphod');

/**
* get the json
Expand Down
28 changes: 14 additions & 14 deletions examples/errors_all_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@
* setting all options
*/

$errorHumanApi = new ErrorObject($genericCode='Invalid input', $genericTitle='Too much options', $specificDetails='Please, choose a bit less. Consult your ...', $specificAboutLink='https://www.example.com/explanation.html', $genericTypeLink='https://www.example.com/documentation.html');
$errorHumanApi = new ErrorObject(genericCode: 'Invalid input', genericTitle: 'Too much options', specificDetails: 'Please, choose a bit less. Consult your ...', specificAboutLink: 'https://www.example.com/explanation.html', genericTypeLink: 'https://www.example.com/documentation.html');

$errorSpecApi = new ErrorObject();

// mark the cause of the error
$errorSpecApi->blameJsonPointer($pointer='/data/attributes/title');
$errorSpecApi->blameQueryParameter($parameter='filter');
$errorSpecApi->blameHeader($headerName='X-Foo');
$errorSpecApi->blameJsonPointer(pointer: '/data/attributes/title');
$errorSpecApi->blameQueryParameter(parameter: 'filter');
$errorSpecApi->blameHeader(headerName: 'X-Foo');

// an identifier useful for helpdesk purposes
$errorSpecApi->setUniqueIdentifier($id=42);
$errorSpecApi->setUniqueIdentifier(id: 42);

// add meta data as you would on a normal json response
$errorSpecApi->addMeta($key='foo', $value='bar');
$errorSpecApi->addMeta(key: 'foo', value: 'bar');

// or as object
$metaObject = new \stdClass();
$metaObject->property = 'value';
$errorSpecApi->addMeta($key='object', $metaObject);
$errorSpecApi->addMeta(key: 'object', value: $metaObject);

// the http status code
// @note it is better to set this on the jsonapi\errors object ..
// .. as only a single one can be consumed by the browser
$errorSpecApi->setHttpStatusCode($httpStatusCode=404);
$errorSpecApi->setHttpStatusCode(httpStatusCode: 404);

// if not set during construction, set them here
$errorSpecApi->setApplicationCode($genericCode='Invalid input');
$errorSpecApi->setHumanTitle($genericTitle='Too much options');
$errorSpecApi->setHumanDetails($specificDetails='Please, choose a bit less. Consult your ...');
$errorSpecApi->setAboutLink($specificAboutLink='https://www.example.com/explanation.html', ['foo'=>'bar']);
$errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);
$errorSpecApi->setApplicationCode(genericCode: 'Invalid input');
$errorSpecApi->setHumanTitle(genericTitle: 'Too much options');
$errorSpecApi->setHumanDetails(specificDetails: 'Please, choose a bit less. Consult your ...');
$errorSpecApi->setAboutLink(href: 'https://www.example.com/explanation.html', meta: ['foo'=>'bar']);
$errorSpecApi->setTypeLink(href: 'https://www.example.com/documentation.html', meta: ['foo'=>'bar']);

/**
* prepare multiple error objects for the errors response
Expand All @@ -68,7 +68,7 @@
$document->addErrorObject($errorSpecApi);
$document->addErrorObject($anotherError);
$document->addException($someException);
$document->add($genericCode='Authentication error', $genericTitle='Not logged in');
$document->add(genericCode: 'Authentication error', genericTitle: 'Not logged in');
$document->addLink('redirect', '/login', ['label'=>'Log in']);

$document->setHttpStatusCode(400);
Expand Down
2 changes: 1 addition & 1 deletion examples/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* get the json
*/

$contentType = Converter::prepareContentType(ContentTypeEnum::Official, [$extension], []);
$contentType = Converter::prepareContentType(ContentTypeEnum::Official, extensions: [$extension], profiles: []);
echo '<code>Content-Type: '.$contentType.'</code>'.PHP_EOL;

$options = [
Expand Down
12 changes: 6 additions & 6 deletions examples/output.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

echo '<h2>Get the array</h2>';
echo '<pre style="font-size: large;">$document->toArray();</pre>';
echo '<pre>'.var_export($document->toArray(), true).'</pre>';
echo '<pre>'.var_export($document->toArray(), return: true).'</pre>';

/**
* get the json
Expand All @@ -25,16 +25,16 @@
$options = ['prettyPrint' => true];
echo '<h2>Get the json</h2>';
echo '<pre style="font-size: large;">$document->toJson();</pre>';
echo '<pre>'.var_export($document->toJson($options), true).'</pre>';
echo '<pre>'.var_export($document->toJson($options), return: true).'</pre>';

/**
* use own json_encode
*/

$options = ['prettyPrint' => true];
echo '<h2>Use own <code>json_encode()</code></h2>';
echo '<pre style="font-size: large;">json_encode($document, JSON_PRETTY_PRINT);</pre>';
echo '<pre>'.var_export(json_encode($document, JSON_PRETTY_PRINT), true).'</pre>';
echo '<pre style="font-size: large;">json_encode($document, flags: JSON_PRETTY_PRINT);</pre>';
echo '<pre>'.var_export(json_encode($document, flags: JSON_PRETTY_PRINT), return: true).'</pre>';

/**
* get custom json (for a non-spec array)
Expand All @@ -51,7 +51,7 @@
echo '$options = [\'array\' => $customArray];'.PHP_EOL;
echo '$document->toJson($options);'.PHP_EOL;
echo '</pre>';
echo '<pre>'.var_export($document->toJson($options), true).'</pre>';
echo '<pre>'.var_export($document->toJson($options), return: true).'</pre>';

/**
* get jsonp with callback
Expand All @@ -63,7 +63,7 @@
echo '$options = [\'jsonpCallback\' => \'callback\'];'.PHP_EOL;
echo '$document->toJson($options);'.PHP_EOL;
echo '</pre>';
echo '<pre>'.var_export($document->toJson($options), true).'</pre>';
echo '<pre>'.var_export($document->toJson($options), return: true).'</pre>';

/**
* send json response
Expand Down
2 changes: 1 addition & 1 deletion examples/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* get the json
*/

$contentType = Converter::prepareContentType(ContentTypeEnum::Official, [], [$profile]);
$contentType = Converter::prepareContentType(ContentTypeEnum::Official, extensions: [], profiles: [$profile]);
echo '<code>Content-Type: '.$contentType.'</code>'.PHP_EOL;

$options = [
Expand Down
2 changes: 1 addition & 1 deletion examples/relationship_to_one_document.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$relationshipDocument = new ResourceDocument('author', 12);

$relationshipDocument->setSelfLink('/articles/1/relationship/author', $meta=[], $level=DocumentLevelEnum::Root);
$relationshipDocument->setSelfLink('/articles/1/relationship/author', level: DocumentLevelEnum::Root);
$relationshipDocument->addLink('related', '/articles/1/author');

/**
Expand Down
10 changes: 5 additions & 5 deletions examples/relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
*/

$options = ['includeContainedResources' => false];
$document->addRelationship('excluded-ship', $ship2Resource, $links=[], $meta=[], $options);
$document->addRelationship('excluded-ship', $ship2Resource, options: $options);

/**
* to-many relationship, one-by-one
*/

$relationshipObject = new RelationshipObject($type=RelationshipTypeEnum::ToMany);
$relationshipObject = new RelationshipObject(type: RelationshipTypeEnum::ToMany);
$relationshipObject->addResource($friend1Resource);
$relationshipObject->addResource($friend2Resource);

Expand All @@ -68,7 +68,7 @@
* to-many relationship, different types
*/

$relationshipObject = new RelationshipObject($type=RelationshipTypeEnum::ToMany);
$relationshipObject = new RelationshipObject(type: RelationshipTypeEnum::ToMany);
$relationshipObject->addResource($ship1Resource);
$relationshipObject->addResource($dockResource);

Expand All @@ -78,10 +78,10 @@
* custom
*/
$jsonapi = new ResourceDocument('user', 1);
$custom_relation = [
$customRelation = [
'data' => ['cus' => 'tom'],
];
$jsonapi->addRelationship('custom', $custom_relation);
$jsonapi->addRelationship('custom', $customRelation);

/**
* sending the response
Expand Down
4 changes: 2 additions & 2 deletions examples/resource_human_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
* objects are converted into arrays using their public keys
*/

$document = ResourceDocument::fromObject($user1, $type='user', $user1->id);
$document = ResourceDocument::fromObject($user1, type: 'user', id: $user1->id);
$document->add('location', $user1->getCurrentLocation());
$document->addLink('homepage', 'https://jsonapi.org');
$document->addMeta('difference', 'is in the code to generate this');

$relation = ResourceDocument::fromObject($user42, $type='user', $user42->id);
$relation = ResourceDocument::fromObject($user42, type: 'user', id: $user42->id);
$document->addRelationship('friend', $relation);

/**
Expand Down
6 changes: 3 additions & 3 deletions examples/resource_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* add links in different ways to a resource
* self links are adding both at root and in data levels
*/
$document = ResourceDocument::fromObject($userEntity, $type='user', $userEntity->id);
$document = ResourceDocument::fromObject($userEntity, type: 'user', id: $userEntity->id);

$selfResourceMeta = ['level' => DocumentLevelEnum::Resource->name];
$partnerMeta = ['level' => DocumentLevelEnum::Resource->name];
$redirectMeta = ['level' => DocumentLevelEnum::Root->name];

$document->setSelfLink('/user/42', $selfResourceMeta);
$document->addLink('partner', '/user/1', $partnerMeta, $level=DocumentLevelEnum::Resource);
$document->addLink('redirect', '/login', $redirectMeta, $level=DocumentLevelEnum::Root);
$document->addLink('partner', '/user/1', $partnerMeta, level: DocumentLevelEnum::Resource);
$document->addLink('redirect', '/login', $redirectMeta, level: DocumentLevelEnum::Root);

/**
* sending the response
Expand Down
2 changes: 1 addition & 1 deletion examples/resource_nested_relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* building up the json response
*/

$document = ResourceDocument::fromObject($userEntity, $type='user', $userEntity->id);
$document = ResourceDocument::fromObject($userEntity, type: 'user', id: $userEntity->id);
$document->addRelationship('ship', $ship);

/**
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

// @see https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md for more rules
Expand All @@ -16,6 +17,7 @@
])
->withRules([
DeclareStrictTypesRector::class,
AddTestsVoidReturnTypeWhereNoReturnRector::class,
])
->withSkip([
// better explicit readability
Expand Down
6 changes: 3 additions & 3 deletions src/CollectionDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class CollectionDocument extends DataDocument implements PaginableInterface, Res
*
* adds included resources if found inside the resource's relationships, use {@see ->addResource()} to change that behavior
*/
public static function fromResources(ResourceInterface ...$resources): self {
$collectionDocument = new self();
public static function fromResources(ResourceInterface ...$resources): static {
$collectionDocument = new static();

foreach ($resources as $resource) {
$collectionDocument->addResource($resource);
Expand Down Expand Up @@ -98,7 +98,7 @@ public function addResource(ResourceInterface $resource, array $options=[]): voi
throw new InputException('does not make sense to add empty resources to a collection');
}

$options = array_merge(self::$defaults, $options);
$options = [...self::$defaults, ...$options];

$this->validator->claimUsedResourceIdentifier($resource);

Expand Down
Loading