> For the complete documentation index, see [llms.txt](https://dustinsimon.gitbook.io/encapsulation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dustinsimon.gitbook.io/encapsulation/v1.1/getting-started/intersection-calculation.md).

# 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&#x20;
* 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:

```php
$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:

```php
use Dustin\Encapsulation\IntersectionCalculation;

IntersectionCalculation::getFieldIntersection($product, $category);
// or
$product->getFieldIntersection($category);
```

{% code title="result" %}

```php
['name', 'metaTitle']
```

{% endcode %}

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:

```php
use Dustin\Encapsulation\IntersectionCalculation;

IntersectionCalculation::getFieldDifference($product, $category);
// or
$product->getFieldDifference($category);
```

{% code title="result" %}

```php
['productNumber', 'ean']
```

{% endcode %}

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

```php
IntersectionCalculation::getFieldDifference($category, $product);
// or
$category->getFieldDifference($product);
```

{% code title="result" %}

```php
['parentCategory', 'description']
```

{% endcode %}

`'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

```php
$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)
```

{% code title="result ($intersection)" %}

```php
NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002'
])
```

{% endcode %}

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

```php
$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);
```

{% code title="result ($intersection)" %}

```php
NestedEncapsulation([
    'name' => 'My cool product',
    'productNumber' => 'SW1002',
    'categories' => NestedEncapsulation([
        'My coolest products'
    ])
])
```

{% endcode %}

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

### Get difference

```php
$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
]);
```

```php
$differenceFirst = IntersectionCalculation::getDifference($firstProduct, $secondProduct);
// or
$differenceFirst = $firstProduct->getDifference($secondProduct);
```

{% code title="result ($differenceFirst)" %}

```php
NestedEncapsulation([
    'stock' => 100,
    'price' => 7.99
])
```

{% endcode %}

```php
$differenceSecond = IntersectionCalculation::getDifference($secondProduct, $firstProduct);
// or
$differenceSecond = $secondProduct->getDifference($firstProduct);
```

{% code title="result ($differenceSecond)" %}

```php
NestedEncapsulation([
    'stock' => 50,
    'price' => 4.8
])
```

{% endcode %}

Differences will always be in-depth too.&#x20;

See the following example:

```php
$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',
    ]
]);
```

```php
$differenceFirst = IntersectionCalculation::getDifference($first$product, $secondProduct);
// or
$differenceFirst = $firstProduct->getDifference($secondProduct);
```

{% code title="result ($differenceFirst)" %}

```php
NestedEncapsulation([
    'stock' => 100,
    'price' => 7.99,
    'categories' => NestedEncapsulation([
        'My fanciest products',
        'My topsellers'
    ])
])
```

{% endcode %}

```php
$differenceSecond  = IntersectionCalculation::getDifference($secondProduct, $firstProduct);
// or
$differenceSecond = $secondProduct->getDifference($firstProduct);
```

{% code title="result ($differenceSecond)" %}

```php
NestedEncapsulation([
    'stock' => 50,
    'price' => 4.69,
    'categories' => NestedEncapsulation([
        'My most cheapest products',
    ])
])
```

{% endcode %}
