• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/Multifield/Assembler.php
  • Класс: Bitrix\Crm\Multifield\Assembler
  • Вызов: Assembler::updateCollectionByArray
static function updateCollectionByArray(Collection $collection, array $compatibleArray): void
{
	foreach ($compatibleArray as $typeId => $valuesOfSameType)
	{
		foreach ($valuesOfSameType as $id => $compatibleValue)
		{
			if (!isset($compatibleValue['VALUE']) || $compatibleValue['VALUE'] === '')
			{
				if (is_numeric($id))
				{
					$collection->removeById((int)$id);
				}

				continue;
			}

			$value = null;
			if (is_numeric($id))
			{
				$value = $collection->getById((int)$id);
			}

			if (!$value)
			{
				if (mb_strpos($id, 'n') === 0)
				{
					// Key is like 'n0', it's an entirely new value, ignore ID. For compatibility reasons.
					unset($compatibleValue['ID']);
				}

				$newValue = self::valueByArray($compatibleValue);
				$newValue->setTypeId((string)$typeId);

				$collection->add($newValue);
				continue;
			}

			if ($value->getValue() !== $compatibleValue['VALUE'])
			{
				$value->setValue((string)$compatibleValue['VALUE']);
			}

			$compatibleValueCountryCode = null;
			if (isset($compatibleValue['VALUE_EXTRA']['COUNTRY_CODE']))
			{
				$compatibleValueCountryCode = (string)$compatibleValue['VALUE_EXTRA']['COUNTRY_CODE'];
			}
			elseif (isset($compatibleValue['VALUE_COUNTRY_CODE']))
			{
				$compatibleValueCountryCode = (string)$compatibleValue['VALUE_COUNTRY_CODE'];
			}

			if ($compatibleValueCountryCode === null && $value->getValueExtra())
			{
				$value->getValueExtra()->setCountryCode(null);
			}
			elseif ($compatibleValueCountryCode !== null)
			{
				if ($value->getValueExtra() && $value->getValueExtra()->getCountryCode() !== $compatibleValueCountryCode)
				{
					$value->getValueExtra()->setCountryCode($compatibleValueCountryCode);
				}
				elseif ($value->getValueExtra() === null)
				{
					$value->setValueExtra((new ValueExtra())->setCountryCode($compatibleValueCountryCode));
				}
			}

			$isValueTypeChanged =
				isset($compatibleValue['VALUE_TYPE'])
				&& ($value->getValueType() !== $compatibleValue['VALUE_TYPE'])
			;
			if ($isValueTypeChanged)
			{
				$value->setValueType((string)$compatibleValue['VALUE_TYPE']);
			}
		}
	}
}