-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from timacdonald/relationship-helper
Just a bunch of mooooare stuff
- Loading branch information
Showing
26 changed files
with
950 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
parameters: | ||
level: 8 | ||
level: max | ||
paths: | ||
- src | ||
checkInternalClassCaseSensitivity: true | ||
checkTooWideReturnTypesInProtectedAndPublicMethods: true | ||
checkUninitializedProperties: true | ||
checkMissingIterableValueType: false | ||
checkMissingIterableValueType: true | ||
includes: | ||
- vendor-bin/linting/vendor/nunomaduro/larastan/extension.neon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor-bin/testing/vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory>tests/Unit</directory> | ||
</testsuite> | ||
<testsuite name="Feature"> | ||
</testsuite> | ||
<testsuite name="Feature"> | ||
<directory>tests/Feature</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<include><directory suffix=".php">src</directory></include> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</coverage> | ||
|
||
<php> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
<env name="DB_CONNECTION" value="sqlite" /> | ||
<env name="DB_DATABASE" value=":memory:" /> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TiMacDonald\JsonApi\Concerns; | ||
|
||
use Closure; | ||
use Illuminate\Support\Collection; | ||
use TiMacDonald\JsonApi\JsonApiResource; | ||
use TiMacDonald\JsonApi\JsonApiResourceCollection; | ||
use TiMacDonald\JsonApi\Support\UnknownRelationship; | ||
|
||
trait Caching | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
private ?string $idCache = null; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private ?string $typeCache = null; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private ?Collection $requestedRelationshipsCache = null; | ||
|
||
/** | ||
* @internal | ||
* @infection-ignore-all | ||
*/ | ||
public function flush(): void | ||
{ | ||
$this->idCache = null; | ||
|
||
$this->typeCache = null; | ||
|
||
if ($this->requestedRelationshipsCache !== null) { | ||
$this->requestedRelationshipsCache->each( | ||
/** | ||
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $resource | ||
*/ | ||
fn ($resource) => $resource->flush() | ||
); | ||
} | ||
|
||
$this->requestedRelationshipsCache = null; | ||
} | ||
|
||
/** | ||
* @internal | ||
* @infection-ignore-all | ||
*/ | ||
private function rememberId(Closure $closure): string | ||
{ | ||
return $this->idCache ??= $closure(); | ||
} | ||
|
||
|
||
/** | ||
* @internal | ||
* @infection-ignore-all | ||
*/ | ||
private function rememberType(Closure $closure): string | ||
{ | ||
return $this->typeCache ??= $closure(); | ||
} | ||
|
||
/** | ||
* @internal | ||
* @infection-ignore-all | ||
*/ | ||
private function rememberRequestRelationships(Closure $closure): Collection | ||
{ | ||
return $this->requestedRelationshipsCache ??= $closure(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function requestedRelationshipsCache(): ?Collection | ||
{ | ||
return $this->requestedRelationshipsCache; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TiMacDonald\JsonApi\Concerns; | ||
|
||
use Closure; | ||
use TiMacDonald\JsonApi\JsonApiServerImplementation as ServerImplementation; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait Implementation | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
private static ?Closure $serverImplementationResolver = null; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function resolveServerImplementationNormally(): void | ||
{ | ||
self::$serverImplementationResolver = null; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function serverImplementationResolver(): Closure | ||
{ | ||
return self::$serverImplementationResolver ?? fn (): ServerImplementation => new ServerImplementation('1.0'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TiMacDonald\JsonApi\Concerns; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Collection; | ||
use TiMacDonald\JsonApi\Link; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait Links | ||
{ | ||
/** | ||
* @internal | ||
* @return array<string, Link> | ||
*/ | ||
private function resolveLinks(Request $request): array | ||
{ | ||
return Collection::make($this->toLinks($request)) | ||
->mapWithKeys( | ||
/** | ||
* @param string|Link $value | ||
* @param string|int $key | ||
*/ | ||
fn ($value, $key): array => $value instanceof Link | ||
? [$value->key() => $value] | ||
: [$key => new Link($value)] | ||
) | ||
->all(); | ||
} | ||
} |
Oops, something went wrong.