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
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test
on:
pull_request:
paths:
- "**.php"

jobs:
test:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: Setup Composer
uses: ramsey/composer-install@v3
with:
composer-options: "--no-scripts"

- name: Run phpunit
run: ./vendor/bin/phpunit
18 changes: 14 additions & 4 deletions examples/bootstrap_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
use alsvanzelf\jsonapi\interfaces\HasAttributesInterface;
use alsvanzelf\jsonapi\interfaces\HasExtensionMembersInterface;
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
use alsvanzelf\jsonapi\objects\ResourceObject;

ini_set('display_errors', 1);
error_reporting(-1);
Expand Down Expand Up @@ -55,7 +58,7 @@ class ExampleDataset {

public static function getRecord($type, $id) {
if (!isset(self::$records[$type][$id])) {
throw new Exception('sorry, we have a limited dataset');
throw new \Exception('sorry, we have a limited dataset');
}

return self::$records[$type][$id];
Expand Down Expand Up @@ -121,6 +124,10 @@ public function getNamespace() {
*/

public function setVersion(ResourceInterface $resource, $version) {
if ($resource instanceof HasExtensionMembersInterface === false) {
throw new \Exception('resource doesn\'t have extension members');
}

if ($resource instanceof ResourceDocument) {
$resource->getResource()->addExtensionMember($this, 'id', $version);
}
Expand All @@ -143,9 +150,12 @@ public function getOfficialLink() {
* optionally helpers for the specific profile
*/

/**
* @param ResourceInterface&HasAttributesInterface $resource
*/
public function setTimestamps(ResourceInterface $resource, ?\DateTimeInterface $created=null, ?\DateTimeInterface $updated=null) {
if ($resource instanceof ResourceIdentifierObject) {
throw new Exception('cannot add attributes to identifier objects');
if ($resource instanceof HasAttributesInterface === false) {
throw new \Exception('cannot add attributes to identifier objects');
}

$timestamps = [];
Expand All @@ -156,6 +166,6 @@ public function setTimestamps(ResourceInterface $resource, ?\DateTimeInterface $
$timestamps['updated'] = $updated->format(\DateTime::ISO8601);
}

$resource->add('timestamps', $timestamps);
$resource->addAttribute('timestamps', $timestamps);
}
}
2 changes: 1 addition & 1 deletion examples/errors_exception_native.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

try {
throw new Exception('unknown user', 404);
throw new \Exception('unknown user', 404);
}
catch (Exception $e) {
$options = [
Expand Down
2 changes: 1 addition & 1 deletion examples/relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
$custom_relation = [
'data' => ['cus' => 'tom'],
];
$jsonapi->add_relation('custom', $custom_relation);
$jsonapi->addRelationship('custom', $custom_relation);

/**
* sending the response
Expand Down
2 changes: 1 addition & 1 deletion phpstan.bonus.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
level: 9
level: 10

treatPhpDocTypesAsCertain: true

Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
# slowly increase
level: 1
level: 4
paths:
- src/
- tests/
Expand All @@ -14,9 +14,11 @@ parameters:
- src/resource.php
- src/response.php

treatPhpDocTypesAsCertain: false

strictRules:
allRules: false

reportUnmatchedIgnoredErrors: true
ignoreErrors:
# add cases to ignore because they are too much work for now
Expand Down
2 changes: 0 additions & 2 deletions script/fix
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ if test "$HAS_TYPE" = 1; then
$CONSOLE_PREFIX ./vendor/bin/phpcbf $EXTRA_ARGUMENTS $FILE_FILTER

elif test "$TYPE" = "rector"; then
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/rector process $EXTRA_ARGUMENTS $FILE_FILTER

else
Expand All @@ -93,7 +92,6 @@ else
echo -e "${T_BOLD}Running fixes on the changed files (not only git-diff) between the base and the head branch${T_RESET}\n"

echo "${T_INFO}Fixing refactors (rector)${T_RESET}"
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/rector process $FILE_FILTER || true

echo "${T_INFO}Fixing coding standards (phpcs)${T_RESET}"
Expand Down
2 changes: 0 additions & 2 deletions script/lint
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ if test "$HAS_TYPE" = 1; then
fi

elif test "$TYPE" = "rector"; then
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/rector process --dry-run $EXTRA_ARGUMENTS $FILE_FILTER

else
Expand Down Expand Up @@ -161,7 +160,6 @@ else
echo -e '\n'

echo "${T_INFO}Checking refactors (rector)${T_RESET}"
echo "${T_WARNING}Rector might not produce correct results until phpstan is on level 4 on the main branch${T_RESET}"
$CONSOLE_PREFIX ./vendor/bin/rector process --memory-limit=4g --dry-run $FILE_FILTER 2> /dev/null \
&& echo "${T_SUCCESS}Success${T_RESET}"

Expand Down
11 changes: 1 addition & 10 deletions src/CollectionDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
use alsvanzelf\jsonapi\objects\ResourceObject;
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
use alsvanzelf\jsonapi\objects\ResourceObject;

/**
* this document is a set of Resources
Expand Down Expand Up @@ -63,9 +63,6 @@ public function add($type, $id, array $attributes=[]) {
}
}

/**
* @inheritDoc
*/
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) {
if ($previousHref !== null) {
$this->addLink('prev', $previousHref);
Expand Down Expand Up @@ -115,9 +112,6 @@ public function addResource(ResourceInterface $resource, array $options=[]) {
* DocumentInterface
*/

/**
* @inheritDoc
*/
public function toArray() {
$array = parent::toArray();

Expand All @@ -133,9 +127,6 @@ public function toArray() {
* ResourceContainerInterface
*/

/**
* @inheritDoc
*/
public function getContainedResources() {
return $this->resources;
}
Expand Down
3 changes: 0 additions & 3 deletions src/DataDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ public function addIncludedResourceObject(ResourceObject ...$resourceObjects) {
* DocumentInterface
*/

/**
* @inheritDoc
*/
public function toArray() {
$array = parent::toArray();

Expand Down
16 changes: 5 additions & 11 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use alsvanzelf\jsonapi\helpers\Validator;
use alsvanzelf\jsonapi\interfaces\DocumentInterface;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
use alsvanzelf\jsonapi\interfaces\HasExtensionMembersInterface;
use alsvanzelf\jsonapi\interfaces\HasLinksInterface;
use alsvanzelf\jsonapi\interfaces\HasMetaInterface;
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
use alsvanzelf\jsonapi\objects\JsonapiObject;
use alsvanzelf\jsonapi\objects\LinkObject;
Expand All @@ -22,7 +25,7 @@
/**
* @see ResourceDocument, CollectionDocument, ErrorsDocument or MetaDocument
*/
abstract class Document implements DocumentInterface, \JsonSerializable {
abstract class Document implements DocumentInterface, \JsonSerializable, HasLinksInterface, HasMetaInterface, HasExtensionMembersInterface {
use AtMemberManager, ExtensionMemberManager, HttpStatusCodeManager, LinksManager {
LinksManager::addLink as linkManagerAddLink;
}
Expand All @@ -41,7 +44,7 @@ abstract class Document implements DocumentInterface, \JsonSerializable {

/** @var MetaObject */
protected $meta;
/** @var JsonapiObject */
/** @var ?JsonapiObject */
protected $jsonapi;
/** @var ExtensionInterface[] */
protected $extensions = [];
Expand Down Expand Up @@ -259,9 +262,6 @@ public function applyProfile(ProfileInterface $profile) {
* DocumentInterface
*/

/**
* @inheritDoc
*/
public function toArray() {
$array = [];

Expand All @@ -285,9 +285,6 @@ public function toArray() {
return $array;
}

/**
* @inheritDoc
*/
public function toJson(array $options=[]) {
$options = array_merge(self::$defaults, $options);

Expand All @@ -309,9 +306,6 @@ public function toJson(array $options=[]) {
return $json;
}

/**
* @inheritDoc
*/
public function sendResponse(array $options=[]) {
$options = array_merge(self::$defaults, $options);

Expand Down
3 changes: 0 additions & 3 deletions src/ErrorsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ public function addErrorObject(ErrorObject $errorObject) {
* DocumentInterface
*/

/**
* @inheritDoc
*/
public function toArray() {
$array = parent::toArray();

Expand Down
3 changes: 0 additions & 3 deletions src/MetaDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public function add($key, $value, $level=Document::LEVEL_ROOT) {
* DocumentInterface
*/

/**
* @inheritDoc
*/
public function toArray() {
$array = parent::toArray();

Expand Down
Loading