Skip to content

Commit

Permalink
Merge pull request #3 from Chemaclass/refactor-inversion-dependency
Browse files Browse the repository at this point in the history
Refactor: apply dependency inversion
  • Loading branch information
Chemaclass committed May 29, 2020
2 parents ac93306 + e85b204 commit 13ac766
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 78 deletions.
2 changes: 1 addition & 1 deletion example.php
Expand Up @@ -61,7 +61,7 @@
UNZ+2+8'
EDI;

$transactionResult = (new EdifactParser())->parse($fileContent);
$transactionResult = EdifactParser::create()->parse($fileContent);

foreach ($transactionResult->messages() as $i => $message) {
print "Message number: {$i}" . PHP_EOL;
Expand Down
23 changes: 17 additions & 6 deletions src/EdifactParser.php
Expand Up @@ -6,16 +6,27 @@

use EDI\Parser;
use EdifactParser\Exception\InvalidFile;
use EdifactParser\Segments\CustomSegmentFactoryInterface;
use EdifactParser\Segments\SegmentFactory;
use EdifactParser\Segments\SegmentFactoryInterface;

/** @psalmphp-immutable */
final class EdifactParser
{
/** @var null|CustomSegmentFactoryInterface */
private ?CustomSegmentFactoryInterface $customSegmentsFactory;
private SegmentFactoryInterface $segmentFactory;

public function __construct(?CustomSegmentFactoryInterface $customSegmentsFactory = null)
public static function create(?SegmentFactoryInterface $segmentFactory = null): self
{
$this->customSegmentsFactory = $customSegmentsFactory;
return new self($segmentFactory ?? new SegmentFactory());
}

private function __construct(SegmentFactoryInterface $segmentFactory)
{
$this->segmentFactory = $segmentFactory;
}

public function __invoke(string $fileContent): TransactionResult
{
return $this->parse($fileContent);
}

public function parse(string $fileContent): TransactionResult
Expand All @@ -27,7 +38,7 @@ public function parse(string $fileContent): TransactionResult
throw InvalidFile::withErrors($errors);
}

$segmentedValues = SegmentedValues::fromRaw($parser->get(), $this->customSegmentsFactory);
$segmentedValues = SegmentedValues::factory($this->segmentFactory)->fromRaw($parser->get());

return TransactionResult::fromSegmentedValues($segmentedValues);
}
Expand Down
1 change: 1 addition & 0 deletions src/Exception/InvalidFile.php
Expand Up @@ -7,6 +7,7 @@
use Exception;
use function json_encode;

/** @psalmphp-immutable */
final class InvalidFile extends Exception
{
public static function withErrors(array $errors): self
Expand Down
37 changes: 17 additions & 20 deletions src/SegmentedValues.php
Expand Up @@ -4,43 +4,40 @@

namespace EdifactParser;

use EdifactParser\Segments\CustomSegmentFactoryInterface;
use EdifactParser\Segments\SegmentFactory;
use EdifactParser\Segments\SegmentFactoryInterface;
use EdifactParser\Segments\SegmentInterface;

/** @psalmphp-immutable */
final class SegmentedValues
{
/** @psalm-var list<SegmentInterface> */
private array $list;
private array $list = [];

public static function fromRaw(
array $rawArrays,
?CustomSegmentFactoryInterface $customSegmentsFactory = null
): self {
$factory = new SegmentFactory($customSegmentsFactory);
private SegmentFactoryInterface $segmentFactory;

$self = new self();
$segments = [];
public static function factory(?SegmentFactoryInterface $segmentFactory = null): self
{
return new self($segmentFactory ?? new SegmentFactory());
}

foreach ($rawArrays as $rawArray) {
$segments[] = $factory->segmentFromArray($rawArray);
}
public function __construct(SegmentFactoryInterface $segmentFactory)
{
$this->segmentFactory = $segmentFactory;
}

foreach ($segments as $segment) {
$self->addSegment($segment);
public function fromRaw(array $rawArrays): self
{
foreach ($rawArrays as $rawArray) {
$this->list[] = $this->segmentFactory->segmentFromArray($rawArray);
}

return $self;
return $this;
}

/** @return SegmentInterface[] */
public function list(): array
{
return $this->list;
}

private function addSegment(SegmentInterface $segment): void
{
$this->list[] = $segment;
}
}
1 change: 1 addition & 0 deletions src/Segments/BGMBeginningOfMessage.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class BGMBeginningOfMessage implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/CNTControl.php
Expand Up @@ -6,6 +6,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class CNTControl implements SegmentInterface
{
private array $rawValues;
Expand Down
10 changes: 0 additions & 10 deletions src/Segments/CustomSegmentFactoryInterface.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Segments/DTMDateTimePeriod.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class DTMDateTimePeriod implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/MEADimensions.php
Expand Up @@ -6,6 +6,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class MEADimensions implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/NADNameAddress.php
Expand Up @@ -6,6 +6,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class NADNameAddress implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/PCIPackageId.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class PCIPackageId implements SegmentInterface
{
private array $rawValues;
Expand Down
29 changes: 2 additions & 27 deletions src/Segments/SegmentFactory.php
Expand Up @@ -4,23 +4,11 @@

namespace EdifactParser\Segments;

final class SegmentFactory
/** @psalmphp-immutable */
final class SegmentFactory implements SegmentFactoryInterface
{
private ?CustomSegmentFactoryInterface $customSegmentsFactory;

public function __construct(?CustomSegmentFactoryInterface $customSegmentsFactory)
{
$this->customSegmentsFactory = $customSegmentsFactory;
}

public function segmentFromArray(array $rawArray): SegmentInterface
{
$customSegment = $this->customSegment($rawArray);

if ($customSegment) {
return $customSegment;
}

$name = $rawArray[0];

switch ($name) {
Expand All @@ -44,17 +32,4 @@ public function segmentFromArray(array $rawArray): SegmentInterface

return new UnknownSegment($rawArray);
}

private function customSegment(array $rawArray): ?SegmentInterface
{
if ($this->customSegmentsFactory) {
$segment = $this->customSegmentsFactory->segmentFromArray($rawArray);

if ($segment) {
return $segment;
}
}

return null;
}
}
10 changes: 10 additions & 0 deletions src/Segments/SegmentFactoryInterface.php
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace EdifactParser\Segments;

interface SegmentFactoryInterface
{
public function segmentFromArray(array $rawArray): SegmentInterface;
}
1 change: 1 addition & 0 deletions src/Segments/UNHMessageHeader.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class UNHMessageHeader implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/UNTMessageFooter.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class UNTMessageFooter implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/Segments/UnknownSegment.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Segments;

/** @psalmphp-immutable */
final class UnknownSegment implements SegmentInterface
{
private array $rawValues;
Expand Down
1 change: 1 addition & 0 deletions src/TransactionMessage.php
Expand Up @@ -6,6 +6,7 @@

use EdifactParser\Segments\SegmentInterface;

/** @psalmphp-immutable */
final class TransactionMessage
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/TransactionResult.php
Expand Up @@ -8,6 +8,8 @@

/**
* A transactionResult is a list of transactionMessages.
*
* @psalmphp-immutable
*/
final class TransactionResult
{
Expand Down
15 changes: 9 additions & 6 deletions tests/Functional/EdifactParserTest.php
Expand Up @@ -21,7 +21,7 @@ public function invalidFileDueToANonPrintableChar(): void
\xE2\x80\xAF
EDI;
$this->expectException(InvalidFile::class);
(new EdifactParser())->parse($fileContent);
EdifactParser::create()->parse($fileContent);
}

/** @test */
Expand All @@ -37,7 +37,7 @@ public function parseMoreThanOneMessage(): void
UNT+19+3'
UNZ+3+4'
EDI;
$transactionResult = (new EdifactParser())->parse($fileContent);
$transactionResult = EdifactParser::create()->parse($fileContent);
self::assertCount(3, $transactionResult->messages());
}

Expand All @@ -53,8 +53,7 @@ public function extractValuesFromMessage(): void
UNZ+1+3'
EDI;

$parser = new EdifactParser();
$transactionResult = $parser->parse($fileContent);
$transactionResult = EdifactParser::create()->parse($fileContent);

self::assertCount(1, $transactionResult->messages());
$firstMessage = $transactionResult->messages()[0];
Expand Down Expand Up @@ -88,8 +87,8 @@ public function useACustomSegmentFactory(): void
UNT+19+1'
UNZ+1+3'
EDI;
$parser = new EdifactParser(new TestingCustomSegmentFactory('CUSTOM'));
$transactionResult = $parser->parse($fileContent);
$parser = EdifactParser::create(new TestingSegmentFactory('CUSTOM'));
$transactionResult = $parser($fileContent);

self::assertCount(1, $transactionResult->messages());
$firstMessage = $transactionResult->messages()[0];
Expand All @@ -98,5 +97,9 @@ public function useACustomSegmentFactory(): void
/** @var SegmentInterface $custom */
$custom = $segments['CUSTOM']['anyKey'];
self::assertEquals(['CUSTOM', 'anyKey', ['whatever', 'value', '9']], $custom->rawValues());

/** @var CNTControl $cnt11 */
$cnt11 = $segments[CNTControl::class]['11'];
self::assertEquals(['CNT', ['11', '1', 'PCE']], $cnt11->rawValues());
}
}
Expand Up @@ -4,22 +4,26 @@

namespace EdifactParser\Tests\Functional;

use EdifactParser\Segments\CustomSegmentFactoryInterface;
use EdifactParser\Segments\SegmentFactory;
use EdifactParser\Segments\SegmentFactoryInterface;
use EdifactParser\Segments\SegmentInterface;

final class TestingCustomSegmentFactory implements CustomSegmentFactoryInterface
final class TestingSegmentFactory implements SegmentFactoryInterface
{
private string $customKey;

private SegmentFactory $defaultFactory;

public function __construct(string $customKey)
{
$this->customKey = $customKey;
$this->defaultFactory = new SegmentFactory();
}

public function segmentFromArray(array $rawArray): ?SegmentInterface
public function segmentFromArray(array $rawArray): SegmentInterface
{
if ($this->customKey !== $rawArray[0]) {
return null;
return $this->defaultFactory->segmentFromArray($rawArray);
}

return new class($rawArray) implements SegmentInterface {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/SegmentedValuesTest.php
Expand Up @@ -6,6 +6,7 @@

use EDI\Parser;
use EdifactParser\SegmentedValues;
use EdifactParser\Segments\SegmentFactory;
use EdifactParser\Segments\UNHMessageHeader;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,7 +17,7 @@ public function getListOfSegments(): void
{
$fileContent = "UNH+1+IFTMIN:S:93A:UN:PN001'\nUNH+2+IFTMIN:S:93A:UN:PN001'";
$parser = new Parser($fileContent);
$values = SegmentedValues::fromRaw($parser->get());
$values = (new SegmentedValues(new SegmentFactory()))->fromRaw($parser->get());

self::assertEquals([
new UNHMessageHeader(['UNH', '1', ['IFTMIN', 'S', '93A', 'UN', 'PN001']]),
Expand Down

0 comments on commit 13ac766

Please sign in to comment.