Skip to content
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;

class ExampleExtension implements ExtensionInterface {
public function getOfficialLink() {
public function getOfficialLink(): string {
return 'https://example.org/extension-documentation';
}

public function getNamespace() {
public function getNamespace(): string {
return 'foo';
}
}
Expand Down
43 changes: 43 additions & 0 deletions UPGRADE_2_TO_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Upgrade from library v2 to v3

## Checking links, ...

Objects more often use uninitialized properties. `has*()` helper methods have been added to support the new flow.

- `$object->links` to `if ($object->hasLinks()) $object->links`

## Interfaces

When extending interfaces, you'll have to add method argument types and return types.

Check [all interfaces](/src/interfaces) for the correct typing.

## Enums

Content types:
- `Document::CONTENT_TYPE_OFFICIAL` to `ContentTypeEnum::Official`
- `Document::CONTENT_TYPE_DEBUG` to `ContentTypeEnum::Debug`
- `Document::CONTENT_TYPE_JSONP` to `ContentTypeEnum::Jsonp`

Jsonapi versions:
- `Document::JSONAPI_VERSION_1_0` to `JsonapiVersionEnum::V_1_0`
- `Document::JSONAPI_VERSION_1_1` to `JsonapiVersionEnum::V_1_1`
- `Document::JSONAPI_VERSION_LATEST` to `JsonapiVersionEnum::Latest`

Document levels:
- `Document::LEVEL_ROOT` to `DocumentLevelEnum::Root`
- `Document::LEVEL_JSONAPI` to `DocumentLevelEnum::Jsonapi`
- `Document::LEVEL_Resource` to `DocumentLevelEnum::Resource`

Sorting orders:
- `RequestParser->getSortFields()` returns a `SortOrderEnum` instead of `string` for the `order` field

Relationship types:
- `RelationshipObject::TO_ONE` to `RelationshipTypeEnum::ToOne`
- `RelationshipObject::TO_MANY` to `RelationshipTypeEnum::ToMany`

## Internal

Enums:
- `RequestParser::SORT_*` to `SortOrderEnum::*`
- `Validator::OBJECT_CONTAINER_*` to `ObjectContainerEnum::*`
2 changes: 2 additions & 0 deletions examples/atomic_operations_extension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\objects\ResourceObject;
use alsvanzelf\jsonapi\extensions\AtomicOperationsDocument;

Expand Down
8 changes: 5 additions & 3 deletions examples/bootstrap_examples.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
Expand Down Expand Up @@ -110,11 +112,11 @@ class ExampleVersionExtension implements ExtensionInterface {
* the required method
*/

public function getOfficialLink() {
public function getOfficialLink(): string {
return 'https://jsonapi.org/format/1.1/#extension-rules';
}

public function getNamespace() {
public function getNamespace(): string {
return 'version';
}

Expand All @@ -141,7 +143,7 @@ class ExampleTimestampsProfile implements ProfileInterface {
* the required method
*/

public function getOfficialLink() {
public function getOfficialLink(): string {
return 'https://jsonapi.org/recommendations/#authoring-profiles';
}

Expand Down
2 changes: 2 additions & 0 deletions examples/collection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;

Expand Down
2 changes: 2 additions & 0 deletions examples/collection_canonical.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;

Expand Down
2 changes: 2 additions & 0 deletions examples/cursor_pagination_profile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;
use alsvanzelf\jsonapi\profiles\CursorPaginationProfile;
Expand Down
2 changes: 2 additions & 0 deletions examples/errors_all_options.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ErrorsDocument;
use alsvanzelf\jsonapi\objects\ErrorObject;

Expand Down
2 changes: 2 additions & 0 deletions examples/errors_exception_native.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ErrorsDocument;

require 'bootstrap_examples.php';
Expand Down
6 changes: 4 additions & 2 deletions examples/extension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\ContentTypeEnum;
use alsvanzelf\jsonapi\helpers\Converter;

require 'bootstrap_examples.php';
Expand All @@ -28,7 +30,7 @@
* get the json
*/

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

$options = [
Expand Down
2 changes: 2 additions & 0 deletions examples/meta_only.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\MetaDocument;

require 'bootstrap_examples.php';
Expand Down
7 changes: 5 additions & 2 deletions examples/null_values.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\RelationshipTypeEnum;
use alsvanzelf\jsonapi\objects\LinkObject;
use alsvanzelf\jsonapi\objects\RelationshipObject;

Expand All @@ -22,8 +25,8 @@

// show a relationship is not set
$document->addRelationship('bar', null);
$document->addRelationshipObject('baz', new RelationshipObject(RelationshipObject::TO_ONE));
$document->addRelationshipObject('baf', new RelationshipObject(RelationshipObject::TO_MANY));
$document->addRelationshipObject('baz', new RelationshipObject(RelationshipTypeEnum::ToOne));
$document->addRelationshipObject('baf', new RelationshipObject(RelationshipTypeEnum::ToMany));

/**
* sending the response
Expand Down
6 changes: 4 additions & 2 deletions examples/output.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\MetaDocument;
use alsvanzelf\jsonapi\enums\ContentTypeEnum;

require 'bootstrap_examples.php';

Expand Down Expand Up @@ -73,4 +75,4 @@
echo '<pre>';
$document->sendResponse($options);
echo '</pre>';
echo '<p><em>Also sends http status code ('.$document->getHttpStatusCode().') and headers: [Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.']</em></p>';
echo '<p><em>Also sends http status code ('.$document->getHttpStatusCode().') and headers: [Content-Type: '.ContentTypeEnum::Official->value.']</em></p>';
6 changes: 4 additions & 2 deletions examples/profile.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\ContentTypeEnum;
use alsvanzelf\jsonapi\helpers\Converter;

require 'bootstrap_examples.php';
Expand Down Expand Up @@ -30,7 +32,7 @@
* get the json
*/

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

$options = [
Expand Down
2 changes: 2 additions & 0 deletions examples/relationship_to_many_document.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\CollectionDocument;

require 'bootstrap_examples.php';
Expand Down
6 changes: 4 additions & 2 deletions examples/relationship_to_one_document.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\DocumentLevelEnum;

require 'bootstrap_examples.php';

Expand All @@ -11,7 +13,7 @@

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

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

/**
Expand Down
7 changes: 5 additions & 2 deletions examples/relationships.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\RelationshipTypeEnum;
use alsvanzelf\jsonapi\objects\RelationshipObject;
use alsvanzelf\jsonapi\objects\ResourceObject;

Expand Down Expand Up @@ -45,7 +48,7 @@
* to-many relationship, one-by-one
*/

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

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

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

Expand Down
6 changes: 4 additions & 2 deletions examples/request_superglobals.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\enums\ContentTypeEnum;
use alsvanzelf\jsonapi\helpers\RequestParser;

require 'bootstrap_examples.php';
Expand Down Expand Up @@ -44,7 +46,7 @@
$_SERVER['REQUEST_SCHEME'] = 'https';
$_SERVER['HTTP_HOST'] = 'example.org';
$_SERVER['REQUEST_URI'] = '/user/42?'.http_build_query($_GET);
$_SERVER['CONTENT_TYPE'] = Document::CONTENT_TYPE_OFFICIAL;
$_SERVER['CONTENT_TYPE'] = ContentTypeEnum::Official->value;

/**
* parsing the request
Expand Down
2 changes: 2 additions & 0 deletions examples/resource_human_api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\objects\AttributesObject;
use alsvanzelf\jsonapi\objects\LinksObject;
Expand Down
14 changes: 8 additions & 6 deletions examples/resource_links.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use alsvanzelf\jsonapi\Document;
declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\DocumentLevelEnum;

require 'bootstrap_examples.php';

Expand All @@ -13,13 +15,13 @@
*/
$document = ResourceDocument::fromObject($userEntity, $type='user', $userEntity->id);

$selfResourceMeta = ['level' => Document::LEVEL_RESOURCE];
$partnerMeta = ['level' => Document::LEVEL_RESOURCE];
$redirectMeta = ['level' => Document::LEVEL_ROOT];
$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=Document::LEVEL_RESOURCE);
$document->addLink('redirect', '/login', $redirectMeta, $level=Document::LEVEL_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: 2 additions & 0 deletions examples/resource_nested_relations.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;

Expand Down
5 changes: 4 additions & 1 deletion examples/resource_spec_api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\enums\RelationshipTypeEnum;
use alsvanzelf\jsonapi\objects\AttributesObject;
use alsvanzelf\jsonapi\objects\LinksObject;
use alsvanzelf\jsonapi\objects\MetaObject;
Expand Down Expand Up @@ -41,7 +44,7 @@
$resource->setType('user');
$resource->setAttributesObject($attributes42);

$relationship = new RelationshipObject(RelationshipObject::TO_ONE);
$relationship = new RelationshipObject(RelationshipTypeEnum::ToOne);
$relationship->setResource($resource);
$relationships = new RelationshipsObject();
$relationships->addRelationshipObject('friend', $relationship);
Expand Down
2 changes: 2 additions & 0 deletions examples/status_only.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use alsvanzelf\jsonapi\MetaDocument;

require 'bootstrap_examples.php';
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ parameters:
- tests/
- examples/

typeAliases:
PHPStanTypeAlias_InternalOptions: 'array<string, bool|int|null|\alsvanzelf\jsonapi\enums\ContentTypeEnum>'

treatPhpDocTypesAsCertain: false

strictRules:
Expand Down
Loading