<?php
 namespace Moto\Application\Util; class Generator { static function generateUniqueIncrementValue(array $listOfNames, $value = '') { $value = trim($value); $numericPart = self::_fetchNumericPart($value, $stringPart); if (empty($listOfNames)) { return $stringPart . $numericPart; } $value = $stringPart . $numericPart; $isUnique = !in_array($value, $listOfNames); $name = $value; while(!$isUnique) { $name = $stringPart . ++$numericPart; $isUnique = !in_array($name, $listOfNames); } return $name; } static protected function _fetchNumericPart($value, &$stringPart) { $numericPart = ''; $stringPart = $value; if (!empty($value) && preg_match('/(.*?)([0-9]+)$/', trim($value), $matches) === 1) { $stringPart = $matches[1]; $numericPart = ($matches[2] >= PHP_INT_MAX) ? substr($matches[2], 0, strlen(PHP_INT_MAX) - 2) : $matches[2]; } return $numericPart; } }