我想将一个新的 segmentId(同名)添加到我的映射数组中,但使用不同的 elementId 但方法相同

Sin*_*ton 15 php arrays mapping constants

下面是 MapperInterface.php

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

if (LIN02 == “VN”) 
o   Treat LIN03 as the SKU
·         else if (LIN04 == “VN”) 
o   Treat LIN05 as the SKU
Run Code Online (Sandbox Code Playgroud)
<?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;
}
Run Code Online (Sandbox Code Playgroud)

我希望这是有道理的。不确定是否有更好的方法来解决它,但最终我需要 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()];
    }

}
Run Code Online (Sandbox Code Playgroud)
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()];
    }

}
Run Code Online (Sandbox Code Playgroud)

dWi*_*der 6

正如你在这里看到的- const 变量不能改变或保持逻辑。请注意,接口也不能保存逻辑 - 所以你不能在你的接口中这样做。

我认为解决您的问题的更好方法是使用抽象类。我会和你的界面一样(你可以在这里看到关于不同的讨论,但我认为它会满足你的需求)。

我建议像这样创建抽象类:

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

}
Run Code Online (Sandbox Code Playgroud)

现在您可以将继承的对象声明为:

class Obj extends AbstractMapper {
    // notice that as interface it need to implement all the abstract methods
    public function getMapping() : array {
        return [];
    }
}
Run Code Online (Sandbox Code Playgroud)

使用示例是:

$obj  = New Obj();
print_r($obj->getProcess('LIN', 'LIN01'));
Run Code Online (Sandbox Code Playgroud)

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

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

希望有帮助!


Kar*_*lis 5

您不能在常量定义中添加 if-else 语句。最接近你正在寻找的可能是这样的:

const A = 1;
const B = 2;

// Value of C is somewhat "more dynamic" and depends on values of other constants
const C = self::A == 1 ? self::A + self::B : 0;

// MAPPING array inherits "more dynamic" properties of C
const MAPPING = [
    self::A,
    self::B,
    self::C,
];
Run Code Online (Sandbox Code Playgroud)

将输出:

0 => 1
1 => 2
2 => 3
Run Code Online (Sandbox Code Playgroud)

换句话说,您需要将数组分解为单独的常量,然后进行所有条件定义,然后根据结果常量值构造最终的 MAPPING 数组。