[ 'prefix' => 'U', 'pattern' => '^(?U)(?\d+)$', 'reversePrefix' => 'U', 'reversePattern' => '^(?\d+)$' ], 'project' => [ 'prefix' => 'SG', 'pattern' => '^(?SG)(?\d+)$', 'reversePrefix' => 'SG', 'reversePattern' => '^(?\d+)$' ], 'crm-company' => [ 'prefix' => 'CRMCOMPANY', 'pattern' => '^(?CRMCOMPANY)(?.+)$', 'reversePrefix' => 'CRMCOMPANY', 'reversePattern' => '^(?\d+)$' ], 'crm-contact' => [ 'prefix' => 'CRMCONTACT', 'pattern' => '^(?CRMCONTACT)(?.+)$', 'reversePrefix' => 'CRMCONTACT', 'reversePattern' => '^(?\d+)$' ], 'crm-lead' => [ 'prefix' => 'CRMLEAD', 'pattern' => '^(?CRMLEAD)(?.+)$', 'reversePrefix' => 'CRMLEAD', 'reversePattern' => '^(?\d+)$' ], 'crm-deal' => [ 'prefix' => 'CRMDEAL', 'pattern' => '^(?CRMDEAL)(?.+)$', 'reversePrefix' => 'CRMDEAL', 'reversePattern' => '^(?\d+)$' ], 'crm-quote' => [ 'prefix' => 'CRMQUOTE', 'pattern' => '^(?CRMQUOTE)(?.+)$', 'reversePrefix' => 'CRMQUOTE', 'reversePattern' => '^(?\d+)$' ], 'crm-order' => [ 'prefix' => 'CRMORDER', 'pattern' => '^(?CRMORDER)(?.+)$', 'reversePrefix' => 'CRMORDER', 'reversePattern' => '^(?\d+)$' ], 'crm-product' => [ 'prefix' => 'CRMPRODUCT', 'pattern' => '^(?CRMPRODUCT)(?.+)$', 'reversePrefix' => 'CRMPRODUCT', 'reversePattern' => '^(?\d+)$' ], 'mail-contact' => [ 'prefix' => 'MC', 'pattern' => '^(?MC)(?[0-9]+)$', 'reversePrefix' => 'MC', 'reversePattern' => '^(?\d+)$' ], 'department' => [ 'prefix' => (function($itemId) { return is_string($itemId) && $itemId[-1] === 'F' ? 'D' : 'DR'; }), 'itemId' => function($prefix, $itemId) { return $prefix === 'D' ? $itemId.':F' : $itemId; }, 'pattern' => '^(?DR?)(?\d+)$', 'reversePrefix' => (function($suffix) { return $suffix === ':F' ? 'D' : 'DR'; }), 'reversePattern' => '^(?\d+)(?.*)$' ], ]; return $compatEntities; } public static function convertFromFinderCodes(array $codesList = []) { $result = []; foreach ($codesList as $code) { if ($code === 'UA') { $result[] = ['meta-user', 'all-users']; continue; } foreach (self::getCompatEntities() as $entityId => $entity) { if(preg_match('/'.$entity['pattern'].'/i', $code, $matches)) { $result[] = [ $entityId, (int)$matches['itemId'] ]; } } } return $result; } public static function convertToFinderCodes(array $entitiesList = []) { $result = []; foreach ($entitiesList as [ $entityId, $id ]) { if ( $entityId === 'meta-user' && $id === 'all-users' ) { $result[] = 'UA'; continue; } foreach (self::getCompatEntities() as $sampleEntityId => $sampleEntity) { if ($entityId !== $sampleEntityId) { continue; } if(preg_match('/'.$sampleEntity['reversePattern'].'/i', $id, $matches)) { $result[] = (is_callable($sampleEntity['reversePrefix']) ? $sampleEntity['reversePrefix']($matches['suffix']) : $sampleEntity['reversePrefix']).$matches['itemId']; } } } return $result; } public static function sortEntities(array $entities = []) { usort($entities, function($a, $b) { $aKey = array_search($a[0], self::$sampleSortPriority, true); $bKey = array_search($b[0], self::$sampleSortPriority, true); if($aKey < $bKey) { return -1; } if ($aKey > $bKey) { return 1; } return 0; }); return $entities; } }