-
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 #448 from tienvx/plugin-formatter
refactor: Add PluginFormatter
- Loading branch information
Showing
44 changed files
with
572 additions
and
148 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
7 changes: 7 additions & 0 deletions
7
src/PhpPact/Consumer/Matcher/Exception/MatchingExpressionException.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,7 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Exception; | ||
|
||
class MatchingExpressionException extends MatcherException | ||
{ | ||
} |
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
104 changes: 104 additions & 0 deletions
104
src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.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,104 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\GeneratorNotRequiredException; | ||
use PhpPact\Consumer\Matcher\Exception\MatcherNotSupportedException; | ||
use PhpPact\Consumer\Matcher\Exception\MatchingExpressionException; | ||
use PhpPact\Consumer\Matcher\Matchers\AbstractDateTime; | ||
use PhpPact\Consumer\Matcher\Matchers\ContentType; | ||
use PhpPact\Consumer\Matcher\Matchers\EachKey; | ||
use PhpPact\Consumer\Matcher\Matchers\EachValue; | ||
use PhpPact\Consumer\Matcher\Matchers\MatchingField; | ||
use PhpPact\Consumer\Matcher\Matchers\NotEmpty; | ||
use PhpPact\Consumer\Matcher\Matchers\NullValue; | ||
use PhpPact\Consumer\Matcher\Matchers\Regex; | ||
use PhpPact\Consumer\Matcher\Matchers\Type; | ||
use PhpPact\Consumer\Matcher\Model\FormatterInterface; | ||
use PhpPact\Consumer\Matcher\Model\GeneratorAwareInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class PluginFormatter implements FormatterInterface | ||
{ | ||
public const MATCHERS_WITHOUT_CONFIG = ['equality', 'type', 'number', 'integer', 'decimal', 'include', 'boolean', 'semver']; | ||
|
||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if ($matcher instanceof GeneratorAwareInterface && null !== $matcher->getGenerator()) { | ||
throw new GeneratorNotRequiredException('Generator is not support in plugin'); | ||
} | ||
|
||
if ($matcher instanceof MatchingField) { | ||
return $this->formatMatchingFieldMatcher($matcher); | ||
} | ||
if ($matcher instanceof NotEmpty) { | ||
return $this->formatNotEmptyMatcher($matcher); | ||
} | ||
if ($matcher instanceof EachKey || $matcher instanceof EachValue) { | ||
return $this->formatEachKeyAndEachValueMatchers($matcher); | ||
} | ||
if ($matcher instanceof NullValue) { | ||
return $this->formatMatchersWithoutConfig(new Type(null)); | ||
} | ||
|
||
if (in_array($matcher->getType(), self::MATCHERS_WITHOUT_CONFIG)) { | ||
return $this->formatMatchersWithoutConfig($matcher); | ||
} | ||
if ($matcher instanceof AbstractDateTime || $matcher instanceof Regex || $matcher instanceof ContentType) { | ||
return $this->formatMatchersWithConfig($matcher); | ||
} | ||
|
||
throw new MatcherNotSupportedException(sprintf("Matcher '%s' is not supported by plugin", $matcher->getType())); | ||
} | ||
|
||
private function formatMatchingFieldMatcher(MatchingField $matcher): string | ||
{ | ||
return sprintf("matching($%s)", $this->normalize($matcher->getFieldName())); | ||
} | ||
|
||
private function formatMatchersWithoutConfig(MatcherInterface $matcher): string | ||
{ | ||
$type = $matcher->getType() === 'equality' ? 'equalTo' : $matcher->getType(); | ||
|
||
return sprintf('matching(%s, %s)', $type, $this->normalize($matcher->getValue())); | ||
} | ||
|
||
private function formatMatchersWithConfig(AbstractDateTime|Regex|ContentType $matcher): string | ||
{ | ||
$config = match (true) { | ||
$matcher instanceof AbstractDateTime => $matcher->getFormat(), | ||
$matcher instanceof Regex => $matcher->getRegex(), | ||
$matcher instanceof ContentType => $matcher->getValue(), | ||
}; | ||
|
||
return sprintf("matching(%s, %s, %s)", $matcher->getType(), $this->normalize($config), $this->normalize($matcher->getValue())); | ||
} | ||
|
||
private function formatNotEmptyMatcher(NotEmpty $matcher): string | ||
{ | ||
return sprintf('notEmpty(%s)', $this->normalize($matcher->getValue())); | ||
} | ||
|
||
private function formatEachKeyAndEachValueMatchers(EachKey|EachValue $matcher): string | ||
{ | ||
$rules = $matcher->getRules(); | ||
if (count($rules) === 0 || count($rules) > 1) { | ||
throw new MatchingExpressionException(sprintf("Matcher '%s' only support 1 rule, %d provided", $matcher->getType(), count($rules))); | ||
} | ||
$rule = reset($rules); | ||
|
||
return sprintf('%s(%s)', $matcher->getType(), $this->format($rule)); | ||
} | ||
|
||
private function normalize(mixed $value): string | ||
{ | ||
return match (gettype($value)) { | ||
'string' => sprintf("'%s'", str_replace("'", "\\'", $value)), | ||
'boolean' => $value ? 'true' : 'false', | ||
'integer' => (string) $value, | ||
'double' => (string) $value, | ||
'NULL' => 'null', | ||
default => throw new MatchingExpressionException(sprintf("Plugin formatter doesn't support value of type %s", gettype($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
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.