hedera-web/rest/edi/lib/message.php

143 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2016-08-31 11:53:46 +00:00
namespace Edi;
2018-05-23 10:14:20 +00:00
require_once(__DIR__.'/section.php');
2018-05-23 10:14:20 +00:00
class SectionInfo {
var $schema;
var $parentInfo;
var $section;
}
2018-05-23 10:14:20 +00:00
class Message {
var $section;
2018-05-23 10:14:20 +00:00
static function loadSchema($schemaName) {
$ediSchemaStr = file_get_contents(__DIR__."/$schemaName.json", TRUE);
if ($ediSchemaStr !== FALSE)
2018-05-23 10:14:20 +00:00
return json_decode($ediSchemaStr, TRUE);
return NULL;
}
2018-05-23 10:14:20 +00:00
static function isEdiString(&$string) {
return substr($string, 0, 4) == 'UNB+';
}
2018-05-23 10:14:20 +00:00
function parse(&$string, &$schema = NULL) {
global $delimiters;
2018-05-23 10:14:20 +00:00
if (!self::isEdiString($string))
throw new \Exception('Not an EDI string.');
$pos = 0;
$error = FALSE;
$endTag = NULL;
$firstLoop = TRUE;
$newSection = TRUE;
2018-05-23 10:14:20 +00:00
$info = new SectionInfo();
$info->schema = $schema;
$info->parentInfo = NULL;
$info->section = NULL;
$topInfo = $info;
try {
2018-05-23 10:14:20 +00:00
while (TRUE) {
$segment = $this->parseSegment($string, $pos);
2018-05-23 10:14:20 +00:00
if (!$segment &&(!$endTag || !$info))
break;
2018-05-23 10:14:20 +00:00
if (!$segment ||($segment && !$info))
throw new \Exception();
2018-05-23 10:14:20 +00:00
if ($firstLoop) {
if ($segment->name != $info->schema['mainTag'])
2018-05-23 10:14:20 +00:00
throw new \Exception();
2018-05-23 11:09:55 +00:00
} else {
for ($i = $info; $i; $i = $i->parentInfo)
2018-05-23 10:14:20 +00:00
if (isset($i->schema['childs'][$segment->name])) {
$info = new SectionInfo();
$info->schema = $i->schema['childs'][$segment->name];
$info->parentInfo = $i;
$newSection = TRUE;
break;
}
}
2018-05-23 10:14:20 +00:00
if ($newSection) {
$section = new Section();
$section->name = $segment->name;
$info->section = $section;
2018-05-23 10:14:20 +00:00
if ($info->parentInfo) {
$section->parent = $info->parentInfo->section;
$section->parent->childs[$segment->name][] = $section;
}
2018-05-23 10:14:20 +00:00
if (isset($info->schema['endTag']))
$endTag = $info;
$newSection = FALSE;
}
2018-05-23 10:14:20 +00:00
if ($endTag && $endTag->schema['endTag'] == $segment->name) {
$endTag->section->segments[] = $segment;
$info = $endTag->parentInfo;
for ($i = $info; $i; $i = $i->parentInfo)
2018-05-23 10:14:20 +00:00
if (isset($i->schema['endTag'])) {
$endTag = $i;
break;
}
2018-05-23 11:09:55 +00:00
} else
$info->section->segments[] = $segment;
$firstLoop = FALSE;
2018-05-23 11:09:55 +00:00
}} catch (\Exception $e) {
2018-05-23 10:14:20 +00:00
throw new \Exception(sprintf('Parse error, something is wrong near "%s"',
substr($string, $pos, 10)));
}
$this->section = $topInfo->section;
}
2018-05-23 10:14:20 +00:00
function parseSegment(&$string, &$pos) {
$empty = TRUE;
$values = [];
2018-05-23 10:14:20 +00:00
while (TRUE) {
if (!isset($string{$pos}))
return NULL;
2018-05-23 10:14:20 +00:00
if (in_array($string{$pos}, ['+', ':', '\''])) {
if (!$empty) {
$values[] =
2018-05-23 10:14:20 +00:00
trim(substr($string, $start, $pos - $start));
$empty = TRUE;
}
}
2018-05-23 10:14:20 +00:00
elseif ($empty) {
$start = $pos;
$empty = FALSE;
}
if ($string{$pos} === '\'')
break;
$pos++;
}
$pos++;
2018-05-23 10:14:20 +00:00
$segment = new Segment();
$segment->name = $values[0];
$segment->values = $values;
return $segment;
}
}