PHP code example of proengeno / edifact

1. Go to this page and download the library: Download proengeno/edifact library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

proengeno / edifact example snippets


use Apfelfrisch\Edifact\StreamMessageFactory;

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

$factory->markAsDefault();

$message = $factory->fromString("UNA:+.? 'SEQ+1");

use Apfelfrisch\Edifact\Segment\GenericSegment;
use Apfelfrisch\Edifact\StreamMessageFactory;

$factory = new StreamMessageFactory;
$factory->addFallback(GenericSegment::class);
$factory->markAsDefault();

use Apfelfrisch\Edifact\Message;

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

use Apfelfrisch\Edifact\Message;

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

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

use My\Namespace\Segments\MyNad;

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

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

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

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

foreach ($message->unwrap('HDR', 'TRL') as $partialMessage) {
    // custom header and trailer
}

use Apfelfrisch\Edifact\Iterators\Stream\Stream;
use Apfelfrisch\Edifact\StreamMessageFactory;

$factory = new StreamMessageFactory;
$stream = new Stream('path/to/interchange.txt');

foreach ($stream->unwrap() as $partialStream) {
    $partialMessage = $factory->fromStream($partialStream);
}

use Apfelfrisch\Edifact\StreamMessageFactory;

$factory = new StreamMessageFactory;
$factory->addStreamFilter('convert.iconv.ISO-8859-1.UTF-8');

use Apfelfrisch\Edifact\Builder;
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'),
);

$stream = $builder->get();

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

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

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

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

use Apfelfrisch\Edifact\Builder;

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

use Apfelfrisch\Edifact\Message;
use Apfelfrisch\Edifact\Validation\InterchangeValidator;

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

foreach (new InterchangeValidator()->validate($message) as $failure) {
    echo $failure->errorCode->value;    // e.g. 28
    echo $failure->description();       // "References do not match"
    echo $failure->serviceSegment;      // e.g. "UNT"
    echo $failure->messageCounter;      // n-th message of the interchange
    echo $failure->segmentPosition;     // position within the message, UNH = 1
}

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

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

$validator = new Validator;

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