-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProductIndexDocument.php
66 lines (58 loc) · 1.91 KB
/
ProductIndexDocument.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace App\Elasticsearch\Index\Product\Document;
use App\Elasticsearch\Index\Product\ProductIndex;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\Element\AbstractElement;
use Valantic\ElasticaBridgeBundle\Document\AbstractTenantAwareDocument;
use Valantic\ElasticaBridgeBundle\Document\DataObjectNormalizerTrait;
use Valantic\ElasticaBridgeBundle\Enum\DocumentType;
/**
* @extends AbstractTenantAwareDocument<Product>
*/
class ProductIndexDocument extends AbstractTenantAwareDocument
{
/** @use DataObjectNormalizerTrait<Product> */
use DataObjectNormalizerTrait;
public function getType(): DocumentType
{
return DocumentType::DATA_OBJECT;
}
public function getSubType(): ?string
{
return Product::class;
}
public function getNormalized(AbstractElement $element): array
{
return [
...$this->plainAttributes(
$element,
[
'sku',
'created_at' => fn (Product $product) => date('r', $product->getCreationDate()),
]
),
...$this->localizedAttributes(
$element,
[
'name',
'url',
'slug' => fn (Product $product, string $locale): string => str_replace(' ', '-', $product->getName()),
]
),
...$this->relationAttributes(
$element,
[
ProductIndex::ATTRIBUTE_CATEGORIES => 'categories',
'relatedProducts' => fn (Product $product) => $product->getRelatedProducts(),
]
),
...$this->children($element),
...$this->childrenRecursive($element),
];
}
public function shouldIndex(AbstractElement $element): bool
{
return $element->isPublished();
}
}