Symfony 3 - difference between 2 arraycollection

2019-07-20 08:30发布

问题:

Is there a way to differentiate 2 ArrayCollection ? (like array_diff)

today I loop on the first and check if $it->contains() match, but I think it can be refactored.

回答1:

You can use array_diff in the following way:

$diff = array_diff($arrayCollection1->toArray(), $arrayCollection2->toArray());

$arrayCollectionDiff = new ArrayCollection($diff);


回答2:

I would suggest the following:

A class made of two functions:

  • function getElementFromANotInB gives the elements from ArrayCollection A not in ArrayCollection B (can also be used the other way around to get elements from B not in A);
  • function getElementCommonInAAndB gives common elements of ArrayCollection A and B.

Here is the class:

<?php
namespace [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB;
use Doctrine\Common\Collections\ArrayCollection;
class ArrayCollectionTB {
    public function __construct(){}
    public function getElementFromANotInB(ArrayCollection $acA,ArrayCollection $acB){
        return $elementsInANotInB = $acA->filter(function($a) use ($acB) {
            return $acB->contains($a)===false;
        });
    }

    public function getElementCommonInAAndB(ArrayCollection $acA,ArrayCollection $acB){
        return $elementsCommonInAAndB = $acA->filter(function($a) use ($acB) {
            return $acB->contains($a)===true;
        });

    }
}
?>

Here is the test class that comes with it:

<?php
namespace Tests\[Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB;

use [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB\ArrayCollectionTB;
use Doctrine\Common\Collections\ArrayCollection;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @coversDefaultClass [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB
*/
class ArrayCollectionTBTest extends KernelTestCase{

    private static $acA;
    private static $acB;
    private static $acTB;

    public static function setUpBeforeClass()
    {
        //start the symfony kernel
        $kernel = static::createKernel();
        $kernel->boot();
        //get the DI container
        $container = $kernel->getContainer();

        self::$acA = new ArrayCollection();
        self::$acB = new ArrayCollection();

        //acA and acB have in common 1,5
        //acA has 2 and 3 not in acB
        self::$acA->add('element 1');
        self::$acA->add('element 2');
        self::$acA->add('element 3');
        self::$acA->add('element 5');
        //acB has 4 and 6 not in acA
        self::$acB->add('element 1');
        self::$acB->add('element 4');
        self::$acB->add('element 5');
        self::$acB->add('element 6');

        self::$acTB = new ArrayCollectionTB();
    }

    /**
    * @covers ::getElementFromANotInB
    * @testdox test check dif give element from ArrayCollection A not in ArrayCollection B
    */
    public function testGetElementFromANotInB(){
        $res = self::$acTB->getElementFromANotInB(self::$acA,self::$acB);
        //result should be  2 and 3
        $this->assertNotContains('element 1',$res);
        $this->assertContains('element 2',$res);
        $this->assertContains('element 3',$res);
        $this->assertNotContains('element 4',$res);
        $this->assertNotContains('element 5',$res);
        $this->assertNotContains('element 6',$res);


    }

    /**
    * @covers ::getElementFromANotInB
    * @testdox test check dif give element from ArrayCollection B not in ArrayCollection A
    */
    public function testGetElementFromBNotInA(){
        $res = self::$acTB->getElementFromANotInB(self::$acB,self::$acA);
        $this->assertNotContains('element 1',$res);
        $this->assertNotContains('element 2',$res);
        $this->assertNotContains('element 3',$res);
        $this->assertContains('element 4',$res);
        $this->assertNotContains('element 5',$res);
        $this->assertContains('element 6',$res);
    }

    /**
    * @covers ::getElementCommonInAAndB
    * @testdox test check gives element from ArrayCollection A and ArrayCollection A
    */
    public function testGetElementFromBAndA(){
        $res = self::$acTB->getElementCommonInAAndB(self::$acB,self::$acA);
        $this->assertContains('element 1',$res);
        $this->assertNotContains('element 2',$res);
        $this->assertNotContains('element 3',$res);
        $this->assertNotContains('element 4',$res);
        $this->assertContains('element 5',$res);
        $this->assertNotContains('element 6',$res);
    }
}
?>