-
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 #599 from tienvx/formatter-factory-method
Formatter factory method
- Loading branch information
Showing
135 changed files
with
2,748 additions
and
594 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
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
24 changes: 0 additions & 24 deletions
24
src/PhpPact/Consumer/Matcher/Formatters/CombinedMatchersFormatter.php
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/PhpPact/Consumer/Matcher/Formatters/Expression/AbstractExpressionFormatter.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,31 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidValueException; | ||
use PhpPact\Consumer\Matcher\Exception\MatcherNotSupportedException; | ||
use PhpPact\Consumer\Matcher\Model\ExpressionFormatterInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
abstract class AbstractExpressionFormatter implements ExpressionFormatterInterface | ||
{ | ||
protected function normalize(mixed $value): string | ||
{ | ||
if (is_string($value) && str_contains($value, "'")) { | ||
throw new InvalidValueException(sprintf('String value "%s" should not contains single quote', $value)); | ||
} | ||
return match (gettype($value)) { | ||
'string' => sprintf("'%s'", $value), | ||
'boolean' => $value ? 'true' : 'false', | ||
'integer' => (string) $value, | ||
'double' => (string) $value, | ||
'NULL' => 'null', | ||
default => throw new InvalidValueException(sprintf("Expression doesn't support value of type %s", gettype($value))), | ||
}; | ||
} | ||
|
||
protected function getMatcherNotSupportedException(MatcherInterface $matcher): MatcherNotSupportedException | ||
{ | ||
return new MatcherNotSupportedException(sprintf('Matcher %s is not supported by %s', $matcher->getType(), static::class)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/PhpPact/Consumer/Matcher/Formatters/Expression/BooleanFormatter.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 PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidValueException; | ||
use PhpPact\Consumer\Matcher\Matchers\Boolean; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class BooleanFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof Boolean) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
$value = $matcher->getValue(); | ||
if (!is_bool($value)) { | ||
throw new InvalidValueException(sprintf("Boolean formatter doesn't support value of type %s", gettype($value))); | ||
} | ||
|
||
return sprintf('matching(boolean, %s)', $this->normalize($value)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatter.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,18 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Matchers\ContentType; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class ContentTypeFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof ContentType) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
|
||
return sprintf("matching(contentType, %s, %s)", $this->normalize($matcher->getContentType()), $this->normalize($matcher->getValue())); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatter.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 PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidValueException; | ||
use PhpPact\Consumer\Matcher\Matchers\AbstractDateTime; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class DateTimeFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof AbstractDateTime) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
$value = $matcher->getValue(); | ||
if (!is_string($value)) { | ||
throw new InvalidValueException(sprintf("DateTime formatter doesn't support value of type %s", gettype($value))); | ||
} | ||
|
||
return sprintf("matching(%s, %s, %s)", $matcher->getType(), $this->normalize($matcher->getFormat()), $this->normalize($value)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/PhpPact/Consumer/Matcher/Formatters/Expression/DecimalFormatter.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 PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidValueException; | ||
use PhpPact\Consumer\Matcher\Matchers\Decimal; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class DecimalFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof Decimal) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
$value = $matcher->getValue(); | ||
if (!is_float($value)) { | ||
throw new InvalidValueException(sprintf("Decimal formatter doesn't support value of type %s", gettype($value))); | ||
} | ||
|
||
return sprintf('matching(decimal, %s)', $this->normalize($value)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatter.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,24 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\MatchingExpressionException; | ||
use PhpPact\Consumer\Matcher\Matchers\EachKey; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class EachKeyFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof EachKey) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
$rules = $matcher->getRules(); | ||
if (count($rules) !== 1) { | ||
throw new MatchingExpressionException(sprintf("Matcher 'eachKey' only support 1 rule in expression, %d provided", count($rules))); | ||
} | ||
$rule = reset($rules); | ||
|
||
return sprintf('eachKey(%s)', $rule->createExpressionFormatter()->format($rule)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatter.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,24 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\MatchingExpressionException; | ||
use PhpPact\Consumer\Matcher\Matchers\EachValue; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class EachValueFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof EachValue) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
$rules = $matcher->getRules(); | ||
if (count($rules) !== 1) { | ||
throw new MatchingExpressionException(sprintf("Matcher 'eachValue' only support 1 rule in expression, %d provided", count($rules))); | ||
} | ||
$rule = reset($rules); | ||
|
||
return sprintf('eachValue(%s)', $rule->createExpressionFormatter()->format($rule)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatter.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,18 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters\Expression; | ||
|
||
use PhpPact\Consumer\Matcher\Matchers\Equality; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class EqualityFormatter extends AbstractExpressionFormatter | ||
{ | ||
public function format(MatcherInterface $matcher): string | ||
{ | ||
if (!$matcher instanceof Equality) { | ||
throw $this->getMatcherNotSupportedException($matcher); | ||
} | ||
|
||
return sprintf('matching(equalTo, %s)', $this->normalize($matcher->getValue())); | ||
} | ||
} |
Oops, something went wrong.