generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14 from Morning-Train/hotfix/special-api-formatting
Add API formatting information to Resources
- Loading branch information
Showing
24 changed files
with
362 additions
and
217 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
36 changes: 36 additions & 0 deletions
36
src/Attributes/Resources/Properties/ApiFormatting/ResourceToArray.php
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,36 @@ | ||
<?php | ||
|
||
namespace Morningtrain\Economic\Attributes\Resources\Properties\ApiFormatting; | ||
|
||
use Attribute; | ||
use Morningtrain\Economic\Abstracts\Resource; | ||
use Morningtrain\Economic\Interfaces\ApiFormatter; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] class ResourceToArray implements ApiFormatter | ||
{ | ||
protected array $properties; | ||
|
||
public function __construct(...$properties) | ||
{ | ||
$this->properties = $properties; | ||
} | ||
|
||
public function format($value): array | ||
{ | ||
if (! is_a($value, Resource::class)) { | ||
throw new \InvalidArgumentException('ResourceToArray can only be used on properties of type Resource'); | ||
} | ||
|
||
$resourceAsArray = $value->toArray(); | ||
|
||
$returnArray = []; | ||
|
||
foreach ($this->properties as $key) { | ||
if (isset($resourceAsArray[$key])) { | ||
$returnArray[$key] = $resourceAsArray[$key]; | ||
} | ||
} | ||
|
||
return $returnArray; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Attributes/Resources/Properties/ApiFormatting/ResourceToPrimaryKey.php
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,23 @@ | ||
<?php | ||
|
||
namespace Morningtrain\Economic\Attributes\Resources\Properties\ApiFormatting; | ||
|
||
use Attribute; | ||
use Morningtrain\Economic\Abstracts\Resource; | ||
use Morningtrain\Economic\Interfaces\ApiFormatter; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] class ResourceToPrimaryKey implements ApiFormatter | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function format($value): mixed | ||
{ | ||
if (! is_a($value, Resource::class)) { | ||
throw new \InvalidArgumentException('ResourceToArray can only be used on properties of type Resource'); | ||
} | ||
|
||
return $value->getPrimaryKey(); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Morningtrain\Economic\Interfaces; | ||
|
||
interface ApiFormatter | ||
{ | ||
public function format($value): mixed; | ||
} |
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
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
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
Oops, something went wrong.