public function testGetContentDataOrderOnlyContact() { $type = new ContactSelectionContentType($this->template, $this->contactManager->reveal(), $this->accountManager->reveal(), $this->serializer->reveal(), new CustomerIdConverter(), new IndexComparator()); $contact1 = $this->prophesize(Contact::class); $contact2 = $this->prophesize(Contact::class); $contact3 = $this->prophesize(Contact::class); $contact1->getId()->willReturn(1); $contact2->getId()->willReturn(2); $contact3->getId()->willReturn(3); $dataUnsorted = [$contact1->reveal(), $contact2->reveal(), $contact3->reveal()]; $data = [$contact2->reveal(), $contact1->reveal(), $contact3->reveal()]; $this->property->getValue()->willReturn(['c2', 'c1', 'c3']); $this->contactManager->getByIds([2, 1, 3], $this->locale)->willReturn($dataUnsorted); $this->accountManager->getByIds([], $this->locale)->willReturn([]); $this->serializer->serialize($data[0], 'array', Argument::type(SerializationContext::class))->willReturn($data[0]); $this->serializer->serialize($data[1], 'array', Argument::type(SerializationContext::class))->willReturn($data[1]); $this->serializer->serialize($data[2], 'array', Argument::type(SerializationContext::class))->willReturn($data[2]); $contacts = $type->getContentData($this->property->reveal()); $this->assertCount(3, $contacts); $this->assertEquals($contact2->reveal(), $contacts[0]); $this->assertEquals($contact1->reveal(), $contacts[1]); $this->assertEquals($contact3->reveal(), $contacts[2]); }
/** * {@inheritdoc} */ public function getContentData(PropertyInterface $property) { $value = $property->getValue(); $locale = $property->getStructure()->getLanguageCode(); if ($value === null || !is_array($value) || count($value) === 0) { return []; } $ids = $this->converter->convertIdsToGroupedIds($value, ['a' => [], 'c' => []]); $accounts = $this->accountManager->getByIds($ids['a'], $locale); $contacts = $this->contactManager->getByIds($ids['c'], $locale); $result = array_merge($accounts, $contacts); @usort($result, function ($a, $b) use($value) { $typeA = $a instanceof Contact ? 'c' : 'a'; $typeB = $b instanceof Contact ? 'c' : 'a'; return $this->comparator->compare($typeA . $a->getId(), $typeB . $b->getId(), $value); }); return array_map(function ($entity) { $groups = ['fullContact', 'partialAccount']; if ($entity instanceof Account) { $groups = ['fullAccount', 'partialContact']; } return $this->serializer->serialize($entity, 'array', SerializationContext::create()->setGroups($groups)->setSerializeNull(true)); }, $result); }