-
Notifications
You must be signed in to change notification settings - Fork 8
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 #32 from square/mommy-daddy
Set parent relations
- Loading branch information
Showing
7 changed files
with
185 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Square\Pjson; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Allows linking a deserialized json item to its parent | ||
* There is no intention for this library to handle more complex backwards lookign paths than | ||
* this one. | ||
* Those more complex paths can easily be handled by methods that traverse the entire | ||
* data structure once it is in memory once the parent is available. | ||
* So encoding something like parent->property->parent->parent->property->property is left to be | ||
* implemented in client code | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class JsonParent extends Json | ||
{ | ||
/** | ||
* No constructor params are made available in this case as we do not allow customizing how we link | ||
* to the parent object. There is only one parent object available, that is the one we are linking to | ||
* and that's it. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->path = []; | ||
$this->omit_empty = false; | ||
} | ||
|
||
public function linksToParentObject(): bool | ||
{ | ||
return true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Square\Pjson\Tests\Definitions; | ||
|
||
use Square\Pjson\Json; | ||
use Square\Pjson\JsonParent; | ||
use Square\Pjson\JsonSerialize; | ||
|
||
class LinkChild | ||
{ | ||
use JsonSerialize; | ||
|
||
#[Json] | ||
public string $name; | ||
|
||
#[JsonParent] | ||
public LinkParent $parent; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Square\Pjson\Tests\Definitions; | ||
|
||
use Square\Pjson\Json; | ||
use Square\Pjson\JsonSerialize; | ||
|
||
class LinkParent | ||
{ | ||
use JsonSerialize; | ||
|
||
#[Json] | ||
public LinkChild $child; | ||
|
||
#[Json(type: LinkChild::class)] | ||
public array $children; | ||
|
||
#[Json] | ||
public string $name; | ||
} |
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