Skip to content

Commit 98a9c36

Browse files
committed
WIP: GetCustomAttributeCodes service
1 parent 7d4523c commit 98a9c36

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Model\Entity;
8+
9+
10+
use Magento\Framework\Api\MetadataServiceInterface;
11+
12+
class GetCustomAttributeCodes
13+
{
14+
/**
15+
* @var string[]
16+
*/
17+
private $customAttributesCodes;
18+
19+
/**
20+
* Receive a list of custom EAV attributes using provided metadata service. The results are cached per entity type
21+
*
22+
* @param MetadataServiceInterface $metadataService Custom attribute metadata service to be used
23+
* @param string[] $interfaceAttributes Attribute codes that are part of the interface and should not be
24+
* considered custom
25+
* @param string|null $entityType Entity type (class name), only needed if metadata service handles different
26+
* entities
27+
* @return string[]
28+
*/
29+
public function execute(
30+
MetadataServiceInterface $metadataService,
31+
array $interfaceAttributes,
32+
string $entityType = null
33+
): array {
34+
$cacheKey = get_class($metadataService) . '|' . $entityType;
35+
if (!isset($this->customAttributesCodes[$cacheKey])) {
36+
$customAttributesCodes = $this->getEavAttributesCodes($metadataService, $entityType);
37+
$this->customAttributesCodes[$cacheKey] = array_values(
38+
array_diff($customAttributesCodes, $interfaceAttributes)
39+
);
40+
}
41+
return $this->customAttributesCodes;
42+
}
43+
44+
/**
45+
* Receive a list of EAV attributes using provided metadata service.
46+
*
47+
* @param MetadataServiceInterface $metadataService
48+
* @param string|null $entityType
49+
* @return string[]
50+
*/
51+
private function getEavAttributesCodes(MetadataServiceInterface $metadataService, string $entityType = null)
52+
{
53+
$attributeCodes = [];
54+
$customAttributesMetadata = $metadataService->getCustomAttributesMetadata($entityType);
55+
if (is_array($customAttributesMetadata)) {
56+
/** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */
57+
foreach ($customAttributesMetadata as $attribute) {
58+
$attributeCodes[] = $attribute->getAttributeCode();
59+
}
60+
}
61+
return $attributeCodes;
62+
}
63+
}

0 commit comments

Comments
 (0)