-
Notifications
You must be signed in to change notification settings - Fork 91
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 #436 from tienvx/move-code-from-matchers-to-format…
…ters refactor: Move code from matchers to formatters
- Loading branch information
Showing
37 changed files
with
394 additions
and
160 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/PhpPact/Consumer/Matcher/Formatters/MinimalFormatter.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,20 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Model\FormatterInterface; | ||
use PhpPact\Consumer\Matcher\Model\GeneratorInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class MinimalFormatter implements FormatterInterface | ||
{ | ||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array | ||
{ | ||
return [ | ||
'pact:matcher:type' => $matcher->getType(), | ||
] + $matcher->getAttributes()->getData(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatter.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,26 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Model\FormatterInterface; | ||
use PhpPact\Consumer\Matcher\Model\GeneratorInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class ValueOptionalFormatter implements FormatterInterface | ||
{ | ||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array | ||
{ | ||
$data = [ | ||
'pact:matcher:type' => $matcher->getType(), | ||
]; | ||
|
||
if ($generator) { | ||
return $data + ['pact:generator:type' => $generator->getType()] + $matcher->getAttributes()->merge($generator->getAttributes())->getData(); | ||
} | ||
|
||
return $data + $matcher->getAttributes()->getData() + ['value' => $value]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatter.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,17 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Model\GeneratorInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class ValueRequiredFormatter extends ValueOptionalFormatter | ||
{ | ||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array | ||
{ | ||
return parent::format($matcher, $generator, $value) + ['value' => $value]; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Formatters\ValueOptionalFormatter; | ||
use PhpPact\Consumer\Matcher\Model\Attributes; | ||
use PhpPact\Consumer\Matcher\Model\FormatterAwareInterface; | ||
use PhpPact\Consumer\Matcher\Model\FormatterInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
abstract class AbstractMatcher implements MatcherInterface, FormatterAwareInterface | ||
{ | ||
private FormatterInterface $formatter; | ||
|
||
public function __construct() | ||
{ | ||
$this->formatter = new ValueOptionalFormatter(); | ||
} | ||
|
||
public function setFormatter(FormatterInterface $formatter): void | ||
{ | ||
$this->formatter = $formatter; | ||
} | ||
|
||
public function getFormatter(): FormatterInterface | ||
{ | ||
return $this->formatter; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return $this->getFormatter()->format($this, null, $this->getValue()); | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return new Attributes($this, $this->getAttributesData()); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
abstract protected function getAttributesData(): array; | ||
|
||
abstract protected function getValue(): 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
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.