Skip to content

Commit

Permalink
Create a tokenizer interface for different implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Jan 19, 2024
1 parent a9d4ff0 commit 7ea8b89
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ final class Parser implements ParserInterface
*/
private $factory;

/**
* @var TokenizerInterface $tokenizer
*/
private $tokenizer;


/**
* Create a new instance.
*
* @param FactoryInterface $factory A segment factory for creating segments
*/
public function __construct(FactoryInterface $factory = null)
public function __construct(FactoryInterface $factory = null, TokenizerInterface $tokenizer = null)
{
if ($factory === null) {
$factory = new Factory();
}
$this->factory = $factory;

if ($tokenizer === null) {
$tokenizer = new Tokenizer();
}
$this->tokenizer = $tokenizer;
}


Expand All @@ -51,11 +61,9 @@ public function __construct(FactoryInterface $factory = null)
*/
public function parse(string $message, ControlCharactersInterface $characters = null): iterable
{
$tokenizer = new Tokenizer();

$characters = $this->getControlCharacters($message, $characters);

$tokens = $tokenizer->getTokens($message, $characters);
$tokens = $this->tokenizer->getTokens($message, $characters);

$segments = $this->convertTokensToSegments($tokens, $characters);

Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Convert EDI messages into tokens for parsing.
*/
final class Tokenizer
final class Tokenizer implements TokenizerInterface
{
/**
* @var string $message The message that we are tokenizing.
Expand Down
20 changes: 20 additions & 0 deletions src/TokenizerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Estrato\Edifact;

use Estrato\Edifact\Control\CharactersInterface as ControlCharactersInterface;
use Estrato\Edifact\Exceptions\ParseException;

/**
* Convert EDI messages into tokens for parsing.
*/
interface TokenizerInterface
{
/**
* Convert the passed message into tokens.
*
* @return Token[]
* @throws ParseException
*/
public function getTokens(string $message, ControlCharactersInterface $characters): array;
}
26 changes: 26 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use duncan3dc\ObjectIntruder\Intruder;
use Estrato\Edifact\Control\CharactersInterface as ControlCharactersInterface;
use Estrato\Edifact\Parser;
use Estrato\Edifact\Segments\Factory;
use Estrato\Edifact\Segments\Segment;
use Estrato\Edifact\Segments\SegmentInterface;
use Estrato\Edifact\Token;
use Estrato\Edifact\TokenizerInterface;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -168,4 +171,27 @@ public function testEscapeSequence(): void
new Segment("ERC", ["10", ":+?' - :+?' - :+?'"]),
]);
}


public function testCustomTokenizer(): void
{
$tokenizer = Mockery::mock(TokenizerInterface::class);
$tokens = [
new Token(Token::CONTENT, "QTY"),
new Token(Token::DATA_SEPARATOR, "+"),
new Token(Token::CONTENT, "136"),
new Token(Token::COMPONENT_SEPARATOR, ":"),
new Token(Token::CONTENT, "999"),
new Token(Token::TERMINATOR, "'"),
];
$tokenizer->shouldReceive('getTokens')->once()->with("QTY+136:12,235", Mockery::type(ControlCharactersInterface::class))->andReturn($tokens);

$parser = new Parser(new Factory(), $tokenizer);
$segments = $parser->parse("UNA:+,? '\nQTY+136:12,235");
$segments = is_array($segments) ? $segments : iterator_to_array($segments);

$this->assertEquals([
new Segment("QTY", ["136", "999"]),
], $segments);
}
}

0 comments on commit 7ea8b89

Please sign in to comment.