Beispiel #1
0
 /** @test */
 public function should_load_messages_from_file()
 {
     $loader = new MappingLoader(__DIR__ . '/../fixtures');
     $result = $loader->loadMessage('D', '96A', 'MESSAGE');
     $this->assertInstanceOf(MessageMapping::class, $result);
     $defaults = $result->getDefaults();
     $this->assertEquals('INVOIC', $defaults['0065']);
     $this->assertEquals('D', $defaults['0052']);
     $this->assertEquals('96A', $defaults['0054']);
     $this->assertEquals('UN', $defaults['0051']);
     $segments = $result->getSegments();
     $this->assertInstanceOf(MessageSegmentMapping::class, $segments[0]);
     $this->assertEquals('UNH', $segments[0]->getCode());
     $this->assertEquals(1, $segments[0]->getMaxRepeat());
     $this->assertEquals(true, $segments[0]->isRequired());
     $this->assertInstanceOf(MessageSegmentGroupMapping::class, $segments[7]);
     $this->assertEquals('SG1', $segments[7]->getCode());
     $this->assertEquals(99, $segments[7]->getMaxRepeat());
     $this->assertEquals(false, $segments[7]->isRequired());
     $groupSegments = $segments[7]->getSegmentMappings();
     $this->assertInstanceOf(MessageSegmentMapping::class, $groupSegments[0]);
     $this->assertEquals('RFF', $groupSegments[0]->getCode());
     $this->assertEquals(1, $groupSegments[0]->getMaxRepeat());
     $this->assertEquals(true, $groupSegments[0]->isRequired());
 }
Beispiel #2
0
 public function encode(Interchange $interchange)
 {
     $out = $this->annotationPrinter->prepareString($interchange);
     foreach ($interchange->getMessages() as $message) {
         $out .= $this->annotationPrinter->prepareString($message);
         $identifier = $message->getIdentifier();
         $this->segmentPrinter->setSegmentMappings($this->mappingLoader->loadSegments($identifier['version'], $identifier['release']));
         foreach ($message->getSegments() as $segment) {
             $out .= $this->segmentPrinter->prepareString($segment);
         }
         $out .= $this->annotationPrinter->prepareString($message->createTrailer());
     }
     $out .= $this->annotationPrinter->prepareString($interchange->createTrailer());
     return $out;
 }
Beispiel #3
0
 /**
  * @param array $data
  * @return Message[]
  * @throws \EDI\Exception\MandatorySegmentPieceMissing
  */
 public function populate(&$data)
 {
     $messages = [];
     $remainingData = null;
     $segment = $this->getNextSegment($data);
     while ($segment && $segment->getCode() == 'UNH') {
         $message = new Message();
         $this->fillProperties($message, $segment);
         $identifier = $message->getIdentifier();
         $mapping = $this->mappingLoader->loadMessage($identifier['version'], $identifier['release'], $identifier['type']);
         $this->segmentPopulator->setSegmentConfig($this->mappingLoader->loadSegments($identifier['version'], $identifier['release']));
         $expectedSegments = $mapping->getSegments();
         $segment = $this->getNextSegment($data);
         $this->nextExpectedSegment($expectedSegments);
         //  Ignore the header, already processed when creating Message
         $expected = $this->nextExpectedSegment($expectedSegments);
         while ('UNT' != $segment->getCode()) {
             /** @var MessageSegmentMapping $expected */
             if (!$expected) {
                 break;
             }
             if ($expected->acceptSegment($segment)) {
                 $segment = $this->getNextSegment($data);
             } else {
                 $message->addSegments($expected->getSegments());
                 $expected = $this->nextExpectedSegment($expectedSegments);
             }
         }
         if ($expected && $expected->hasSegments()) {
             $message->addSegments($expected->getSegments());
         }
         $trailer = new MessageTrailer();
         $this->fillProperties($trailer, $segment);
         $message->setTrailer($trailer);
         $messages[] = $message;
         $remainingData = reset($data);
         $segment = $this->getNextSegment($data);
     }
     if ($remainingData) {
         array_unshift($data, $remainingData);
     }
     return $messages;
 }
Beispiel #4
0
 /**
  * @param $version
  * @return Populator
  * @throws MappingNotFoundException
  */
 protected function updatePopulatorConfiguration($version)
 {
     $dir = realpath($this->mappingDirectory . "/{$version}");
     if (!file_exists($dir)) {
         throw new MappingNotFoundException(sprintf("Mapping directory '%s' not found", $this->mappingDirectory));
     }
     $segmentsFile = $dir . '/segments.xml';
     if (!file_exists($segmentsFile) || !is_readable($segmentsFile)) {
         throw new MappingNotFoundException(sprintf("segments.xml mapping file not found in directory '%s'", $this->mappingDirectory));
     }
     $this->populator->setSegments($this->mappingLoader->loadSegments($segmentsFile));
     $codesFile = $dir . '/codes.xml';
     if (!file_exists($codesFile) || !is_readable($codesFile)) {
         throw new MappingNotFoundException(sprintf("codes.xml mapping file not found in directory '%s'", $this->mappingDirectory));
     }
     $this->populator->setCodes($this->mappingLoader->loadCodes($codesFile));
 }