|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EDI\Generator; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Base |
| 7 | + * |
| 8 | + * @package EDI\Generator |
| 9 | + */ |
| 10 | +class Base |
| 11 | +{ |
| 12 | + |
| 13 | + /** @var array */ |
| 14 | + protected $messageContent = []; |
| 15 | + |
| 16 | + /** @var array */ |
| 17 | + protected $composed; |
| 18 | + |
| 19 | + /** @var string */ |
| 20 | + protected $sender; |
| 21 | + |
| 22 | + /** @var string */ |
| 23 | + protected $receiver; |
| 24 | + |
| 25 | + /** @var string */ |
| 26 | +// protected $managingOrganisation = '89'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param $keyName |
| 30 | + */ |
| 31 | + public function addKeyToCompose($keyName) |
| 32 | + { |
| 33 | + $this->composeKeys[] = $keyName; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * compose message by keys givven in an ordered array |
| 38 | + * |
| 39 | + * @param array $keys |
| 40 | + * |
| 41 | + * @return array |
| 42 | + * @throws EdifactException |
| 43 | + */ |
| 44 | + public function composeByKeys($keys = null) |
| 45 | + { |
| 46 | + if ($keys === null) { |
| 47 | + $keys = $this->composeKeys; |
| 48 | + } |
| 49 | + foreach ($keys as $key) { |
| 50 | + if (property_exists($this, $key)) { |
| 51 | + if ($this->{$key} !== null) { |
| 52 | + $value = $this->{$key}; |
| 53 | + if ($value) { |
| 54 | + $this->messageContent[] = $value; |
| 55 | + } else { |
| 56 | + throw new EdifactException("key " . $key . " returns no array structure"); |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + throw new EdifactException( |
| 61 | + 'key: ' . $key . ' not found for composeByKeys in ' . get_class($this) . '->' . |
| 62 | + debug_backtrace()[1]['function'] |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return $this->messageContent; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + public function getComposed() |
| 74 | + { |
| 75 | + return $this->composed; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public function getSender() |
| 82 | + { |
| 83 | + return $this->sender; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param string $sender |
| 88 | + * |
| 89 | + * @return $this |
| 90 | + */ |
| 91 | + public function setSender($sender) |
| 92 | + { |
| 93 | + $this->sender = $sender; |
| 94 | + |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return string |
| 100 | + */ |
| 101 | + public function getReceiver() |
| 102 | + { |
| 103 | + return $this->receiver; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param string $receiver |
| 108 | + * |
| 109 | + * @return $this |
| 110 | + */ |
| 111 | + public function setReceiver($receiver) |
| 112 | + { |
| 113 | + $this->receiver = $receiver; |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /** |
| 120 | + * @param string, $functionCode |
| 121 | + * @param $identifier |
| 122 | + * |
| 123 | + * @return array|bool |
| 124 | + */ |
| 125 | + protected function addRFFSegment($functionCode, $identifier) |
| 126 | + { |
| 127 | + if (empty($identifier)) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + return [ |
| 132 | + 'RFF', |
| 133 | + [ |
| 134 | + $functionCode, |
| 135 | + self::maxChars($identifier, 35), |
| 136 | + ], |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param $dateString |
| 142 | + * @param $type |
| 143 | + * @param int $formatQualifier |
| 144 | + * |
| 145 | + * @see http://www.unece.org/trade/untdid/d96a/trsd/trsddtm.htm |
| 146 | + * @return array |
| 147 | + * @throws EdifactException |
| 148 | + */ |
| 149 | + protected function addDTMSegment($dateString, $type, $formatQualifier = EdifactDate::DATE) |
| 150 | + { |
| 151 | + $data = []; |
| 152 | + $data[] = (string)$type; |
| 153 | + if (!empty($dateString)) { |
| 154 | + $data[] = EdifactDate::get($dateString, $formatQualifier); |
| 155 | + $data[] = (string)$formatQualifier; |
| 156 | + } |
| 157 | + |
| 158 | + return ['DTM', $data]; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @param $documentNumber |
| 163 | + * @param $type |
| 164 | + * |
| 165 | + * @return array |
| 166 | + */ |
| 167 | + public static function addBGMSegment($documentNumber, $type) |
| 168 | + { |
| 169 | + return [ |
| 170 | + 'BGM', |
| 171 | + [ |
| 172 | + $type, |
| 173 | + '', |
| 174 | + '89', |
| 175 | + ], |
| 176 | + $documentNumber, |
| 177 | + ]; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Crop String to max char length |
| 182 | + * |
| 183 | + * @param string $string |
| 184 | + * @param int $length |
| 185 | + * |
| 186 | + * @return string |
| 187 | + */ |
| 188 | + protected static function maxChars($string, $length = 35) |
| 189 | + { |
| 190 | + if (empty($string)) { |
| 191 | + return ''; |
| 192 | + } |
| 193 | + |
| 194 | + return mb_substr($string, 0, $length); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * |
| 199 | + * @param $value |
| 200 | + * @param $array |
| 201 | + * @param null $errorMessage |
| 202 | + * |
| 203 | + * @throws EdifactException |
| 204 | + */ |
| 205 | + protected function isAllowed($value, $array, $errorMessage = null) |
| 206 | + { |
| 207 | + if ($errorMessage === null) { |
| 208 | + $errorMessage = 'value: ' . $value . ' is not in allowed values: ' . |
| 209 | + ' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' . |
| 210 | + debug_backtrace()[1]['function']; |
| 211 | + } |
| 212 | + if (!in_array($value, $array, true)) { |
| 213 | + throw new EdifactException($errorMessage); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | + /** |
| 219 | + * @param $qualifier |
| 220 | + * @param $value |
| 221 | + * |
| 222 | + * @return array |
| 223 | + */ |
| 224 | + public static function addMOASegment($qualifier, $value) |
| 225 | + { |
| 226 | + return [ |
| 227 | + 'MOA', |
| 228 | + [ |
| 229 | + '', |
| 230 | + (string)$qualifier, |
| 231 | + EdiFactNumber::convert($value), |
| 232 | + ], |
| 233 | + ]; |
| 234 | + } |
| 235 | +} |
0 commit comments