Skip to content

Commit

Permalink
Direct creation of Message with Segments, add BGM and TDT
Browse files Browse the repository at this point in the history
  • Loading branch information
sabas committed Jul 12, 2024
1 parent f95cec6 commit 6e26ae3
Show file tree
Hide file tree
Showing 5 changed files with 606 additions and 45 deletions.
86 changes: 44 additions & 42 deletions src/Generator/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Base
protected $receiver;

/** @var string */
// protected $managingOrganisation = '89';
// protected $managingOrganisation = '89';

/**
* @param $keyName
Expand Down Expand Up @@ -114,6 +114,46 @@ public function setReceiver($receiver)
return $this;
}

/**
* Crop String to max char length
*
* @param string $string
* @param int $length
*
* @return string
*/
protected static function maxChars($string, $length = 35)
{
if (empty($string)) {
return '';
}

return mb_substr($string, 0, $length);
}

/**
*
* @param $value
* @param $array
* @param $errorMessage
*
* @throws EdifactException
*/
protected function isAllowed($value, $array, $errorMessage = null)
{
if ($errorMessage === null) {
$errorMessage = 'value: ' . $value . ' is not in allowed values: ' .
' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' .
debug_backtrace()[1]['function'];
}
if (!in_array($value, $array, true)) {
throw new EdifactException($errorMessage);
}
}

/**
* SEGMENT UTILITIES
*/

/**
* @param string, $functionCode
Expand Down Expand Up @@ -148,10 +188,10 @@ protected function addRFFSegment($functionCode, $identifier)
protected function addDTMSegment($date, $type, $formatQualifier = EdifactDate::DATE)
{
$data = [];
$data[] = (string)$type;
$data[] = (string) $type;
if (!empty($date)) {
$data[] = EdifactDate::get($date, $formatQualifier);
$data[] = (string)$formatQualifier;
$data[] = (string) $formatQualifier;
}

return ['DTM', $data];
Expand All @@ -176,44 +216,6 @@ public static function addBGMSegment($documentNumber, $type)
];
}

/**
* Crop String to max char length
*
* @param string $string
* @param int $length
*
* @return string
*/
protected static function maxChars($string, $length = 35)
{
if (empty($string)) {
return '';
}

return mb_substr($string, 0, $length);
}

/**
*
* @param $value
* @param $array
* @param $errorMessage
*
* @throws EdifactException
*/
protected function isAllowed($value, $array, $errorMessage = null)
{
if ($errorMessage === null) {
$errorMessage = 'value: ' . $value . ' is not in allowed values: ' .
' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' .
debug_backtrace()[1]['function'];
}
if (!in_array($value, $array, true)) {
throw new EdifactException($errorMessage);
}
}


/**
* @param $qualifier
* @param $value
Expand All @@ -226,7 +228,7 @@ public static function addMOASegment($qualifier, $value)
'MOA',
[
'',
(string)$qualifier,
(string) $qualifier,
EdiFactNumber::convert($value),
],
];
Expand Down
18 changes: 15 additions & 3 deletions src/Generator/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ public function __construct(
} else {
$this->messageID = $messageID;
}

$this->messageContent = [];
}

public function setMessageContent($messageContent)
{
$this->messageContent = $messageContent;
return $this;
}

public function addSegment($segment)
{
if ($segment instanceof \EDI\Generator\Segment) {
$segment = $segment->compose()->getComposed();
}
$this->messageContent[] = $segment;
return $this;
}

public function getMessageID()
Expand Down Expand Up @@ -94,7 +106,7 @@ public function compose()
*/
public static function dtmSegment($type, $dtmString, $format = 203)
{
return ['DTM', [$type, $dtmString, $format]];
return self::addDTMSegment($dtmString, $type, $format);
}

/**
Expand All @@ -107,7 +119,7 @@ public static function dtmSegment($type, $dtmString, $format = 203)
*/
public static function rffSegment($functionCode, $identifier)
{
return ['RFF', [$functionCode, $identifier]];
return self::addRFFSegment($functionCode, $identifier);
}

/**
Expand All @@ -117,7 +129,7 @@ public static function rffSegment($functionCode, $identifier)
* $secondaryLoc = preferred [locode, 139, 6] (if needed)
* @param $qualifier
* @param $firstLoc
*@param $secondaryLoc
* @param $secondaryLoc
* @return array
*/
public static function locSegment($qualifier, $firstLoc, $secondaryLoc = null)
Expand Down
10 changes: 10 additions & 0 deletions src/Generator/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public function setComposed(array $aComposed): void
{
$this->aComposed = $aComposed;
}

/**
* Compose.
*
* @return self
*/
public function compose(): self
{
return $this;
}
}
188 changes: 188 additions & 0 deletions src/Generator/Segment/BeginningOfMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace EDI\Generator\Segment;

use EDI\Generator\Segment;

/**
* Beginning of Message.
*
* @see https://service.unece.org/trade/untdid/d17b/trsd/trsdbgm.htm
*/
class BeginningOfMessage extends Segment
{
public const SEGMENT_NAME = 'BGM';

protected $aDocument = [];
protected $aDocumentIdentification = [];
protected $sMessageFunctionCode;
protected $sResponseTypeCode;
protected $sDocumentStatusCode;
protected $sLanguageNameCode;

/**
* Set Document (C002).
*
* @param string|null $sDocumentNameCode
* @param string|null $sCodeListIdentificationCode
* @param string|null $sCodeListResponsibleAgencyCode
* @param string|null $sDocumentName
*
* @return self
*/
public function setDocument(
?string $sDocumentNameCode = null,
?string $sCodeListIdentificationCode = null,
?string $sCodeListResponsibleAgencyCode = null,
?string $sDocumentName = null
): self {
$aDocument = [];

if ($sDocumentNameCode !== null) {
$aDocument[] = $sDocumentNameCode;
}

if ($sCodeListIdentificationCode !== null) {
$aDocument[] = $sCodeListIdentificationCode;
}

if ($sCodeListResponsibleAgencyCode !== null) {
$aDocument[] = $sCodeListResponsibleAgencyCode;
}

if ($sDocumentName !== null) {
$aDocument[] = $sDocumentName;
}

$this->aDocument = $aDocument;

return $this;
}

/**
* Set Document Identification (C106).
*
* @param string|null $sDocumentIdentifier
* @param string|null $sVersionIdentifier
* @param string|null $sRevisionIdentifier
*
* @return self
*/
public function setDocumentIdentification(
?string $sDocumentIdentifier = null,
?string $sVersionIdentifier = null,
?string $sRevisionIdentifier = null
): self {
$aDocumentIdentification = [];

if ($sDocumentIdentifier !== null) {
$aDocumentIdentification[] = $sDocumentIdentifier;
}

if ($sVersionIdentifier !== null) {
$aDocumentIdentification[] = $sVersionIdentifier;
}

if ($sRevisionIdentifier !== null) {
$aDocumentIdentification[] = $sRevisionIdentifier;
}

$this->aDocumentIdentification = $aDocumentIdentification;

return $this;
}

/**
* Set Message Function Code.
*
* @param string $sMessageFunctionCode
*
* @return self
*/
public function setMessageFunctionCode(string $sMessageFunctionCode): self
{
$this->sMessageFunctionCode = $sMessageFunctionCode;

return $this;
}

/**
* Set Response Type Code.
*
* @param string $sResponseTypeCode
*
* @return self
*/
public function setResponseTypeCode(string $sResponseTypeCode): self
{
$this->sResponseTypeCode = $sResponseTypeCode;

return $this;
}

/**
* Set Document Status Code.
*
* @param string $sDocumentStatusCode
*
* @return self
*/
public function setDocumentStatusCode(string $sDocumentStatusCode): self
{
$this->sDocumentStatusCode = $sDocumentStatusCode;

return $this;
}

/**
* Set Language Name Code.
*
* @param string $sLanguageNameCode
*
* @return self
*/
public function setLanguageNameCode(string $sLanguageNameCode): self
{
$this->sLanguageNameCode = $sLanguageNameCode;

return $this;
}

/**
* Compose.
*
* @return self
*/
public function compose(): self
{
$aComposed = [self::SEGMENT_NAME];

if (!empty($this->aDocument)) {
$aComposed[] = $this->aDocument;
}

if (!empty($this->aDocumentIdentification)) {
$aComposed[] = $this->aDocumentIdentification;
}

if ($this->sMessageFunctionCode !== null) {
$aComposed[] = $this->sMessageFunctionCode;
}

if ($this->sResponseTypeCode !== null) {
$aComposed[] = $this->sResponseTypeCode;
}

if ($this->sDocumentStatusCode !== null) {
$aComposed[] = $this->sDocumentStatusCode;
}

if ($this->sLanguageNameCode !== null) {
$aComposed[] = $this->sLanguageNameCode;
}

$this->setComposed($aComposed);

return $this;
}
}
Loading

0 comments on commit 6e26ae3

Please sign in to comment.