2017 © Pedro Peláez
 

library edifact

image

proengeno/edifact

  • Friday, June 22, 2018
  • by Apfelfrisch
  • Repository
  • 3 Watchers
  • 4 Stars
  • 6,115 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 14 % Grown

The README.md

PHP - EDIFACT

Unit test Static Analysis Mutation tests, (*1)

Parse, build, serialize and validate UN/EDIFACT Messages in a memory efficient way., (*2)

You will likely have to generate your own Segments, see php-edifact/edifact-mapping for XML Mappings. I have done a protype for autogeneration, it should give you a good starting point., (*3)

If you don't need validation or Segment getter you can also parse to the generic Segment., (*4)

Usage

Parse EDIFACT Messages

Load Segment Classes

You can add your Segments to the Factory like so:, (*5)

use Apfelfrisch\Edifact\Segment\SegmentFactory;

$segmentFactory = new SegmentFactory;
$segmentFactory->addSegment('SEQ', \My\Namespace\Segments\Seq::class);

After that you can either mark the Factory as default:, (*6)

$segmentFactory->markAsDefault();

or you inject the Factory in the Message:, (*7)

use Apfelfrisch\Edifact\Segment\SegmentFactory;

$message = Message::fromString("UNA:+.? 'SEQ+1", $segmentFactory);

If you don't need validation or Segment getter you can also parse to the generic Segement, (*8)

use Apfelfrisch\Edifact\Segments\Generic;
use Apfelfrisch\Edifact\Segment\SegmentFactory;

$segmentFactory = new SegmentFactory;
$segmentFactory->addFallback(Generic::class);
$segmentFactory->markAsDefault();

Parse from String

use Apfelfrisch\Edifact\Message;

$message = Message::fromString("UNA:+.? 'NAD+DP++++Musterstr.::10+City++12345+DE");

Parse from File

use Apfelfrisch\Edifact\Message;

$message = Message::fromFilepath('path/to/file.txt');

Iterate over Segments

use Apfelfrisch\Edifact\Segments\SegmentInterface;

foreach ($message->getSegments() as $segment) {
    if ($segment instanceof SegmentInterface) {
        echo $segment->name();
    }
}

Filter Segments

use My\Namespace\Segments\MyNad;

foreach ($message->filterSegments(MyNad::class) as $segment) {
    echo $segment->name(); // NAD
}

$message->filterSegments(MyNad::class, fn(Nad $seg): bool 
    => $seg->street() === 'Musterstr.'
);

echo $message->findFirstSegment(MyNad::class)->name(); // NAD

Unwrap Messages

foreach ($message->unwrap() as $partialMessage) {
    echo $segment instanceof \Apfelfrisch\Edifact\Message;
}

Add Readfilter

$message->addStreamFilter('iso-to-utf8', 'convert.iconv.ISO-8859-1.UTF-8');

Build a Message:

Build with default Una

use Apfelfrisch\Edifact\Builder;
use Apfelfrisch\Edifact\Message;
use My\Namespace\Segments\MyUnb;
use My\Namespace\Segments\MyUnh;

$builder = new Builder;

$builder->writeSegments(
    MyUnb::fromAttributes('1', '2', 'sender', '500', 'receiver', '400', new DateTime('2021-01-01 12:01:01'), 'unb-ref'),
    MyUnh::fromAttributes('unh-ref', 'type', 'v-no', 'r-no', 'o-no', 'o-co')
);

$message = new Message($builder->get());

UNA and the trailing Segments (UNT and UNZ) will be added automatically. If no UNA Segment is provided, it uses the default values [UNA:+.? ']., (*9)

Build with custom Una

use Apfelfrisch\Edifact\Builder;
use Apfelfrisch\Edifact\Segment\UnaSegment;

$builder = new Builder(new UnaSegment('|', '#', ',', '!', '_', '"'));

If you replace the decimal seperator, be sure that the blueprint marks the value as numeric., (*10)

Write directly into File

use Apfelfrisch\Edifact\Builder;
use Apfelfrisch\Edifact\Segment\UnaSegment;

$builder = new Builder(new UnaSegment, 'path/to/file.txt');

Add Writefilter to the Builder

use Apfelfrisch\Edifact\Builder;

$builder = new Builder;
$builder->addStreamFilter('utf8-to-iso', 'convert.iconv.UTF-8.ISO-8859-1');

Validate Message Segments

use Apfelfrisch\Edifact\Message;
use Apfelfrisch\Edifact\Validation\Failure;
use Apfelfrisch\Edifact\Validation\Validator;

$message = Message::fromString("UNA:+.? 'SEQ+9999", $segmentFactory);

$validator = new Validator;

if(! $validator->isValid($message)) {
    foreach ($validator->getFailures() as $failure) {
        echo $failure instanceof Failure;
    }
}

The Versions

22/06 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

20/10 2016

dev-jsonDescription

dev-jsonDescription

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

08/08 2016

0.2.2

0.2.2.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

08/08 2016

0.2.1

0.2.1.0

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

08/08 2016

0.2.0

0.2.0.0

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

08/08 2016

0.1.1

0.1.1.0

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

05/06 2016

0.1.0

0.1.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires