Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/cvetdv.ru/bitrix/modules/yandex.market/lib/utils/ |
Upload File : |
<?php namespace Yandex\Market\Utils; use Bitrix\Main; class Field { const GLUE_DOT = 'dot'; const GLUE_BRACKET = 'bracket'; public static function hasChainValue($values, $key, $glue = Field::GLUE_DOT) { $keyParts = static::splitKey($key, $glue); $lastKey = array_pop($keyParts); $lastLevel = static::getChainValue($values, $keyParts, $glue); return is_array($lastLevel) && array_key_exists($lastKey, $lastLevel); } public static function getChainValue($values, $key, $glue = Field::GLUE_DOT) { $keyParts = static::splitKey($key, $glue); $lastLevel = $values; foreach ($keyParts as $keyPart) { if (isset($lastLevel[$keyPart])) { $lastLevel = $lastLevel[$keyPart]; } else { $lastLevel = null; break; } } return $lastLevel; } public static function setChainValue(&$values, $key, $value, $glue = Field::GLUE_DOT) { $keyParts = static::splitKey($key, $glue); $keyPartIndex = 0; $keyPartCount = count($keyParts); $lastLevel = &$values; foreach ($keyParts as $keyPart) { if ($keyPartCount === $keyPartIndex + 1) { $lastLevel[$keyPart] = $value; } else { if (!isset($lastLevel[$keyPart]) || !is_array($lastLevel[$keyPart])) { $lastLevel[$keyPart] = []; } $lastLevel = &$lastLevel[$keyPart]; } $keyPartIndex++; } } public static function pushChainValue(&$values, $key, $value, $glue = Field::GLUE_DOT) { $keyParts = static::splitKey($key, $glue); $keyPartIndex = 0; $keyPartCount = count($keyParts); $lastLevel = &$values; foreach ($keyParts as $keyPart) { if ($keyPartCount === $keyPartIndex + 1) { if (!isset($lastLevel[$keyPart])) { $lastLevel[$keyPart] = []; } $lastLevel[$keyPart][] = $value; } else { if (!isset($lastLevel[$keyPart]) || !is_array($lastLevel[$keyPart])) { $lastLevel[$keyPart] = []; } $lastLevel = &$lastLevel[$keyPart]; } $keyPartIndex++; } } public static function unsetChainValue(&$values, $key, $glue = Field::GLUE_DOT) { $keyParts = static::splitKey($key, $glue); $keyPartIndex = 0; $keyPartCount = count($keyParts); $lastLevel = &$values; foreach ($keyParts as $keyPart) { if (!isset($lastLevel[$keyPart])) { break; } if ($keyPartCount === $keyPartIndex + 1) { unset($lastLevel[$keyPart]); } else { $lastLevel = &$lastLevel[$keyPart]; } $keyPartIndex++; } } public static function splitKey($key, $glue = Field::GLUE_DOT) { if (is_array($key)) { return $key; } if ($glue === static::GLUE_DOT) { return explode('.', $key); } if ($glue === static::GLUE_BRACKET) { return static::splitKeyByBrackets($key); } throw new Main\ArgumentException(sprintf('unknown glue %s', $glue)); } public static function implodeKey(array $partials, $glue = Field::GLUE_DOT) { if ($glue === static::GLUE_DOT) { return implode('.', $partials); } if ($glue === static::GLUE_BRACKET) { $base = array_shift($partials); $children = array_map(static function($part) { return "[{$part}]"; }, $partials); return $base . implode('', $children); } throw new Main\ArgumentException(sprintf('unknown glue %s', $glue)); } protected static function splitKeyByBrackets($key) { $keyOffset = 0; $keyLength = mb_strlen($key); $keyChain = []; do { if ($keyOffset === 0) { $arrayEnd = mb_strpos($key, '['); if ($arrayEnd === false) { $keyPart = $key; $keyOffset = $keyLength; } else if ($arrayEnd === 0) { $keyOffset = 1; continue; } else { $keyPart = mb_substr($key, $keyOffset, $arrayEnd - $keyOffset); $keyOffset = $arrayEnd + 1; } } else { $arrayEnd = mb_strpos($key, ']', $keyOffset); if ($arrayEnd === false) { $keyPart = mb_substr($key, $keyOffset); $keyOffset = $keyLength; } else { $keyPart = mb_substr($key, $keyOffset, $arrayEnd - $keyOffset); $keyOffset = $arrayEnd + 2; } } if ((string)$keyPart !== '') { $keyChain[] = $keyPart; } else { break; } } while ($keyOffset < $keyLength); return $keyChain; } }