有没有办法可以简化这个?

时间:2014-12-31 21:12:38

标签: php laravel collections eloquent

我正在使用Laravel Eloquent Collection过滤器。我很确定这可以简化,但我不确定。我在这段代码之后使用了所有变量,但我想重构,所以它更好。

    $cards = new Collection($data);

    $whiteBlueBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Red","Green"]') { return true; }});

    $whiteBlueBlackRed = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Red"]') { return true; }});
    $whiteBlueBlackGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black","Green"]') { return true; }});
    $whiteBlueRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Red","Green"]') { return true; }});
    $whiteBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Black","Red","Green"]') { return true; }});
    $blueBlackRedGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Red","Green"]') { return true; }});

    $whiteBlueBlack = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Black"]') { return true; }});
    $whiteBlueRed = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Red"]') { return true; }});
    $whiteBlueGreen = $cards->filter(function($card) { if ($card->colors == '["White","Blue","Green"]') { return true; }});
    $whiteBlackRed = $cards->filter(function($card) { if ($card->colors == '["White","Black","Red"]') { return true; }});
    $whiteBlackGreen = $cards->filter(function($card) { if ($card->colors == '["White","Black","Green"]') { return true; }});
    $whiteRedGreen = $cards->filter(function($card) { if ($card->colors == '["White","Red","Green"]') { return true; }});
    $blueBlackRed = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Red"]') { return true; }});
    $blueBlackGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Black","Green"]') { return true; }});
    $blueRedGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Red","Green"]') { return true; }});
    $blackRedGreen = $cards->filter(function($card) { if ($card->colors == '["Black","Red","Green"]') { return true; }});

    $whiteBlue = $cards->filter(function($card) { if ($card->colors == '["White","Blue"]') { return true; }});
    $whiteBlack = $cards->filter(function($card) { if ($card->colors == '["White","Black"]') { return true; }});
    $whiteRed = $cards->filter(function($card) { if ($card->colors == '["White","Red"]') { return true; }});
    $whiteGreen = $cards->filter(function($card) { if ($card->colors == '["White","Green"]') { return true; }});
    $blueBlack = $cards->filter(function($card) { if ($card->colors == '["Blue","Black"]') { return true; }});
    $blueRed = $cards->filter(function($card) { if ($card->colors == '["Blue","Red"]') { return true; }});
    $blueGreen = $cards->filter(function($card) { if ($card->colors == '["Blue","Green"]') { return true; }});
    $blackRed = $cards->filter(function($card) { if ($card->colors == '["Black","Red"]') { return true; }});
    $blackGreen = $cards->filter(function($card) { if ($card->colors == '["Black","Green"]') { return true; }});
    $redGreen = $cards->filter(function($card) { if ($card->colors == '["Red","Green"]') { return true; }});

    $white = $cards->filter(function($card) { if ($card->colors == '["White"]') { return true; }});
    $blue = $cards->filter(function($card) { if ($card->colors == '["Blue"]') { return true; }});
    $black = $cards->filter(function($card) { if ($card->colors == '["Black"]') { return true; }});
    $red = $cards->filter(function($card) { if ($card->colors == '["Red"]') { return true; }});
    $green = $cards->filter(function($card) { if ($card->colors == '["Green"]') { return true; }});

1 个答案:

答案 0 :(得分:2)

嗯,你可以稍微分解一下过滤:

function getCards($cards, $colors) {
    return $cards->filter(function($card) use ($colors) {
        if ($card->colors == $colors) { return true; }
    });
}

$whiteBlueBlackRedGreen = getCards($cards, '["White","Blue","Black","Red","Green"]');
// and so on...

这样可以避免大量重复输入并使代码更易于阅读。


深入兔子洞......

$cards = new Collection($data);

function getCards($cards, $colors) {
    return $cards->filter(function($card) use ($colors) {
        if ($card->colors == $colors) { return true; }
    });
}

$colors = array("White", "Blue", "Black", "Red", "Green");
$coloredCards = array();
foreach($colors as $color) {
    foreach($coloredCards as $existingCombo => $existingCards) {
        $newKey = $existingCombo . "-" . $color;
        $coloredCards[$newKey] = getCards($cards, json_encode(explode("-", $newKey)));
    }
    $coloredCards[$color] = getCards($cards, '["' . $color . '"]');
}

我实际上没有对此进行测试,但它应该为您提供一个包含$coloredCards['White-Blue-Black']等键的数组,其中包含您的变量所具有的内容。

编辑:我测试了一个实际上没有调用getCards的版本,以确保它是正确的组合集 - 您可以在此处查看测试:http://ideone.com/Uk6H5x