Unten ist die MapperInterface.php
Ich versuche herauszufinden, wie man der const eine if-else-Anweisung hinzufügt. Mapping-Array. So etwas wie das:
if (LIN02 == “VN”)
o Treat LIN03 as the SKU
· else if (LIN04 == “VN”)
o Treat LIN05 as the SKU
<?php
declare(strict_types=1);
namespace Direct\OrderUpdate\Api;
use Direct\OrderUpdate\Api\OrderUpdateInterface;
/**
* Interface MapperInterface
* Translates parsed edi file data to a \Direct\OrderUpdate\Api\OrderUpdateInterface
* @package Direct\OrderUpdate\Api
*/
interface MapperInterface
{
/**
* Mapping array formatted as MAPPING[segemntId][elemntId] => methodNameToProcessTheValueOfElement
* @var array
*/
const MAPPING = [
'DTM' => ['DTM02' => 'processCreatedAt'], // shipment.created_at
'PRF' => ['PRF01' => 'processIncrementId'], // order.increment_id
'LIN' => ['LIN05' => 'processSku'], // shipment.items.sku
'SN1' => ['SN102' => 'processQty'], // shipment.items.qty
'REF' => ['REF02' => 'processTrack'] // shipment.tracks.track_number, shipment.tracks.carrier_code
];
/**
* Mapping for carrier codes
* @var array
*/
const CARRIER_CODES_MAPPING = ['FED' => 'fedex'];
/**
* @return array
*/
public function getMapping(): array;
/**
* @param array $segments
* @return OrderUpdateInterface
*/
public function map(array $segments): OrderUpdateInterface;
}
Ich hoffe das ergibt Sinn. Ich bin mir nicht sicher, ob es einen besseren Weg gibt, aber letztendlich brauche ich mehr als 1 "LIN" -Segment-ID. Vielleicht eine neue Funktion hinzufügen und diese Bedingung verwenden?
NEUE DATEI ANTWORT ***
<?php
declare(strict_types=1);
namespace Direct\OrderUpdate\Api;
use Direct\OrderUpdate\Api\OrderUpdateInterface;
/**
* Abstract Mapper
* Translates parsed edi file data to a \Direct\OrderUpdate\Api\OrderUpdateInterface
* @package Direct\OrderUpdate\Api
*/
abstract class AbstractMapper{
// Here we add all the methods from our interface as abstract
public abstract function getMapping(): array;
public abstract function map(array $segments): OrderUpdateInterface;
// The const here will behave the same as in the interface
const CARRIER_CODES_MAPPING = ['FED' => 'fedex'];
// We will set our default mapping - notice these are private to disable access from outside
private const MAPPING = ['LIN' => [
'LIN02' => 'VN',
'LIN01' => 'processSku'],
'PRF' => ['PRF01' => 'processIncrementId'],
'DTM' => ['DTM02' => 'processCreatedAt'],
'SN1' => ['SN102' => 'processQty'],
'REF' => ['REF02' => 'processTrack']];
private $mapToProcess = [];
// When we initiate this class we modify our $mapping member according to our new logic
function __construct() {
$this->mapToProcess = self::MAPPING; // init as
if ($this->mapToProcess['LIN']['LIN02'] == 'VN')
$this->mapToProcess['LIN']['LIN03'] = 'processSku';
else if ($this->mapToProcess['LIN']['LIN04'] == 'VN')
$this->mapToProcess['LIN']['LIN05'] = 'processSku';
}
// We use this method to get our process and don't directly use the map
public function getProcess($segemntId, $elemntId) {
return $this->mapToProcess[$segemntId][$elemntId];
}
}
class Obj extends AbstractMapper {
// notice that as interface it need to implement all the abstract methods
public function getMapping() : array {
return [$this->getMapping()];
}
public function map() : array {
return [$this->map()];
}
}
class Obj extends AbstractMapper {
// notice that as interface it need to implement all the abstract methods
public function getMapping() : array {
return [$this->getMapping()];
}
public function map() : array {
return [$this->map()];
}
}