Skip to content
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

refactor: Move xml to core #434

Merged
merged 1 commit into from
Dec 27, 2023
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
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": ">=8.5.23 <10",
"guzzlehttp/guzzle": "^7.8",
"tienvx/pact-php-xml": "^0.1",
"behat/behat": "^3.13",
"galbar/jsonpath": "^3.0",
"ramsey/uuid": "^4.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace XmlConsumer\Tests\Service;

use Tienvx\PactPhpXml\XmlBuilder;
use PhpPact\Xml\XmlBuilder;
use XmlConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\ConsumerRequest;
Expand Down
54 changes: 54 additions & 0 deletions src/PhpPact/Xml/Model/Builder/ElementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PhpPact\Xml\Model\Builder;

use PhpPact\Xml\Model\Matcher\Matcher;
use PhpPact\Xml\XmlElement;
use PhpPact\Xml\XmlText;

trait ElementTrait
{
private XmlElement $root;

public function root(callable ...$options): void
{
$this->root = new XmlElement(...$options);
}

public function add(callable ...$options): callable
{
return fn (XmlElement $element) => $element->addChild(new XmlElement(...$options));
}

public function name(string $name): callable
{
return fn (XmlElement $element) => $element->setName($name);
}

public function text(callable ...$options): callable
{
return fn (XmlElement $element) => $element->setText(new XmlText(...$options));
}

public function attribute(string $name, mixed $value): callable
{
return fn (XmlElement $element) => $element->addAttribute($name, $value);
}

public function eachLike(int $min = 1, ?int $max = null, int $examples = 1): callable
{
return function (XmlElement $element) use ($min, $max, $examples): void {
$options = [
'min' => $min,
'examples' => $examples,
];
if (isset($max)) {
$options['max'] = $max;
}
$element->setMatcher(new Matcher(
fn (Matcher $matcher) => $matcher->setType('type'),
fn (Matcher $matcher) => $matcher->setOptions($options),
));
};
}
}
21 changes: 21 additions & 0 deletions src/PhpPact/Xml/Model/Builder/GeneratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace PhpPact\Xml\Model\Builder;

use PhpPact\Xml\Model\Matcher\Generator;

trait GeneratorTrait
{
public function generatorType(string $type): callable
{
return fn (Generator $generator) => $generator->setType($type);
}

/**
* @param array<string, mixed> $options
*/
public function generatorOptions(array $options): callable
{
return fn (Generator $generator) => $generator->setOptions($options);
}
}
21 changes: 21 additions & 0 deletions src/PhpPact/Xml/Model/Builder/MatcherTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace PhpPact\Xml\Model\Builder;

use PhpPact\Xml\Model\Matcher\Matcher;

trait MatcherTrait
{
public function matcherType(string $type): callable
{
return fn (Matcher $matcher) => $matcher->setType($type);
}

/**
* @param array<string, mixed> $options
*/
public function matcherOptions(array $options): callable
{
return fn (Matcher $matcher) => $matcher->setOptions($options);
}
}
35 changes: 35 additions & 0 deletions src/PhpPact/Xml/Model/Builder/TextTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace PhpPact\Xml\Model\Builder;

use PhpPact\Xml\Model\Matcher\Generator;
use PhpPact\Xml\Model\Matcher\Matcher;
use PhpPact\Xml\XmlText;

trait TextTrait
{
public function content(string|int|float $content): callable
{
return fn (XmlText $text) => $text->setContent($content);
}

public function matcher(callable ...$options): callable
{
return fn (XmlText $text) => $text->setMatcher(new Matcher(...$options));
}

public function generator(callable ...$options): callable
{
return fn (XmlText $text) => $text->setGenerator(new Generator(...$options));
}

public function contentLike(string|int|float $content): callable
{
return function (XmlText $text) use ($content): void {
$text->setContent($content);
$text->setMatcher(new Matcher(
fn (Matcher $matcher) => $matcher->setType('type')
));
};
}
}
43 changes: 43 additions & 0 deletions src/PhpPact/Xml/Model/Matcher/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpPact\Xml\Model\Matcher;

class Generator
{
private string $type;

/**
* @var array<string, mixed>
*/
protected array $options = [];

public function __construct(callable ...$options)
{
array_walk($options, fn (callable $option) => $option($this));
}

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): self
{
$this->options = $options;

return $this;
}

/**
* @return array<string, mixed>
*/
public function getArray(): array
{
return ['pact:generator:type' => $this->type] + $this->options;
}
}
43 changes: 43 additions & 0 deletions src/PhpPact/Xml/Model/Matcher/Matcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpPact\Xml\Model\Matcher;

class Matcher
{
private string $type;

/**
* @var array<string, mixed>
*/
protected array $options = [];

public function __construct(callable ...$options)
{
array_walk($options, fn (callable $option) => $option($this));
}

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): self
{
$this->options = $options;

return $this;
}

/**
* @return array<string, mixed>
*/
public function getArray(): array
{
return ['pact:matcher:type' => $this->type] + $this->options;
}
}
32 changes: 32 additions & 0 deletions src/PhpPact/Xml/XmlBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpPact\Xml;

use PhpPact\Xml\Model\Builder\ElementTrait;
use PhpPact\Xml\Model\Builder\GeneratorTrait;
use PhpPact\Xml\Model\Builder\MatcherTrait;
use PhpPact\Xml\Model\Builder\TextTrait;

class XmlBuilder
{
use ElementTrait;
use TextTrait;
use MatcherTrait;
use GeneratorTrait;

public function __construct(private string $version, private string $charset)
{
}

/**
* @return array<string, mixed>
*/
public function getArray(): array
{
return [
'version' => $this->version,
'charset' => $this->charset,
'root' => $this->root->getArray(),
];
}
}
113 changes: 113 additions & 0 deletions src/PhpPact/Xml/XmlElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace PhpPact\Xml;

use PhpPact\Xml\Model\Matcher\Generator;
use PhpPact\Xml\Model\Matcher\Matcher;

class XmlElement
{
private string $name;

/**
* @var XmlElement[]
*/
private array $children = [];

/**
* @var array<string, mixed>
*/
private array $attributes = [];

private ?XmlText $text = null;

private ?Matcher $matcher = null;

private ?Generator $generator = null;

public function __construct(callable ...$options)
{
array_walk($options, fn (callable $option) => $option($this));
}

public function setName(string $name): self
{
$this->name = preg_replace('/(^[0-9]+|[^a-zA-Z0-9\-\_\:]+)/', '', $name);

return $this;
}

public function addChild(self $child): self
{
$this->children[] = $child;

return $this;
}

public function setText(?XmlText $text): self
{
$this->text = $text;

return $this;
}

public function setMatcher(?Matcher $matcher): self
{
$this->matcher = $matcher;

return $this;
}

public function setGenerator(?Generator $generator): self
{
$this->generator = $generator;

return $this;
}

public function addAttribute(string $name, mixed $value): self
{
$this->attributes[$name] = $value;

return $this;
}

/**
* @return array<string, mixed>
*/
public function getArray(): array
{
if ($this->matcher) {
$result = [
'value' => $this->getBaseArray(),
];
$result += $this->matcher->getArray();

if ($this->generator) {
$result += $this->generator->getArray();
}
} else {
$result = $this->getBaseArray();
}

return $result;
}

/**
* @return array<string, mixed>
*/
private function getBaseArray(): array
{
$result = [
'name' => $this->name,
'children' => array_map(fn (XmlElement $element) => $element->getArray(), $this->children),
'attributes' => $this->attributes
];

if ($this->text) {
$result['children'][] = $this->text->getArray();
}

return $result;
}
}
Loading