gpt4 book ai didi

php - 我想在我的映射数组中添加一个新的 segmentId(同名),但 elementId 不同但方法相同

转载 作者:行者123 更新时间:2023-12-02 11:27:19 24 4
gpt4 key购买 nike

下面是 MapperInterface.php

我试图弄清楚如何将 if-else 语句添加到 const 中。映射数组。像这样:

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;
}

我希望这是有道理的。不确定是否有更好的方法来解决它,但最终我需要超过 1 个“LIN”segmentId。也许添加一个新功能并使用这个条件?

新文件 回答 ***
    <?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()];
}

}

最佳答案

如您所见here - const 变量不能更改或保持逻辑 .
请注意,界面也不能保存逻辑 - 因此您不能在界面中执行此操作。

我认为解决您的问题的更好方法是使用 abstract class .我将与您的界面相同(您可以看到关于不同 here 的讨论,但我认为它会满足您的需求)。

我建议这样创建抽象类:

abstract class AbstractMapper{
// here add all the method from your 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'];

// 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']['LIN03'] = 'processSku';
else if ($this->mapToProcess['LIN']['LIN04'] == 'NV')
$this->mapToProcess['LIN']['LIN05'] = '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'));

请注意,您的逻辑似乎没有改变,所以我放置了新变量并在构造过程中设置它。如果你愿意,你可以转储它并修改 getProcess 的返回值。功能 - 把所有的逻辑放在那里。

另一种选择是制作 $mapToProcess public 并直接访问它,但我想更好的编程是使用 getter 方法。

希望有帮助!

关于php - 我想在我的映射数组中添加一个新的 segmentId(同名),但 elementId 不同但方法相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59730379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com