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

NEPT-1591: Make sure that requests can be parsed via XML #62 #63

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/Messages/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EC\Poetry\Messages\Traits\ArrayAccessTrait;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
use EC\Poetry\Messages\Traits\ParserAwareTrait;

/**
* Class AbstractMessage
Expand All @@ -15,6 +16,7 @@
abstract class AbstractMessage implements MessageInterface
{
use ArrayAccessTrait;
use ParserAwareTrait;

const COMMUNICATION_SYNCHRONOUS = 'synchrone';
const COMMUNICATION_ASYNCHRONOUS = 'asynchrone';
Expand Down Expand Up @@ -125,4 +127,20 @@ public function setMessageId($messageId)
{
$this->messageId = $messageId;
}

/**
* {@inheritdoc}
*/
public function withXml($xml)
{
$this->setRaw($xml);
$parser = $this->getParser();
$parser->addXmlContent($xml);

$xml = $parser->getOuterContent('POETRY/request/demandeId');
$this->getIdentifier()->withXml($xml);
$this->setMessageId($parser->getAttribute('POETRY/request', 'id'));

return $this;
}
}
28 changes: 27 additions & 1 deletion src/Messages/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package EC\Poetry\Messages
*/
interface ComponentInterface extends \ArrayAccess
interface ComponentInterface extends ParserAwareInterface, \ArrayAccess
{
/**
* Get template name.
Expand All @@ -30,10 +30,36 @@ public function getAttributes();
* Construct component from given array.
*
* @param array $properties
*
* @return $this
*/
public function withArray(array $properties);

/**
* Set message or component internal properties given its XML representation.
*
* @param string $xml
*
* @return $this
*/
public function withXml($xml);

/**
* Get raw XML.
*
* @return string
*/
public function getRaw();

/**
* Set raw XML.
*
* @param string $raw
*
* @return $this
*/
public function setRaw($raw);

/**
* @param \Symfony\Component\Validator\Mapping\ClassMetadata $metadata
*/
Expand Down
24 changes: 4 additions & 20 deletions src/Messages/Components/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
use EC\Poetry\Messages\ComponentInterface;
use EC\Poetry\Messages\Traits\ArrayAccessTrait;
use EC\Poetry\Messages\Traits\ParserAwareTrait;
use EC\Poetry\Messages\ParserAwareInterface;

/**
* Class AbstractComponent
*
* @package EC\Poetry\Messages\Components
*/
abstract class AbstractComponent implements ComponentInterface, ParserAwareInterface
abstract class AbstractComponent implements ComponentInterface
{
use ArrayAccessTrait;
use ParserAwareTrait;
Expand All @@ -29,27 +28,12 @@ public function getAttributes()
}

/**
* Set a message or a component internal properties given its XML representation.
*
* @param string $xml
* XML string.
*
* @return \EC\Poetry\Messages\MessageInterface|\EC\Poetry\Messages\ComponentInterface
* {@inheritdoc}
*/
public function fromXml($xml)
public function withXml($xml)
{
$this->setRaw($xml);

return $this->parseXml($xml);
return $this;
}

/**
* Parse a XML string into a set of properties.
*
* @param string $xml
* XML string.
*
* @return \EC\Poetry\Messages\MessageInterface|\EC\Poetry\Messages\ComponentInterface
*/
abstract protected function parseXml($xml);
}
2 changes: 1 addition & 1 deletion src/Messages/Components/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function setEmail($email)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function setReferenceFilesRemark($referenceFilesRemark)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function getGroupSequence()
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/ReferenceDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function setFile($file)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/ReturnAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function setRemark($remark)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
4 changes: 2 additions & 2 deletions src/Messages/Components/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function setFile($file)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand All @@ -314,7 +314,7 @@ private function parseSourceLanguage(Parser $parser)
$parser->eachComponent("documentSource/documentSourceLang", function (Parser $language) {
$this->withSourceLanguage()
->setParser($this->getParser())
->fromXml($language->outerHtml());
->withXml($language->outerHtml());
}, $this);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/SourceLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function setPages($pages)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function setMessage($message)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/Components/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function withReturnAddress()
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
$parser = $this->getParser();
$parser->addXmlContent($xml);
Expand Down
1 change: 0 additions & 1 deletion src/Messages/Components/TargetReturnAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class TargetReturnAddress extends ReturnAddress
{

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Messages/MessageAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace EC\Poetry\Messages;

use EC\Poetry\Messages\MessageInterface;

/**
* Interface MessageAwareInterface
*
Expand Down
34 changes: 1 addition & 33 deletions src/Messages/Notifications/AbstractNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

use EC\Poetry\Events\ParseNotificationEvent;
use EC\Poetry\Messages\AbstractMessage;
use EC\Poetry\Messages\Traits\ParserAwareTrait;
use EC\Poetry\Messages\ParserAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Class AbstractNotification.
*
* @package EC\Poetry\Messages\Notifications
*/
abstract class AbstractNotification extends AbstractMessage implements ParserAwareInterface, EventSubscriberInterface
abstract class AbstractNotification extends AbstractMessage implements EventSubscriberInterface
{
use ParserAwareTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -33,32 +29,4 @@ public static function getSubscribedEvents()
* @return mixed
*/
abstract public function onParseNotification(ParseNotificationEvent $event);

/**
* Set a message or a component internal properties given its XML representation.
*
* @param string $xml
* XML string.
*
* @return \EC\Poetry\Messages\MessageInterface|\EC\Poetry\Messages\ComponentInterface
*/
public function fromXml($xml)
{
$this->setRaw($xml);

return $this->parseXml($xml);
}

/**
* Parse a XML string into a set of properties.
*
* @param string $xml
* XML string.
*
* @return \EC\Poetry\Messages\MessageInterface|\EC\Poetry\Messages\ComponentInterface
*/
protected function parseXml($xml)
{
return $this;
}
}
15 changes: 6 additions & 9 deletions src/Messages/Notifications/StatusUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function onParseNotification(ParseNotificationEvent $event)
$parser = $this->getParser();
$parser->addXmlContent($event->getXml());
if ('status' === $parser->getAttribute('POETRY/request', 'type')) {
$this->fromXml($event->getXml());
$this->withXml($event->getXml());
$event->setEvent(new StatusUpdatedEvent($this));
$event->stopPropagation();
}
Expand All @@ -43,26 +43,23 @@ public function onParseNotification(ParseNotificationEvent $event)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
parent::withXml($xml);

$parser = $this->getParser();
$parser->addXmlContent($xml);

$xml = $parser->getOuterContent('POETRY/request/demandeId');
$this->getIdentifier()->fromXml($xml);

$this->setMessageId($parser->getAttribute('POETRY/request', 'id'));

$parser->eachComponent("POETRY/request/status", function (Parser $component) {
$this->withStatus()
->setParser($this->getParser())
->fromXml($component->outerHtml());
->withXml($component->outerHtml());
}, $this);

$parser->eachComponent("POETRY/request/attributions", function (Parser $component) {
$this->withTarget()
->setParser($this->getParser())
->fromXml($component->outerHtml());
->withXml($component->outerHtml());
}, $this);

return $this;
Expand Down
13 changes: 5 additions & 8 deletions src/Messages/Notifications/TranslationReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function onParseNotification(ParseNotificationEvent $event)
$parser = $this->getParser();
$parser->addXmlContent($event->getXml());
if ('translation' === $parser->getAttribute('POETRY/request', 'type')) {
$this->fromXml($event->getXml());
$this->withXml($event->getXml());
$event->setEvent(new TranslationReceivedEvent($this));
$event->stopPropagation();
}
Expand All @@ -41,20 +41,17 @@ public function onParseNotification(ParseNotificationEvent $event)
/**
* {@inheritdoc}
*/
protected function parseXml($xml)
public function withXml($xml)
{
parent::withXml($xml);

$parser = $this->getParser();
$parser->addXmlContent($xml);

$xml = $parser->getOuterContent('POETRY/request/demandeId');
$this->getIdentifier()->fromXml($xml);

$this->setMessageId($parser->getAttribute('POETRY/request', 'id'));

$parser->eachComponent("POETRY/request/attributions", function (Parser $component) {
$this->withTarget()
->setParser($this->getParser())
->fromXml($component->outerHtml());
->withXml($component->outerHtml());
}, $this);

return $this;
Expand Down
2 changes: 0 additions & 2 deletions src/Messages/ParserAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ interface ParserAwareInterface
* Get Parser property.
*
* @return \EC\Poetry\Services\Parser
* Property value.
*/
public function getParser();

/**
* Set Parser property.
*
* @param \EC\Poetry\Services\Parser $parser
* Property value.
*
* @return $this
*/
Expand Down
Loading