From 6e26ae35ed87d668b17095e3051038cfdc3686eb Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Fri, 12 Jul 2024 16:00:52 +0200 Subject: [PATCH] Direct creation of Message with Segments, add BGM and TDT --- src/Generator/Base.php | 86 ++--- src/Generator/Message.php | 18 +- src/Generator/Segment.php | 10 + src/Generator/Segment/BeginningOfMessage.php | 188 ++++++++++ .../Segment/TransportInformation.php | 349 ++++++++++++++++++ 5 files changed, 606 insertions(+), 45 deletions(-) create mode 100644 src/Generator/Segment/BeginningOfMessage.php create mode 100644 src/Generator/Segment/TransportInformation.php diff --git a/src/Generator/Base.php b/src/Generator/Base.php index 1e28c8d..f5467a2 100755 --- a/src/Generator/Base.php +++ b/src/Generator/Base.php @@ -22,7 +22,7 @@ class Base protected $receiver; /** @var string */ -// protected $managingOrganisation = '89'; + // protected $managingOrganisation = '89'; /** * @param $keyName @@ -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 @@ -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]; @@ -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 @@ -226,7 +228,7 @@ public static function addMOASegment($qualifier, $value) 'MOA', [ '', - (string)$qualifier, + (string) $qualifier, EdiFactNumber::convert($value), ], ]; diff --git a/src/Generator/Message.php b/src/Generator/Message.php index 84b6521..7af76bf 100644 --- a/src/Generator/Message.php +++ b/src/Generator/Message.php @@ -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() @@ -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); } /** @@ -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); } /** @@ -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) diff --git a/src/Generator/Segment.php b/src/Generator/Segment.php index 53f585b..2a6ea9b 100644 --- a/src/Generator/Segment.php +++ b/src/Generator/Segment.php @@ -30,4 +30,14 @@ public function setComposed(array $aComposed): void { $this->aComposed = $aComposed; } + + /** + * Compose. + * + * @return self + */ + public function compose(): self + { + return $this; + } } diff --git a/src/Generator/Segment/BeginningOfMessage.php b/src/Generator/Segment/BeginningOfMessage.php new file mode 100644 index 0000000..a8b9ecc --- /dev/null +++ b/src/Generator/Segment/BeginningOfMessage.php @@ -0,0 +1,188 @@ +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; + } +} \ No newline at end of file diff --git a/src/Generator/Segment/TransportInformation.php b/src/Generator/Segment/TransportInformation.php new file mode 100644 index 0000000..aacc6d3 --- /dev/null +++ b/src/Generator/Segment/TransportInformation.php @@ -0,0 +1,349 @@ +sTransportStageCodeQualifier = $sTransportStageCodeQualifier; + return $this; + } + + /** + * Set Means of Transport Journey Identifier. + * + * @param string $sMeansOfTransportJourneyIdentifier + * @return self + */ + public function setMeansOfTransportJourneyIdentifier(string $sMeansOfTransportJourneyIdentifier): self + { + $this->sMeansOfTransportJourneyIdentifier = $sMeansOfTransportJourneyIdentifier; + return $this; + } + + /** + * Set Mode of Transport (C220). + * + * @param string|null $sTransportModeNameCode + * @param string|null $sTransportModeName + * @return self + */ + public function setModeOfTransport(?string $sTransportModeNameCode = null, ?string $sTransportModeName = null): self + { + $aModeOfTransport = []; + if ($sTransportModeNameCode !== null) { + $aModeOfTransport[] = $sTransportModeNameCode; + } + if ($sTransportModeName !== null) { + $aModeOfTransport[] = $sTransportModeName; + } + $this->aModeOfTransport = $aModeOfTransport; + return $this; + } + + /** + * Set Transport Means (C001). + * + * @param string|null $sTransportMeansDescriptionCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportMeansDescription + * @return self + */ + public function setTransportMeans( + ?string $sTransportMeansDescriptionCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportMeansDescription = null + ): self { + $aTransportMeans = []; + if ($sTransportMeansDescriptionCode !== null) { + $aTransportMeans[] = $sTransportMeansDescriptionCode; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportMeans[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportMeans[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportMeansDescription !== null) { + $aTransportMeans[] = $sTransportMeansDescription; + } + $this->aTransportMeans = $aTransportMeans; + return $this; + } + + /** + * Set Carrier (C040). + * + * @param string|null $sCarrierIdentifier + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sCarrierName + * @return self + */ + public function setCarrier( + ?string $sCarrierIdentifier = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sCarrierName = null + ): self { + $aCarrier = []; + if ($sCarrierIdentifier !== null) { + $aCarrier[] = $sCarrierIdentifier; + } + if ($sCodeListIdentificationCode !== null) { + $aCarrier[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aCarrier[] = $sCodeListResponsibleAgencyCode; + } + if ($sCarrierName !== null) { + $aCarrier[] = $sCarrierName; + } + $this->aCarrier = $aCarrier; + return $this; + } + + /** + * Set Transit Direction Indicator Code. + * + * @param string $sTransitDirectionIndicatorCode + * @return self + */ + public function setTransitDirectionIndicatorCode(string $sTransitDirectionIndicatorCode): self + { + $this->sTransitDirectionIndicatorCode = $sTransitDirectionIndicatorCode; + return $this; + } + + /** + * Set Excess Transportation Information (C401). + * + * @param string $sExcessTransportationReasonCode + * @param string $sExcessTransportationResponsibilityCode + * @param string|null $sCustomerShipmentAuthorisationIdentifier + * @return self + */ + public function setExcessTransportationInformation( + string $sExcessTransportationReasonCode, + ?string $sExcessTransportationResponsibilityCode = null, + ?string $sCustomerShipmentAuthorisationIdentifier = null + ): self { + $aExcessTransportationInformation = [ + $sExcessTransportationReasonCode + ]; + if ($sExcessTransportationResponsibilityCode !== null) { + $aExcessTransportationInformation[] = $sExcessTransportationResponsibilityCode; + } + if ($sCustomerShipmentAuthorisationIdentifier !== null) { + $aExcessTransportationInformation[] = $sCustomerShipmentAuthorisationIdentifier; + } + $this->aExcessTransportationInformation = $aExcessTransportationInformation; + return $this; + } + + /** + * Set Transport Identification (C222). + * + * @param string|null $sTransportMeansIdentificationNameIdentifier + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportMeansIdentificationName + * @param string|null $sTransportMeansNationalityCode + * @return self + */ + public function setTransportIdentification( + ?string $sTransportMeansIdentificationNameIdentifier = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportMeansIdentificationName = null, + ?string $sTransportMeansNationalityCode = null + ): self { + $aTransportIdentification = []; + if ($sTransportMeansIdentificationNameIdentifier !== null) { + $aTransportIdentification[] = $sTransportMeansIdentificationNameIdentifier; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportIdentification[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportIdentification[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportMeansIdentificationName !== null) { + $aTransportIdentification[] = $sTransportMeansIdentificationName; + } + if ($sTransportMeansNationalityCode !== null) { + $aTransportIdentification[] = $sTransportMeansNationalityCode; + } + $this->aTransportIdentification = $aTransportIdentification; + return $this; + } + + /** + * Set Transport Means Ownership Indicator Code. + * + * @param string $sTransportMeansOwnershipIndicatorCode + * @return self + */ + public function setTransportMeansOwnershipIndicatorCode(string $sTransportMeansOwnershipIndicatorCode): self + { + $this->sTransportMeansOwnershipIndicatorCode = $sTransportMeansOwnershipIndicatorCode; + return $this; + } + + /** + * Set Power Type (C003). + * + * @param string|null $sPowerTypeCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sPowerTypeDescription + * @return self + */ + public function setPowerType( + ?string $sPowerTypeCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sPowerTypeDescription = null + ): self { + $aPowerType = []; + if ($sPowerTypeCode !== null) { + $aPowerType[] = $sPowerTypeCode; + } + if ($sCodeListIdentificationCode !== null) { + $aPowerType[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aPowerType[] = $sCodeListResponsibleAgencyCode; + } + if ($sPowerTypeDescription !== null) { + $aPowerType[] = $sPowerTypeDescription; + } + $this->aPowerType = $aPowerType; + return $this; + } + + /** + * Set Transport Service (C290). + * + * @param string|null $sTransportServiceIdentificationCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportServiceName + * @param string|null $sTransportServiceDescription + * @return self + */ + public function setTransportService( + ?string $sTransportServiceIdentificationCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportServiceName = null, + ?string $sTransportServiceDescription = null + ): self { + $aTransportService = []; + if ($sTransportServiceIdentificationCode !== null) { + $aTransportService[] = $sTransportServiceIdentificationCode; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportService[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportService[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportServiceName !== null) { + $aTransportService[] = $sTransportServiceName; + } + if ($sTransportServiceDescription !== null) { + $aTransportService[] = $sTransportServiceDescription; + } + $this->aTransportService = $aTransportService; + return $this; + } + + /** + * Compose the segment. + * + * @return self + */ + public function compose(): self + { + $aComposed = [self::SEGMENT_NAME]; + + if ($this->sTransportStageCodeQualifier !== null) { + $aComposed[] = $this->sTransportStageCodeQualifier; + } + + if ($this->sMeansOfTransportJourneyIdentifier !== null) { + $aComposed[] = $this->sMeansOfTransportJourneyIdentifier; + } + + if (!empty($this->aModeOfTransport)) { + $aComposed[] = $this->aModeOfTransport; + } + + if (!empty($this->aTransportMeans)) { + $aComposed[] = $this->aTransportMeans; + } + + if (!empty($this->aCarrier)) { + $aComposed[] = $this->aCarrier; + } + + if ($this->sTransitDirectionIndicatorCode !== null) { + $aComposed[] = $this->sTransitDirectionIndicatorCode; + } + + if (!empty($this->aExcessTransportationInformation)) { + $aComposed[] = $this->aExcessTransportationInformation; + } + + if (!empty($this->aTransportIdentification)) { + $aComposed[] = $this->aTransportIdentification; + } + + if ($this->sTransportMeansOwnershipIndicatorCode !== null) { + $aComposed[] = $this->sTransportMeansOwnershipIndicatorCode; + } + + if (!empty($this->aPowerType)) { + $aComposed[] = $this->aPowerType; + } + + if (!empty($this->aTransportService)) { + $aComposed[] = $this->aTransportService; + } + + $this->setComposed($aComposed); + + return $this; + } +} \ No newline at end of file