Intersection calculation

dustin/encapsulation brings intersection calculation which allows to figure out

  • which fields two encapsulations have in common

  • which fields differ between two encapsulations

  • which data two encapsulations have in common

  • which data differ between two encapsulations

Dustin\Encapsulation\IntersectionCalculation can do each of the operations from above with static method calls. But you can also use intersection calculation directly with your encapsulation objects. See the sections below if you want to know how.

Compare fields

Comparing fields will only check the existance of a field but not the containing value.

For our example we use the following two objects:

$product = new Encapsulation([
    'name' => 'My product',
    'productNumber' => 'SW1002',
    'metaTitle' => 'My cool product',
    'ean' => 'my ean'
]);

$category = new Encapsulation([
    'name' => 'My category',
    'metaTitle' => 'My cool category',
    'parentCategory' => 'My cool products',
    'description' => 'These are my coolest products'
]);

Get field intersection

Get a list of fields which two encapsulations have in common:

use Dustin\Encapsulation\IntersectionCalculation;

IntersectionCalculation::getFieldIntersection($product, $category);
// or
$product->getFieldIntersection($category);
result
['name', 'metaTitle']

Both fields appear in both encapsulations.

Get field difference

Get a list of fields which are available in the first encapsulation but not the second one:

use Dustin\Encapsulation\IntersectionCalculation;

IntersectionCalculation::getFieldDifference($product, $category);
// or
$product->getFieldDifference($category);
result
['productNumber', 'ean']

'productNumber' and 'ean' are available in $product but not in $category.

IntersectionCalculation::getFieldDifference($category, $product);
// or
$category->getFieldDifference($product);
result
['parentCategory', 'description']

'parentCategory' and 'description' are available in $category but not in $product.

Compare fields and their values

Intersection calculation can compare the data of two encapsulations. The result will always be a new encapsulation object representing the intersection or the difference. The following rules take effect:

  • Intersections or differences will always be in-depth which means inner collections like arrays and encapsulations will also be compared.

  • Inner collections will be compared together regardless of their type. No distinction is made between arrays or encapsulations.

  • Inner collections always result in a new encapsulation object within the result object.

Get intersection

$firstProduct = new NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 100,
    'price' => 7.99
]);

$secondProduct = new NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 50,
    'price' => 4.89
]);

$intersection = IntersectionCalculation::getIntersection($firstProduct, $secondProduct);
// or
$intersection = $firstProduct->getIntersection($secondProduct)
result ($intersection)
NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002'
])

The next example shows intersection calculation with inner collections like arrays and encapsulations:

$firstProduct = new Encapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 100,
    'price' => 7.99,
    'categories' => [
        'My coolest products',
        'My fanciest products',
        'My topsellers'
    ]
]);

$secondProduct = new Encapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 50,
    'price' => 4.69,
    'categories' => [
        'My coolest products',
        'My most cheapest products',
    ]
]);

$intersection = IntersectionCalculation::getIntersection($firstProduct, $secondProduct);
// or
$intersection = $firstProduct->getIntersection($secondProduct);
result ($intersection)
NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'categories' => NestedEncapsulation([
        'My coolest products'
    ])
])

As you can see inner collections like arrays and encapsulations will be compared together and result in a new NestedEncapsulation object.

Get difference

$firstProduct = new NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 100,
    'price' => 7.99
]);

$secondProduct = new NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 50,
    'price' => 4.89
]);
$differenceFirst = IntersectionCalculation::getDifference($firstProduct, $secondProduct);
// or
$differenceFirst = $firstProduct->getDifference($secondProduct);
result ($differenceFirst)
NestedEncapsulation([
    'stock' => 100,
    'price' => 7.99
])
$differenceSecond = IntersectionCalculation::getDifference($secondProduct, $firstProduct);
// or
$differenceSecond = $secondProduct->getDifference($firstProduct);
result ($differenceSecond)
NestedEncapsulation([
    'stock' => 50,
    'price' => 4.8
])

Differences will always be in-depth too.

See the following example:

$firstProduct = new Encapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 100,
    'price' => 7.99,
    'categories' => [
        'My coolest products',
        'My fanciest products',
        'My topsellers'
    ]
]);

$secondProduct = new Encapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'stock' => 50,
    'price' => 4.69,
    'categories' => [
        'My coolest products',
        'My most cheapest products',
    ]
]);
$differenceFirst = IntersectionCalculation::getDifference($first$product, $secondProduct);
// or
$differenceFirst = $firstProduct->getDifference($secondProduct);
result ($differenceFirst)
NestedEncapsulation([
    'stock' => 100,
    'price' => 7.99,
    'categories' => NestedEncapsulation([
        'My fanciest products',
        'My topsellers'
    ])
])
$differenceSecond  = IntersectionCalculation::getDifference($secondProduct, $firstProduct);
// or
$differenceSecond = $secondProduct->getDifference($firstProduct);
result ($differenceSecond)
NestedEncapsulation([
    'stock' => 50,
    'price' => 4.69,
    'categories' => NestedEncapsulation([
        'My most cheapest products',
    ])
])

Last updated