Skip to content

Commit d0d1baf

Browse files
authored
Merge pull request #88 from lode/cleanup-and-document
2 parents 888b2c7 + 8a0dee9 commit d0d1baf

10 files changed

Lines changed: 133 additions & 90 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ while being easy to understand for people without knowledge of the jsonapi stand
88
The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them.
99
Read more about it at [jsonapi.org](https://jsonapi.org/).
1010

11+
The library's code uses strict typing and is checked against a high standard in phpstan and rector. It is tested extensively with 99% coverage.
12+
1113

1214
## Installation
1315

@@ -19,9 +21,9 @@ composer require alsvanzelf/jsonapi
1921

2022
The library requires php 8.2. Use the latest [v2.x release](/releases/tag/v2.5.0) for lower php versions.
2123

22-
#### Upgrading from v1
24+
#### Upgrading from v1 or v2
2325

24-
If you used v1 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) on how to upgrade.
26+
If you used v1 or v2 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) or [UPGRADE_2_TO_3.md](/UPGRADE_1_TO_2.md) on how to upgrade.
2527

2628

2729

UPGRADE_2_TO_3.md

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,70 @@
11
# Upgrade from library v2 to v3
22

3-
## Checking links, ...
3+
## Enums
44

5-
Objects more often use uninitialized properties. `has*()` helper methods have been added to support the new flow.
5+
All constants have been replaced by enums.
66

7-
- `$object->links` to `if ($object->hasLinks()) $object->links`
7+
Content types:
8+
- `Document::CONTENT_TYPE_OFFICIAL` (`'application/vnd.api+json'`) to `ContentTypeEnum::Official`
9+
- `Document::CONTENT_TYPE_DEBUG` (`'application/json'`) to `ContentTypeEnum::Debug`
10+
- `Document::CONTENT_TYPE_JSONP` (`'application/javascript'`) to `ContentTypeEnum::Jsonp`
811

9-
## Interfaces
12+
Jsonapi versions:
13+
- `Document::JSONAPI_VERSION_1_0` (`'1.0'`) to `JsonapiVersionEnum::V_1_0`
14+
- `Document::JSONAPI_VERSION_1_1` (`'1.1'`) to `JsonapiVersionEnum::V_1_1`
15+
- `Document::JSONAPI_VERSION_LATEST` (`'1.1'`) to `JsonapiVersionEnum::latest()` (still refering to v1.1)
1016

11-
When extending interfaces, you'll have to add method argument types and return types.
17+
Document levels:
18+
- `Document::LEVEL_ROOT` (`'root'`) to `DocumentLevelEnum::Root`
19+
- `Document::LEVEL_JSONAPI` (`'jsonapi'`) to `DocumentLevelEnum::Jsonapi`
20+
- `Document::LEVEL_Resource` (`'resource'`) to `DocumentLevelEnum::Resource`
1221

13-
Check [all interfaces](/src/interfaces) for the correct typing.
22+
Sorting orders (as return value in the `order` field from `RequestParser->getSortFields()`):
23+
- `RequestParser::SORT_ASCENDING` (`'ascending'`) to `SortOrderEnum::Ascending`
24+
- `RequestParser::SORT_DESCENDING` (`'descending'`) to `SortOrderEnum::Descending`
1425

15-
## Enums
26+
Relationship types:
27+
- `RelationshipObject::TO_ONE` (`'one'`) to `RelationshipTypeEnum::ToOne`
28+
- `RelationshipObject::TO_MANY` (`'many'`) to `RelationshipTypeEnum::ToMany`
1629

17-
Content types:
18-
- `Document::CONTENT_TYPE_OFFICIAL` to `ContentTypeEnum::Official`
19-
- `Document::CONTENT_TYPE_DEBUG` to `ContentTypeEnum::Debug`
20-
- `Document::CONTENT_TYPE_JSONP` to `ContentTypeEnum::Jsonp`
30+
Validator object containers (only used internally):
31+
- `Validator::OBJECT_CONTAINER_*` to `ObjectContainerEnum::*`
2132

22-
Jsonapi versions:
23-
- `Document::JSONAPI_VERSION_1_0` to `JsonapiVersionEnum::V_1_0`
24-
- `Document::JSONAPI_VERSION_1_1` to `JsonapiVersionEnum::V_1_1`
25-
- `Document::JSONAPI_VERSION_LATEST` to `JsonapiVersionEnum::Latest`
2633

27-
Document levels:
28-
- `Document::LEVEL_ROOT` to `DocumentLevelEnum::Root`
29-
- `Document::LEVEL_JSONAPI` to `DocumentLevelEnum::Jsonapi`
30-
- `Document::LEVEL_Resource` to `DocumentLevelEnum::Resource`
34+
## Strict type checking
3135

32-
Sorting orders:
33-
- `RequestParser->getSortFields()` returns a `SortOrderEnum` instead of `string` for the `order` field
36+
When using the library you shouldn't notice this unless passing values of the wrong type. When extending the library you probably will notice this when overriding methods.
3437

35-
Relationship types:
36-
- `RelationshipObject::TO_ONE` to `RelationshipTypeEnum::ToOne`
37-
- `RelationshipObject::TO_MANY` to `RelationshipTypeEnum::ToMany`
38+
In both cases the upgrade is relatively easy, you can just follow php's type errors. Using phpstan will also help you find typing mismatches.
3839

39-
## Internal
40+
Some invalid types were already checked at run-time and threw library exceptions. This now changed into php type errors.
4041

41-
Enums:
42-
- `RequestParser::SORT_*` to `SortOrderEnum::*`
43-
- `Validator::OBJECT_CONTAINER_*` to `ObjectContainerEnum::*`
42+
43+
## Object properties are uninitialized by default
44+
45+
Objects more often use uninitialized properties. In some cases, `has*()` helper methods have been added to support the new flow. Otherwise, an `isset()` can be used if needed.
46+
47+
_This should only affect you when you extend the libraries classes. Otherwise you can just use the library methods to handle these properties._
48+
49+
- `->links`, on `Document`, `ErrorObject`, `RelationshipObject`, `ResourceObject`, can be checked with `->hasLinks()`
50+
- `Document`: `->httpStatusCode`, `->jsonapi`, `->meta`
51+
- `ErrorObject`: `->id`, `->code`, `->title`, `->detail`, `->meta`, `->httpStatusCode`
52+
- `JsonapiObject`: `->meta`, `->version`
53+
- `LinkObject`: `->href`, `->rel`, `->describedby`, `->title`, `->type`, `->meta`
54+
- `RelationshipObject`: `->meta`, `->resource`, `->type`
55+
- `ResourceDocument`: `->resource`
56+
- `ResourceIdentifierObject`: `->type`, `->id`, `->lid`, `->meta`, `->validator`
57+
- `ResourceObject`: `->attributes`, `->relationships`
58+
59+
60+
## Json encoding and decoding errors
61+
62+
The default encoding options changed to also include `JSON_THROW_ON_ERROR`. Failure to encode already used to throw an exception, but now it will default to a `\JsonException`.
63+
64+
65+
## Removed deprecations
66+
67+
- Old v1-style classes (`base.php`, `collection.php`, `error.php`, `errors.php`, `exception.php`, `resource.php`, `response.php`)
68+
- Array-style links (`->appendLink()`, `->addLinksArray()`, `->appendLinkObject()`, `ErrorObject->appendTypeLink()`, `LinksArray`, `LinksObject->append()`)
69+
- `CursorPaginationProfile->getKeyword()`
70+
- `Converter::mergeProfilesInContentType()`, use `Converter::prepareContentType()` instead

0 commit comments

Comments
 (0)