Skip to content

Commit 6e26ae3

Browse files
committed
Direct creation of Message with Segments, add BGM and TDT
1 parent f95cec6 commit 6e26ae3

5 files changed

Lines changed: 606 additions & 45 deletions

File tree

src/Generator/Base.php

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Base
2222
protected $receiver;
2323

2424
/** @var string */
25-
// protected $managingOrganisation = '89';
25+
// protected $managingOrganisation = '89';
2626

2727
/**
2828
* @param $keyName
@@ -114,6 +114,46 @@ public function setReceiver($receiver)
114114
return $this;
115115
}
116116

117+
/**
118+
* Crop String to max char length
119+
*
120+
* @param string $string
121+
* @param int $length
122+
*
123+
* @return string
124+
*/
125+
protected static function maxChars($string, $length = 35)
126+
{
127+
if (empty($string)) {
128+
return '';
129+
}
130+
131+
return mb_substr($string, 0, $length);
132+
}
133+
134+
/**
135+
*
136+
* @param $value
137+
* @param $array
138+
* @param $errorMessage
139+
*
140+
* @throws EdifactException
141+
*/
142+
protected function isAllowed($value, $array, $errorMessage = null)
143+
{
144+
if ($errorMessage === null) {
145+
$errorMessage = 'value: ' . $value . ' is not in allowed values: ' .
146+
' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' .
147+
debug_backtrace()[1]['function'];
148+
}
149+
if (!in_array($value, $array, true)) {
150+
throw new EdifactException($errorMessage);
151+
}
152+
}
153+
154+
/**
155+
* SEGMENT UTILITIES
156+
*/
117157

118158
/**
119159
* @param string, $functionCode
@@ -148,10 +188,10 @@ protected function addRFFSegment($functionCode, $identifier)
148188
protected function addDTMSegment($date, $type, $formatQualifier = EdifactDate::DATE)
149189
{
150190
$data = [];
151-
$data[] = (string)$type;
191+
$data[] = (string) $type;
152192
if (!empty($date)) {
153193
$data[] = EdifactDate::get($date, $formatQualifier);
154-
$data[] = (string)$formatQualifier;
194+
$data[] = (string) $formatQualifier;
155195
}
156196

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

179-
/**
180-
* Crop String to max char length
181-
*
182-
* @param string $string
183-
* @param int $length
184-
*
185-
* @return string
186-
*/
187-
protected static function maxChars($string, $length = 35)
188-
{
189-
if (empty($string)) {
190-
return '';
191-
}
192-
193-
return mb_substr($string, 0, $length);
194-
}
195-
196-
/**
197-
*
198-
* @param $value
199-
* @param $array
200-
* @param $errorMessage
201-
*
202-
* @throws EdifactException
203-
*/
204-
protected function isAllowed($value, $array, $errorMessage = null)
205-
{
206-
if ($errorMessage === null) {
207-
$errorMessage = 'value: ' . $value . ' is not in allowed values: ' .
208-
' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' .
209-
debug_backtrace()[1]['function'];
210-
}
211-
if (!in_array($value, $array, true)) {
212-
throw new EdifactException($errorMessage);
213-
}
214-
}
215-
216-
217219
/**
218220
* @param $qualifier
219221
* @param $value
@@ -226,7 +228,7 @@ public static function addMOASegment($qualifier, $value)
226228
'MOA',
227229
[
228230
'',
229-
(string)$qualifier,
231+
(string) $qualifier,
230232
EdiFactNumber::convert($value),
231233
],
232234
];

src/Generator/Message.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,23 @@ public function __construct(
4343
} else {
4444
$this->messageID = $messageID;
4545
}
46+
47+
$this->messageContent = [];
4648
}
4749

4850
public function setMessageContent($messageContent)
4951
{
5052
$this->messageContent = $messageContent;
53+
return $this;
54+
}
55+
56+
public function addSegment($segment)
57+
{
58+
if ($segment instanceof \EDI\Generator\Segment) {
59+
$segment = $segment->compose()->getComposed();
60+
}
61+
$this->messageContent[] = $segment;
62+
return $this;
5163
}
5264

5365
public function getMessageID()
@@ -94,7 +106,7 @@ public function compose()
94106
*/
95107
public static function dtmSegment($type, $dtmString, $format = 203)
96108
{
97-
return ['DTM', [$type, $dtmString, $format]];
109+
return self::addDTMSegment($dtmString, $type, $format);
98110
}
99111

100112
/**
@@ -107,7 +119,7 @@ public static function dtmSegment($type, $dtmString, $format = 203)
107119
*/
108120
public static function rffSegment($functionCode, $identifier)
109121
{
110-
return ['RFF', [$functionCode, $identifier]];
122+
return self::addRFFSegment($functionCode, $identifier);
111123
}
112124

113125
/**
@@ -117,7 +129,7 @@ public static function rffSegment($functionCode, $identifier)
117129
* $secondaryLoc = preferred [locode, 139, 6] (if needed)
118130
* @param $qualifier
119131
* @param $firstLoc
120-
*@param $secondaryLoc
132+
* @param $secondaryLoc
121133
* @return array
122134
*/
123135
public static function locSegment($qualifier, $firstLoc, $secondaryLoc = null)

src/Generator/Segment.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public function setComposed(array $aComposed): void
3030
{
3131
$this->aComposed = $aComposed;
3232
}
33+
34+
/**
35+
* Compose.
36+
*
37+
* @return self
38+
*/
39+
public function compose(): self
40+
{
41+
return $this;
42+
}
3343
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace EDI\Generator\Segment;
4+
5+
use EDI\Generator\Segment;
6+
7+
/**
8+
* Beginning of Message.
9+
*
10+
* @see https://service.unece.org/trade/untdid/d17b/trsd/trsdbgm.htm
11+
*/
12+
class BeginningOfMessage extends Segment
13+
{
14+
public const SEGMENT_NAME = 'BGM';
15+
16+
protected $aDocument = [];
17+
protected $aDocumentIdentification = [];
18+
protected $sMessageFunctionCode;
19+
protected $sResponseTypeCode;
20+
protected $sDocumentStatusCode;
21+
protected $sLanguageNameCode;
22+
23+
/**
24+
* Set Document (C002).
25+
*
26+
* @param string|null $sDocumentNameCode
27+
* @param string|null $sCodeListIdentificationCode
28+
* @param string|null $sCodeListResponsibleAgencyCode
29+
* @param string|null $sDocumentName
30+
*
31+
* @return self
32+
*/
33+
public function setDocument(
34+
?string $sDocumentNameCode = null,
35+
?string $sCodeListIdentificationCode = null,
36+
?string $sCodeListResponsibleAgencyCode = null,
37+
?string $sDocumentName = null
38+
): self {
39+
$aDocument = [];
40+
41+
if ($sDocumentNameCode !== null) {
42+
$aDocument[] = $sDocumentNameCode;
43+
}
44+
45+
if ($sCodeListIdentificationCode !== null) {
46+
$aDocument[] = $sCodeListIdentificationCode;
47+
}
48+
49+
if ($sCodeListResponsibleAgencyCode !== null) {
50+
$aDocument[] = $sCodeListResponsibleAgencyCode;
51+
}
52+
53+
if ($sDocumentName !== null) {
54+
$aDocument[] = $sDocumentName;
55+
}
56+
57+
$this->aDocument = $aDocument;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* Set Document Identification (C106).
64+
*
65+
* @param string|null $sDocumentIdentifier
66+
* @param string|null $sVersionIdentifier
67+
* @param string|null $sRevisionIdentifier
68+
*
69+
* @return self
70+
*/
71+
public function setDocumentIdentification(
72+
?string $sDocumentIdentifier = null,
73+
?string $sVersionIdentifier = null,
74+
?string $sRevisionIdentifier = null
75+
): self {
76+
$aDocumentIdentification = [];
77+
78+
if ($sDocumentIdentifier !== null) {
79+
$aDocumentIdentification[] = $sDocumentIdentifier;
80+
}
81+
82+
if ($sVersionIdentifier !== null) {
83+
$aDocumentIdentification[] = $sVersionIdentifier;
84+
}
85+
86+
if ($sRevisionIdentifier !== null) {
87+
$aDocumentIdentification[] = $sRevisionIdentifier;
88+
}
89+
90+
$this->aDocumentIdentification = $aDocumentIdentification;
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* Set Message Function Code.
97+
*
98+
* @param string $sMessageFunctionCode
99+
*
100+
* @return self
101+
*/
102+
public function setMessageFunctionCode(string $sMessageFunctionCode): self
103+
{
104+
$this->sMessageFunctionCode = $sMessageFunctionCode;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* Set Response Type Code.
111+
*
112+
* @param string $sResponseTypeCode
113+
*
114+
* @return self
115+
*/
116+
public function setResponseTypeCode(string $sResponseTypeCode): self
117+
{
118+
$this->sResponseTypeCode = $sResponseTypeCode;
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Set Document Status Code.
125+
*
126+
* @param string $sDocumentStatusCode
127+
*
128+
* @return self
129+
*/
130+
public function setDocumentStatusCode(string $sDocumentStatusCode): self
131+
{
132+
$this->sDocumentStatusCode = $sDocumentStatusCode;
133+
134+
return $this;
135+
}
136+
137+
/**
138+
* Set Language Name Code.
139+
*
140+
* @param string $sLanguageNameCode
141+
*
142+
* @return self
143+
*/
144+
public function setLanguageNameCode(string $sLanguageNameCode): self
145+
{
146+
$this->sLanguageNameCode = $sLanguageNameCode;
147+
148+
return $this;
149+
}
150+
151+
/**
152+
* Compose.
153+
*
154+
* @return self
155+
*/
156+
public function compose(): self
157+
{
158+
$aComposed = [self::SEGMENT_NAME];
159+
160+
if (!empty($this->aDocument)) {
161+
$aComposed[] = $this->aDocument;
162+
}
163+
164+
if (!empty($this->aDocumentIdentification)) {
165+
$aComposed[] = $this->aDocumentIdentification;
166+
}
167+
168+
if ($this->sMessageFunctionCode !== null) {
169+
$aComposed[] = $this->sMessageFunctionCode;
170+
}
171+
172+
if ($this->sResponseTypeCode !== null) {
173+
$aComposed[] = $this->sResponseTypeCode;
174+
}
175+
176+
if ($this->sDocumentStatusCode !== null) {
177+
$aComposed[] = $this->sDocumentStatusCode;
178+
}
179+
180+
if ($this->sLanguageNameCode !== null) {
181+
$aComposed[] = $this->sLanguageNameCode;
182+
}
183+
184+
$this->setComposed($aComposed);
185+
186+
return $this;
187+
}
188+
}

0 commit comments

Comments
 (0)