18 private const MAX_CNT = 20;
20 private int $currentUserId;
24 parent::__construct($config);
26 $this->currentUserId = (int)CurrentUser::get()?->getId();
31 $normalizedAddress = $this->getNormalizedAddress($address);
34 ->setSelect([
'ID',
'ADDRESS'])
35 ->where(
'USER_ID', $this->currentUserId)
36 ->setOrder([
'USED_AT' =>
'DESC'])
37 ->setLimit(self::MAX_CNT)
42 foreach ($recentAddressList as $recentAddressListItem)
44 $recentAddress =
null;
49 catch (\Exception $e) {}
51 if ($recentAddress ===
null)
56 if ($this->areAddressesEqual($normalizedAddress, $recentAddress))
59 (
int)$recentAddressListItem[
'ID'],
61 'ADDRESS' => $normalizedAddress->toJson(),
74 'USER_ID' => $this->currentUserId,
75 'ADDRESS' => $normalizedAddress->toJson(),
80 public function get(
int $limit = self::MAX_CNT): array
85 ->setSelect([
'ADDRESS'])
86 ->where(
'USER_ID', $this->currentUserId)
87 ->setLimit(min($limit, self::MAX_CNT))
88 ->setOrder([
'USED_AT' =>
'DESC'])
91 foreach ($recentAddressList as $recentAddressListItem)
93 $recentAddressJson = $recentAddressListItem[
'ADDRESS'];
101 $result[] = $address;
110 DELETE FROM b_location_recent_address
112 USED_AT < DATE_SUB(NOW(), INTERVAL 30 DAY)
115 return '\\Bitrix\\Location\\Infrastructure\\Service\\RecentAddressesService::cleanUp();';
118 private function areAddressesEqual(
Address $address1,
Address $address2): bool
120 return $this->getAddressFields($address1) === $this->getAddressFields($address2);
123 private function getAddressFields(Address $address): array
128 foreach ($address->getFieldCollection() as $field)
130 $result[$field->getType()] = $field->getValue();
138 private function getNormalizedAddress(Address $address): Address
140 $result =
new Address(
141 $address->getLanguageId()
143 $fieldCollection =
new Address\FieldCollection();
145 $result->setLatitude($address->getLatitude());
146 $result->setLongitude($address->getLongitude());
149 foreach ($address->getFieldCollection() as $field)
151 $fieldCollection->addItem(
152 new Address\Field($field->getType(), $field->getValue())
156 $result->setFieldCollection($fieldCollection);
static getConnection($name="")
static update($primary, array $data)