Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerDataProvider.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
21 {
25  private $customerRepository;
26 
30  private $serviceOutputProcessor;
31 
35  private $jsonSerializer;
36 
42  public function __construct(
43  CustomerRepositoryInterface $customerRepository,
44  ServiceOutputProcessor $serviceOutputProcessor,
45  SerializerInterface $jsonSerializer
46  ) {
47  $this->customerRepository = $customerRepository;
48  $this->serviceOutputProcessor = $serviceOutputProcessor;
49  $this->jsonSerializer = $jsonSerializer;
50  }
51 
59  public function getCustomerById(int $customerId) : array
60  {
61  try {
62  $customerObject = $this->customerRepository->getById($customerId);
63  } catch (NoSuchEntityException $e) {
64  // No error should be thrown, null result should be returned
65  return [];
66  }
67  return $this->processCustomer($customerObject);
68  }
69 
76  private function processCustomer(CustomerInterface $customerObject) : array
77  {
78  $customer = $this->serviceOutputProcessor->process(
79  $customerObject,
80  CustomerRepositoryInterface::class,
81  'get'
82  );
83  if (isset($customer['extension_attributes'])) {
84  $customer = array_merge($customer, $customer['extension_attributes']);
85  }
86  $customAttributes = [];
87  if (isset($customer['custom_attributes'])) {
88  foreach ($customer['custom_attributes'] as $attribute) {
89  $isArray = false;
90  if (is_array($attribute['value'])) {
91  $isArray = true;
92  foreach ($attribute['value'] as $attributeValue) {
93  if (is_array($attributeValue)) {
94  $customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
95  $attribute['value']
96  );
97  continue;
98  }
99  $customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
100  continue;
101  }
102  }
103  if ($isArray) {
104  continue;
105  }
106  $customAttributes[$attribute['attribute_code']] = $attribute['value'];
107  }
108  }
109  $customer = array_merge($customer, $customAttributes);
110 
111  return $customer;
112  }
113 }
$customer
Definition: customers.php:11
$customerRepository
__construct(CustomerRepositoryInterface $customerRepository, ServiceOutputProcessor $serviceOutputProcessor, SerializerInterface $jsonSerializer)