<?php
namespace App\Utils\BaseParser\Utils; class ConjunctionsTicketsConvertor { use \App\Utils\BaseParser\SingletonTrait; public function extractFromDashNotation($ticketStr) { $dashpos = strpos($ticketStr, "-"); if ($dashpos===false) { return [$ticketStr]; } $ticketsLen = strlen($ticketStr); $originalTicketNumberPrefix = ''; if ($ticketsLen>9) { $originalTicketNumberRaw = substr($ticketStr, 0, $dashpos); $originalTicketNumber = substr($originalTicketNumberRaw, -9); $originalTicketNumberPrefix = substr($originalTicketNumberRaw, 0, -9); } else { $originalTicketNumber = substr($ticketStr, 0, $dashpos); } if (($originalTicketNumber && !is_numeric($originalTicketNumber)) || ($originalTicketNumberPrefix && !is_numeric($originalTicketNumberPrefix))) { throw new \RuntimeException('Either the original ticket or the prefix are not numeric. Ticket no: '.$originalTicketNumber); } $endTicketNumber = substr($originalTicketNumber, 0, -($ticketsLen-$dashpos-1)) . substr($ticketStr, -($ticketsLen-$dashpos-1)); $endTicketNumberLen = strlen($endTicketNumber); $endTicketNumberPrefix = ''; if ($endTicketNumberLen>9) { $endTicketNumberRaw = $endTicketNumber; $endTicketNumber = substr($endTicketNumberRaw, -9); $endTicketNumberPrefix = substr($endTicketNumberRaw, 0, -9); if($endTicketNumberPrefix && $originalTicketNumberPrefix) { if (strrpos($originalTicketNumberPrefix, $endTicketNumberPrefix)!==0) { throw new \Exception(sprintf("Cannot handle ticket number: %s. It implies too many changes. endTicketNumberPrefix doesn't match originalTicketNumberPrefix", $ticketStr)); } if (strlen($endTicketNumberPrefix) > strlen($originalTicketNumberPrefix)) { throw new \Exception(sprintf("Cannot handle ticket number: %s. It implies too many changes. endTicketNumberPrefix is longer than originalTicketNumberPrefix", $ticketStr)); } } } if ($endTicketNumber < $originalTicketNumber) { $endTicketNumber = sprintf("%0{$endTicketNumberLen}d", ((int)$endTicketNumber + pow(10, $ticketsLen-$dashpos-1))); if (strlen($endTicketNumber) != strlen($originalTicketNumber)) { throw new \Exception(sprintf("Cannot handle ticket number: %s. It implies too many changes. endTicketNumber is longer than originalTicketNumber", $ticketStr)); } } $conjunctionsList = []; $tkt = $originalTicketNumber; $ol = strlen($originalTicketNumber); while ($tkt <= $endTicketNumber) { $conjunctionsList[] = $originalTicketNumberPrefix.sprintf("%0{$ol}s", $tkt); $tkt++; } return $conjunctionsList; } public function extractFromTicketAndCount($ticketStr, $ticketCount) { if ($ticketCount<0) { throw new \Exception(sprintf("Cannot handle ticket number: %s with count %d. ticketCount should be positive", $ticketStr, $ticketCount)); } $optimalLength = strlen($ticketCount)+1; $remainder = substr($ticketStr, -1*$optimalLength); $loops = 100; while ($loops-- && strlen($remainder+$ticketCount)>$optimalLength) { $optimalLength+=1; $remainder = substr($ticketStr, -1*$optimalLength); } if ($loops<=0) { throw new \Exception(sprintf("Cannot handle ticket number: %s. Infinite loop detected while figuring out optimalLength", $ticketStr)); } $newTicketStr = sprintf("%s-%s", $ticketStr, (int)$remainder+$ticketCount); return $this->extractFromDashNotation($newTicketStr); } }