Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
cb5fabd
aliases are removed from the 1.1 spec
lode Aug 13, 2019
47d867f
allow adding local id if there is no normal id
lode Oct 20, 2020
f6481f4
make sure to pass along the local id when we normally pass the id
lode Oct 20, 2020
defb86d
easily set a json schema or other describing link to the document
lode Oct 20, 2020
e886ae4
support the new link 'native-meta' properties
lode Oct 20, 2020
b13f326
fix type checking
lode Oct 24, 2020
e1fd802
Merge branch 'master' into remove-aliasing
lode Apr 5, 2021
1ac504a
Merge branch 'remove-aliasing' into split-extension-and-profile
lode Apr 5, 2021
17eea59
add interface for extensions
lode Apr 5, 2021
760548f
add extension and profiles links in the jsonapi object and content-type
lode Apr 5, 2021
d9988db
allow to set extension members
lode Apr 5, 2021
8c79241
adjust example code for profiles and add for extensions
lode Apr 5, 2021
ab8a2d2
adjust tests
lode Apr 5, 2021
f69059b
adjust and add more tests
lode Apr 5, 2021
7722c17
add generic tests around extensions
lode Apr 5, 2021
5eaf3f5
add ready-to-use atomic operations extension
lode Apr 5, 2021
eca7637
test profile link appears in jsonapi object
lode Apr 5, 2021
0a9f38d
try to fix pre-php7.2 date constants
lode Apr 5, 2021
a980a46
cleanup
lode Apr 5, 2021
a513d3d
improve test coverage
lode Apr 5, 2021
e7fc8ff
improve test coverage
lode Apr 5, 2021
ad40d13
Merge branch 'remove-aliasing' into split-extension-and-profile
lode Apr 5, 2021
214a517
prevent using setUp as it has a BC break between php7.1 and 7.2
lode Apr 5, 2021
867a224
check for valid extension namespaces
lode Apr 5, 2021
7140bbe
Merge branch 'split-extension-and-profile' into remove-array-links
lode Apr 5, 2021
a68da0e
Merge branch 'main' into split-extension-and-profile
lode Apr 25, 2021
6aa605c
Merge branch 'main' into remove-aliasing
lode Apr 25, 2021
2a1a7a1
Merge branch 'main' into remove-array-links
lode Apr 25, 2021
e709233
Merge branch 'main' into new-link-properties
lode Apr 25, 2021
f8eb1df
Merge branch 'main' into described-by-at-root
lode Apr 25, 2021
f3d6d74
Merge branch 'main' into local-ids
lode Apr 25, 2021
260e411
Merge branch 'main' into remove-aliasing
lode May 14, 2021
5088604
Merge branch 'main' into local-ids
lode May 14, 2021
66d3949
Merge branch 'main' into described-by-at-root
lode May 14, 2021
06d4775
Merge branch 'main' into new-link-properties
lode May 14, 2021
74501c8
Merge branch 'remove-aliasing' into split-extension-and-profile
lode May 14, 2021
7efeb44
Merge branch 'main' into remove-aliasing
lode May 14, 2021
2e9cc5c
Merge branch 'main' into local-ids
lode May 14, 2021
a72bc22
Merge branch 'main' into described-by-at-root
lode May 14, 2021
ce2a31f
Merge branch 'main' into new-link-properties
lode May 14, 2021
8b9492e
Merge branch 'remove-aliasing' into split-extension-and-profile
lode May 14, 2021
9529521
explain when to use local id
lode May 14, 2021
a396260
test using local ids
lode May 14, 2021
65ebda7
test described by root link
lode May 14, 2021
3c3f011
fix adding link languages
lode May 15, 2021
7e53ef2
test emptyness
lode May 15, 2021
03f34f5
add some future validation ideas
lode May 15, 2021
353ef77
test the new link properties
lode May 15, 2021
51a9d27
be explicit about time
lode May 15, 2021
107c331
annotate new tests
lode May 15, 2021
68cd3e7
Merge branch 'remove-aliasing' into 2.3.0
lode May 15, 2021
07affd7
Merge branch 'local-ids' into 2.3.0
lode May 15, 2021
778b563
Merge branch 'described-by-at-root' into 2.3.0
lode May 15, 2021
2f23979
Merge branch 'new-link-properties' into 2.3.0
lode May 15, 2021
210ce0c
Merge branch '2.3.0' into split-extension-and-profile
lode May 15, 2021
8d21f97
Merge branch 'split-extension-and-profile' into 2.3.0
lode May 15, 2021
a767475
Merge branch '2.3.0' into remove-array-links
lode May 15, 2021
c01a21e
mark all methods using link arrays as deprecated
lode May 16, 2021
c168e95
adjust tests
lode May 16, 2021
9647198
keep code coverage for the deprecated method
lode May 16, 2021
11a2fc7
CS
lode May 16, 2021
9c64d6c
test
lode May 16, 2021
05b0b8e
Merge branch '2.3.0' into type-on-root-self
lode May 16, 2021
e02f986
add the extension/profile headers to the root self link
lode May 16, 2021
9c3143b
improve tests around applying extensions and profiles
lode May 16, 2021
9afcda0
test whether the extended self link works
lode May 16, 2021
d357322
re-use links initialization of the link manager
lode May 16, 2021
5351415
Merge pull request #62 from lode/remove-array-links
lode Aug 21, 2021
2675b65
Merge branch '2.3.0' into type-on-root-self
lode Aug 21, 2021
0073d5d
Merge branch 'main' into 2.3.0
lode Aug 21, 2021
8dc7855
Merge branch '2.3.0' into type-on-root-self
lode Aug 21, 2021
8332164
Merge pull request #67 from lode/type-on-root-self
lode Aug 21, 2021
30e6fb3
test extentions link for atomic operations in self link
lode Aug 22, 2021
bd98dd0
make it easy to set localids on resource documents as well
lode Aug 22, 2021
b08b5e3
make it easy to fetch local ids from request documents
lode Aug 22, 2021
aaf9ec9
Merge branch 'main' into 2.3.0
lode Aug 23, 2021
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,18 @@ It has support for generating & sending documents with:
- resource collections
- to-one and to-many relationships
- errors (easily turning exceptions into jsonapi output)
- v1.1 extensions via profiles
- v1.1 extensions and profiles
- v1.1 @-members for JSON-LD and others

Also there's tools to help processing of incoming requests:

- parse request options (include paths, sparse fieldsets, sort fields, pagination, filtering)
- parse request documents for creating, updating and deleting resources and relationships

Next to custom extensions, the following [official extensions](https://jsonapi.org/extensions/) are included:
Next to custom extensions/profiles, the following [official extensions/profiles](https://jsonapi.org/extensions/) are included:

- Cursor Pagination ([example code](/examples/cursor_pagination_profile.php), [specification](https://jsonapi.org/profiles/ethanresnick/cursor-pagination/))
- Atomic Operations extension ([example code](/examples/atomic_operations_extension.php), [specification](https://jsonapi.org/ext/atomic/))
- Cursor Pagination profile ([example code](/examples/cursor_pagination_profile.php), [specification](https://jsonapi.org/profiles/ethanresnick/cursor-pagination/))

Plans for the future include:

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

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

require 'bootstrap_examples.php';

/**
* use the atomic operations extension as extension to the document
*/

$document = new AtomicOperationsDocument();

$user1 = new ResourceObject('user', 1);
$user2 = new ResourceObject('user', 2);
$user42 = new ResourceObject('user', 42);

$user1->add('name', 'Ford');
$user2->add('name', 'Arthur');
$user42->add('name', 'Zaphod');

$document->addResults($user1);
$document->addResults($user2);
$document->addResults($user42);

/**
* get the json
*/

$options = [
'prettyPrint' => true,
];
echo '<pre>'.$document->toJson($options);
48 changes: 39 additions & 9 deletions examples/bootstrap_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\helpers\ProfileAliasManager;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;

Expand Down Expand Up @@ -102,29 +102,59 @@ function getCurrentLocation() {
}
}

class ExampleVersionProfile extends ProfileAliasManager implements ProfileInterface {
class ExampleVersionExtension implements ExtensionInterface {
/**
* the required methods (next to extending ProfileAliasManager)
* the required method
*/

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

public function getOfficialKeywords() {
return ['version'];
public function getNamespace() {
return 'version';
}

/**
* optionally helpers for the specific profile
* optionally helpers for the specific extension
*/

public function setVersion(ResourceInterface $resource, $version) {
if ($resource instanceof ResourceDocument) {
$resource->addMeta($this->getKeyword('version'), $version, $level=Document::LEVEL_RESOURCE);
$resource->getResource()->addExtensionMember($this, 'id', $version);
}
else {
$resource->addMeta($this->getKeyword('version'), $version);
$resource->addExtensionMember($this, 'id', $version);
}
}
}

class ExampleTimestampsProfile implements ProfileInterface {
/**
* the required method
*/

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

/**
* optionally helpers for the specific profile
*/

public function setTimestamps(ResourceInterface $resource, \DateTimeInterface $created=null, \DateTimeInterface $updated=null) {
if ($resource instanceof ResourceIdentifierObject) {
throw new Exception('cannot add attributes to identifier objects');
}

$timestamps = [];
if ($created !== null) {
$timestamps['created'] = $created->format(\DateTime::ISO8601);
}
if ($updated !== null) {
$timestamps['updated'] = $updated->format(\DateTime::ISO8601);
}

$resource->add('timestamps', $timestamps);
}
}
2 changes: 1 addition & 1 deletion examples/errors_all_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$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->appendTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);
$errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);

/**
* prepare multiple error objects for the errors response
Expand Down
32 changes: 0 additions & 32 deletions examples/example_profile.php

This file was deleted.

37 changes: 37 additions & 0 deletions examples/extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\helpers\Converter;

require 'bootstrap_examples.php';

/**
* use an extension extend the document with new members
*/

$extension = new ExampleVersionExtension();

$document = new ResourceDocument('user', 42);
$document->applyExtension($extension);

$document->add('foo', 'bar');

/**
* you can apply the rules of the extension manually
* or use methods of the extension if provided
*/

$extension->setVersion($document, '2019');

/**
* get the json
*/

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

$options = [
'prettyPrint' => true,
];
echo '<pre>'.$document->toJson($options);
4 changes: 3 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ <h3>Misc</h3>
<li><a href="null_values.php">Null values if explicitly not available</a></li>
<li><a href="meta_only.php">Meta-only use-cases</a></li>
<li><a href="status_only.php">Status-only</a></li>
<li><a href="example_profile.php">Example profile</a></li>
<li><a href="extension.php">Example extension</a></li>
<li><a href="atomic_operations_extension.php">Atomic operations extension</a></li>
<li><a href="profile.php">Example profile</a></li>
<li><a href="cursor_pagination_profile.php">Cursor pagination profile</a></li>
<li><a href="output.php">Different ways to output</a></li>
</ul>
Expand Down
39 changes: 39 additions & 0 deletions examples/profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\helpers\Converter;

require 'bootstrap_examples.php';

/**
* use a profile to define rules for members
*/

$profile = new ExampleTimestampsProfile();

$document = new ResourceDocument('user', 42);
$document->applyProfile($profile);

$document->add('foo', 'bar');

/**
* you can apply the rules of the profile manually
* or use methods of the profile if provided
*/

$created = new \DateTime('-1 year');
$updated = new \DateTime('-1 month');
$profile->setTimestamps($document, $created, $updated);

/**
* get the json
*/

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

$options = [
'prettyPrint' => true,
];
echo '<pre>'.$document->toJson($options);
Loading