Skip to content

Pattern expander backtrace #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,43 @@ public function matcherFailed(string $name, $value, $pattern, string $error) : v
);
}

public function expanderEntrance(string $name, $value) : void
{
$this->trace[] = \sprintf(
'#%d Expander %s matching value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);
}

public function expanderSucceed(string $name, $value) : void
{
$this->trace[] = \sprintf(
'#%d Expander %s successfully matched value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);
}

public function expanderFailed(string $name, $value, string $error) : void
{
$this->trace[] = \sprintf(
'#%d Expander %s failed to match value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);

$this->trace[] = \sprintf(
'#%d Expander %s error: %s',
$this->entriesCount(),
$name,
new SingleLineString($error)
);
}

public function __toString() : string
{
return \implode("\n", $this->trace);
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/MatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function createMatcher(Backtrace $backtrace = null) : Matcher
{
$matcherBacktrace = $backtrace ? $backtrace : new Backtrace();

return new Matcher($this->buildMatchers($this->buildParser(), $matcherBacktrace), $matcherBacktrace);
return new Matcher($this->buildMatchers($this->buildParser($matcherBacktrace), $matcherBacktrace), $matcherBacktrace);
}

protected function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher\ChainMatcher
Expand Down Expand Up @@ -88,8 +88,8 @@ protected function buildScalarMatchers(Parser $parser, Backtrace $backtrace) : M
);
}

protected function buildParser() : Parser
protected function buildParser(Backtrace $backtrace) : Parser
{
return new Parser(new Lexer(), new Parser\ExpanderInitializer());
return new Parser(new Lexer(), new Parser\ExpanderInitializer($backtrace));
}
}
22 changes: 19 additions & 3 deletions src/Matcher/Pattern/Expander/After.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ final class After implements PatternExpander
{
const NAME = 'after';

private $boundary;
use BacktraceBehavior;

private $boundary;
private $error;


public function __construct($boundary)
{
if (false === \is_string($boundary)) {
Expand All @@ -37,24 +37,40 @@ public static function is(string $name) : bool

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (false === \is_string($value)) {
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if (!$this->is_datetime($value)) {
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$value = new \DateTime($value);

if ($value <= $this->boundary) {
$this->error = \sprintf('Value "%s" is not after "%s".', new StringConverter($value), new StringConverter($this->boundary));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

return $value > $this->boundary;
$result = $value > $this->boundary;

if ($result) {
$this->backtrace->expanderSucceed(self::NAME, $value);
} else {
$this->backtrace->expanderFailed(self::NAME, $value, '');
}

return $result;
}

private function is_datetime(string $value) : bool
Expand Down
20 changes: 20 additions & 0 deletions src/Matcher/Pattern/Expander/BacktraceBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Backtrace;

trait BacktraceBehavior
{
/**
* @var Backtrace
*/
protected $backtrace;

public function setBacktrace(Backtrace $backtrace) : void
{
$this->backtrace = $backtrace;
}
}
21 changes: 19 additions & 2 deletions src/Matcher/Pattern/Expander/Before.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ final class Before implements PatternExpander
{
const NAME = 'before';

private $boundary;
use BacktraceBehavior;

private $boundary;
private $error;


Expand All @@ -36,24 +37,40 @@ public static function is(string $name) : bool

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_string($value)) {
$this->error = \sprintf('Before expander require "string", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if (!$this->is_datetime($value)) {
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$value = new \DateTime($value);

if ($value >= $this->boundary) {
$this->error = \sprintf('Value "%s" is before "%s".', new StringConverter($value), new StringConverter($this->boundary));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

return $value < $this->boundary;
$result = $value < $this->boundary;

if ($result) {
$this->backtrace->expanderSucceed(self::NAME, $value);
} else {
$this->backtrace->expanderFailed(self::NAME, $value, '');
}

return $result;
}

private function is_datetime(string $value) : bool
Expand Down
12 changes: 10 additions & 2 deletions src/Matcher/Pattern/Expander/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ final class Contains implements PatternExpander
{
const NAME = 'contains';

private $error;
use BacktraceBehavior;

private $error;
private $string;

private $ignoreCase;

public static function is(string $name) : bool
Expand All @@ -30,8 +30,12 @@ public function __construct(string $string, $ignoreCase = false)

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_string($value)) {
$this->error = \sprintf('Contains expander require "string", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

Expand All @@ -41,9 +45,13 @@ public function match($value) : bool

if ($contains === false) {
$this->error = \sprintf("String \"%s\" doesn't contains \"%s\".", $value, $this->string);
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$this->backtrace->expanderSucceed(self::NAME, $value);

return true;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Matcher/Pattern/Expander/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ final class Count implements PatternExpander
{
const NAME = 'count';

private $error;
use BacktraceBehavior;

private $error;
private $value;

public static function is(string $name) : bool
Expand All @@ -27,16 +28,24 @@ public function __construct(int $value)

public function match($value) :bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_array($value)) {
$this->error = \sprintf('Count expander require "array", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if (\count($value) !== $this->value) {
$this->error = \sprintf('Expected count of %s is %s.', new StringConverter($value), new StringConverter($this->value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$this->backtrace->expanderSucceed(self::NAME, $value);

return true;
}
public function getError() : ?string
Expand Down
14 changes: 12 additions & 2 deletions src/Matcher/Pattern/Expander/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ final class EndsWith implements PatternExpander
{
const NAME = 'endsWith';

private $stringEnding;
use BacktraceBehavior;

private $stringEnding;
private $error;

private $ignoreCase;

public function __construct(string $stringEnding, bool $ignoreCase = false)
Expand All @@ -34,20 +34,30 @@ public static function is(string $name) : bool

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_string($value)) {
$this->error = \sprintf('EndsWith expander require "string", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if (empty($this->stringEnding)) {
$this->backtrace->expanderSucceed(self::NAME, $value);

return true;
}

if (!$this->matchValue($value)) {
$this->error = \sprintf("string \"%s\" doesn't ends with string \"%s\".", $value, $this->stringEnding);
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$this->backtrace->expanderSucceed(self::NAME, $value);

return true;
}

Expand Down
19 changes: 17 additions & 2 deletions src/Matcher/Pattern/Expander/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ final class GreaterThan implements PatternExpander
{
const NAME = 'greaterThan';

private $boundary;
use BacktraceBehavior;

private $boundary;
private $error;

public function __construct($boundary)
Expand All @@ -31,17 +32,31 @@ public static function is(string $name) : bool

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_float($value) && !\is_int($value) && !\is_numeric($value)) {
$this->error = \sprintf('Value "%s" is not a valid number.', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if ($value <= $this->boundary) {
$this->error = \sprintf('Value "%s" is not greater than "%s".', new StringConverter($value), new StringConverter($this->boundary));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

return $value > $this->boundary;
$result = $value > $this->boundary;

if ($result) {
$this->backtrace->expanderSucceed(self::NAME, $value);
} else {
$this->backtrace->expanderFailed(self::NAME, $value, '');
}

return $result;
}

public function getError() : ?string
Expand Down
11 changes: 10 additions & 1 deletion src/Matcher/Pattern/Expander/InArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ final class InArray implements PatternExpander
{
const NAME = 'inArray';

private $error;
use BacktraceBehavior;

private $error;
private $value;

public function __construct($value)
Expand All @@ -27,16 +28,24 @@ public static function is(string $name) : bool

public function match($value) : bool
{
$this->backtrace->expanderEntrance(self::NAME, $value);

if (!\is_array($value)) {
$this->error = \sprintf('InArray expander require "array", got "%s".', new StringConverter($value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

if (!\in_array($this->value, $value, true)) {
$this->error = \sprintf("%s doesn't have \"%s\" element.", new StringConverter($value), new StringConverter($this->value));
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);

return false;
}

$this->backtrace->expanderSucceed(self::NAME, $value);

return true;
}

Expand Down
Loading