我想将新的segmentId(具有相同的名称)添加到映射数组中,但具有不同的elementId但具有相同的方法


14

以下是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。也许添加一个新功能并使用这种条件?

新文件答案***

    <?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()];
    }

}

因此,您希望MAPPING常量数组是动态的吗?你不能用const做到这一点。您可以使用另一个函数来获取该数组并根据需要进行修改
dWinder

我真的不知道你要做什么。您想实现什么?
Stephan Vierkant

Answers:


6

正如你可以看到在这里 - 常量变量不能改变或保持逻辑。请注意,接口也不能保存逻辑-因此您不能在接口中做到这一点。

我认为针对您的问题的更好解决方案是使用抽象类。我将与您的界面相同(您可以在此处看到有关不同之处的讨论,但我认为满足您的需求将是相同的)。

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

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公开并直接访问它,但我想更好的编程方法是使用getter方法。

希望有帮助!


我应该能够在最后一个公共函数map(array $ segments)下方的同一文件中集成/添加整个抽象类:OrderUpdateInterface; } 这里
辛格尔顿

所以现在我可以重写所有旧代码并使用此抽象类?我将答案标记为正确,对我的朋友非常有帮助。@dWinder
辛格尔顿

是的你可以。接口和抽象类之间有区别,但是在大多数情况下,它的行为相同(您可以在文章开头的链接中了解它)。
dWinder

我认为在逻辑上我仍然需要添加此正确内容?否则if($ this-> mapToProcess ['LIN'] ['LIN04'] =='VN')$ this-> mapToProcess ['LIN'] ['LIN05'] ='processSku';
Singleton

1
您也应该添加它。我仅将其中一些作为逻辑应该放在哪里的示例。我也将对其进行编辑以使代码涵盖其中
dWinder

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

将输出:

0 => 1
1 => 2
2 => 3

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.