finished default parser
This commit is contained in:
20
src/Application/Boundary/PersonMapper.php
Normal file
20
src/Application/Boundary/PersonMapper.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application\Boundary;
|
||||
|
||||
use Enzyme\Axiom\Bags\BagInterface;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
interface PersonMapper
|
||||
{
|
||||
public function insert();
|
||||
|
||||
public function update();
|
||||
|
||||
public function delete();
|
||||
|
||||
/**
|
||||
* @param Uuid $id
|
||||
* @return BagInterface
|
||||
*/
|
||||
public function fetch(Uuid $id);
|
||||
}
|
||||
14
src/Application/PersonFactory.php
Normal file
14
src/Application/PersonFactory.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\EmailAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\Person;
|
||||
|
||||
class PersonFactory implements \XaiCorp\AbcParser\Domain\Boundary\PersonFactory
|
||||
{
|
||||
public function create(StringAtom $name, EmailAtom $email)
|
||||
{
|
||||
return new Person($name, $email);
|
||||
}
|
||||
}
|
||||
44
src/Application/PersonRepository.php
Normal file
44
src/Application/PersonRepository.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
use XaiCorp\AbcParser\Application\Boundary\PersonMapper;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\EmailAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\Person;
|
||||
|
||||
class PersonRepository implements \XaiCorp\AbcParser\Domain\Boundary\PersonRepository
|
||||
{
|
||||
/**
|
||||
* @var PersonMapper
|
||||
*/
|
||||
protected $mapper;
|
||||
|
||||
protected $store = [];
|
||||
|
||||
public function __construct(PersonMapper $personMapper)
|
||||
{
|
||||
$this->mapper = $personMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Uuid $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPerson(Uuid $id)
|
||||
{
|
||||
if (!empty($this->store[$id->string])) {
|
||||
$person = $this->store[$id->string];
|
||||
} else {
|
||||
$personInfo = $this->mapper->fetch($id);
|
||||
|
||||
$person = new Person(
|
||||
new StringAtom($personInfo->get('name')),
|
||||
new EmailAtom($personInfo->get('email'))
|
||||
);
|
||||
$this->store[$id->string] = $person;
|
||||
}
|
||||
|
||||
return $person;
|
||||
}
|
||||
}
|
||||
55
src/Application/UseCases/ImportAbcFile.php
Normal file
55
src/Application/UseCases/ImportAbcFile.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application\UseCases;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\DefaultInterpreter;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Interpreter;
|
||||
|
||||
class ImportAbcFile
|
||||
{
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builderTemplate;
|
||||
|
||||
/**
|
||||
* @param Builder|null $builderTemplate
|
||||
*/
|
||||
public function __construct(Builder $builderTemplate = null)
|
||||
{
|
||||
$this->builderTemplate = $builderTemplate ?: new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $abc
|
||||
* @return TuneCollection
|
||||
* @throws \Enzyme\Axiom\Exceptions\AtomException
|
||||
*/
|
||||
public function import(string $abc) : TuneCollection
|
||||
{
|
||||
$context = new Context(new StringAtom($abc));
|
||||
$parser = $this->getBestParser($context);
|
||||
$result = $parser->execute($context);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @return Interpreter
|
||||
*/
|
||||
public function getBestParser(Context $context) : Interpreter
|
||||
{
|
||||
$builder = clone $this->builderTemplate;
|
||||
|
||||
switch($context->getVersion()) {
|
||||
default:
|
||||
$interpreter = new DefaultInterpreter($builder);
|
||||
}
|
||||
|
||||
return $interpreter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user