forked from biig-io/Melodiia
-
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 biig-io#32 from swagindustries/feature/serializati…
…on-context Add context builder
- Loading branch information
Showing
8 changed files
with
158 additions
and
4 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
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwagIndustries\Melodiia\Serialization\Context; | ||
|
||
use SwagIndustries\Melodiia\Response\ApiResponse; | ||
|
||
final class ContextBuilderChain implements ContextBuilderChainInterface | ||
{ | ||
/** @var ContextBuilderInterface[] */ | ||
private $builders; | ||
|
||
public function __construct(iterable $builders) | ||
{ | ||
$this->builders = $builders; | ||
} | ||
|
||
public function buildContext(array $context, ApiResponse $response): array | ||
{ | ||
foreach ($this->builders as $builder) { | ||
if ($builder->supports($response)) { | ||
$context = $builder->buildContext($context, $response); | ||
} | ||
} | ||
|
||
return $context; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Serialization/Context/ContextBuilderChainInterface.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwagIndustries\Melodiia\Serialization\Context; | ||
|
||
use SwagIndustries\Melodiia\Response\ApiResponse; | ||
|
||
interface ContextBuilderChainInterface | ||
{ | ||
public function buildContext(array $context, ApiResponse $response): array; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwagIndustries\Melodiia\Serialization\Context; | ||
|
||
use SwagIndustries\Melodiia\Response\ApiResponse; | ||
|
||
/** | ||
* Build a part of the serialization context. | ||
*/ | ||
interface ContextBuilderInterface extends ContextBuilderChainInterface | ||
{ | ||
/** | ||
* Return true if the buildContext method can be called. | ||
*/ | ||
public function supports(ApiResponse $response): bool; | ||
} |
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
63 changes: 63 additions & 0 deletions
63
tests/Melodiia/Serialization/Context/ContextBuilderFactoryTest.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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Melodiia\Serialization\Context; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SwagIndustries\Melodiia\Response\ApiResponse; | ||
use SwagIndustries\Melodiia\Serialization\Context\ContextBuilderChain; | ||
use SwagIndustries\Melodiia\Serialization\Context\ContextBuilderInterface; | ||
|
||
class ContextBuilderFactoryTest extends TestCase | ||
{ | ||
public function testItBuildContextUsingGivenBuilders() | ||
{ | ||
$builder1 = new class() implements ContextBuilderInterface { | ||
public function buildContext(array $context, ApiResponse $response): array | ||
{ | ||
$context['foo'] = true; | ||
|
||
return $context; | ||
} | ||
|
||
public function supports(ApiResponse $response): bool | ||
{ | ||
return true; | ||
} | ||
}; | ||
$builder2 = new class() implements ContextBuilderInterface { | ||
public function buildContext(array $context, ApiResponse $response): array | ||
{ | ||
$context['baz'] = true; | ||
|
||
return $context; | ||
} | ||
|
||
public function supports(ApiResponse $response): bool | ||
{ | ||
return false; | ||
} | ||
}; | ||
$builder3 = new class() implements ContextBuilderInterface { | ||
public function buildContext(array $context, ApiResponse $response): array | ||
{ | ||
$context['bar'] = true; | ||
|
||
return $context; | ||
} | ||
|
||
public function supports(ApiResponse $response): bool | ||
{ | ||
return true; | ||
} | ||
}; | ||
|
||
$chain = new ContextBuilderChain([$builder1, $builder2, $builder3]); | ||
|
||
$context = $chain->buildContext([], $this->prophesize(ApiResponse::class)->reveal()); | ||
$this->assertTrue($context['foo']); | ||
$this->assertTrue($context['bar']); | ||
$this->assertFalse(isset($context['baz'])); | ||
} | ||
} |