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.
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.