下面是MapperInterface.php
我试图弄清楚如何在const中添加if-else语句。映射数组。像这样:
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;
}
我希望这是有道理的。不知道是否有更好的方法可以解决此问题,但最终我需要多个“ LIN” segmentId。也许添加一个新功能并使用这种条件?
您的方法没有意义,因为:
嗯,您有两种方法可以解决此任务:
如您所见,here-const变量不能更改或保持逻辑。
请注意,该接口无法保存逻辑-因此您可以在接口中进行操作。
我认为针对您的问题的更好解决方案是使用abstract class。我将与您的界面相同(您可以看到有关不同的here的讨论,但我认为对于您的需求它将是相同的)。
我建议以此方式创建抽象类:
abstract class AbstractMapper{
// here add all the method from your interface as abstract
public abstract function getMapping(): array;
// set your default mapping - notice those are private to disable access from outside
private const MAPPING = ['LIN' => ['LIN02' => 'NV', 'LIN01' => 'processSku'], 'PRF' => ['PRF01' => 'processIncrementId']];
private $mapToProcess = [];
// when initiate this class modify your $mapping member according your logic
function __construct() {
$this->mapToProcess = self::MAPPING; // init as
if ($this->mapToProcess['LIN']['LIN02'] == 'NV')
$this->mapToProcess['LIN']['LIN04'] = 'processSku';
}
// use method to get your process and don't use directly 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 [];
}
}
使用示例为:
$obj = New Obj();
print_r($obj->getProcess('LIN', 'LIN01'));
希望有帮助!