Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerMetadata.php
Go to the documentation of this file.
1 <?php
8 
15 
20 {
24  private $customerDataObjectMethods;
25 
29  private $attributeMetadataConverter;
30 
34  private $attributeMetadataDataProvider;
35 
40  public function __construct(
41  AttributeMetadataConverter $attributeMetadataConverter,
42  AttributeMetadataDataProvider $attributeMetadataDataProvider
43  ) {
44  $this->attributeMetadataConverter = $attributeMetadataConverter;
45  $this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
46  }
47 
51  public function getAttributes($formCode)
52  {
53  $attributes = [];
54  $attributesFormCollection = $this->attributeMetadataDataProvider->loadAttributesCollection(
55  self::ENTITY_TYPE_CUSTOMER,
56  $formCode
57  );
58  foreach ($attributesFormCollection as $attribute) {
60  $attributes[$attribute->getAttributeCode()] = $this->attributeMetadataConverter
61  ->createMetadataAttribute($attribute);
62  }
63  if (empty($attributes)) {
64  throw NoSuchEntityException::singleField('formCode', $formCode);
65  }
66  return $attributes;
67  }
68 
72  public function getAttributeMetadata($attributeCode)
73  {
75  $attribute = $this->attributeMetadataDataProvider->getAttribute(self::ENTITY_TYPE_CUSTOMER, $attributeCode);
76  if ($attribute && ($attributeCode === 'id' || $attribute->getId() !== null)) {
77  $attributeMetadata = $this->attributeMetadataConverter->createMetadataAttribute($attribute);
78  return $attributeMetadata;
79  } else {
80  throw new NoSuchEntityException(
81  __(
82  'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
83  [
84  'fieldName' => 'entityType',
85  'fieldValue' => self::ENTITY_TYPE_CUSTOMER,
86  'field2Name' => 'attributeCode',
87  'field2Value' => $attributeCode
88  ]
89  )
90  );
91  }
92  }
93 
97  public function getAllAttributesMetadata()
98  {
100  $attributeCodes = $this->attributeMetadataDataProvider->getAllAttributeCodes(
101  self::ENTITY_TYPE_CUSTOMER,
102  self::ATTRIBUTE_SET_ID_CUSTOMER
103  );
104 
105  $attributesMetadata = [];
106 
107  foreach ($attributeCodes as $attributeCode) {
108  try {
109  $attributesMetadata[] = $this->getAttributeMetadata($attributeCode);
110  } catch (NoSuchEntityException $e) {
111  //If no such entity, skip
112  }
113  }
114 
115  return $attributesMetadata;
116  }
117 
121  public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_INTERFACE_NAME)
122  {
123  $customAttributes = [];
124  if (!$this->customerDataObjectMethods) {
125  $dataObjectMethods = array_flip(get_class_methods($dataObjectClassName));
126  $baseClassDataObjectMethods = array_flip(
127  get_class_methods(\Magento\Framework\Api\AbstractExtensibleObject::class)
128  );
129  $this->customerDataObjectMethods = array_diff_key($dataObjectMethods, $baseClassDataObjectMethods);
130  }
131  foreach ($this->getAllAttributesMetadata() as $attributeMetadata) {
132  $attributeCode = $attributeMetadata->getAttributeCode();
134  $isDataObjectMethod = isset($this->customerDataObjectMethods['get' . $camelCaseKey])
135  || isset($this->customerDataObjectMethods['is' . $camelCaseKey]);
136 
138  if (!$isDataObjectMethod
139  && (!$attributeMetadata->isSystem() || $attributeCode == 'disable_auto_group_change')
140  ) {
141  $customAttributes[] = $attributeMetadata;
142  }
143  }
144  return $customAttributes;
145  }
146 }
getCustomAttributesMetadata($dataObjectClassName=self::DATA_INTERFACE_NAME)
__()
Definition: __.php:13
__construct(AttributeMetadataConverter $attributeMetadataConverter, AttributeMetadataDataProvider $attributeMetadataDataProvider)
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13