Alphanum.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Enter description here...
  8. *
  9. * Properties:
  10. * - prefix
  11. * - pad_length
  12. * - pad_char
  13. * - last_id
  14. */
  15. namespace Magento\Eav\Model\Entity\Increment;
  16. class Alphanum extends \Magento\Eav\Model\Entity\Increment\AbstractIncrement
  17. {
  18. /**
  19. * Get allowed chars
  20. *
  21. * @return string
  22. * @codeCoverageIgnore
  23. */
  24. public function getAllowedChars()
  25. {
  26. return '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  27. }
  28. /**
  29. * Get next id
  30. *
  31. * @return string
  32. * @throws \Magento\Framework\Exception\LocalizedException
  33. */
  34. public function getNextId()
  35. {
  36. $lastId = $this->getLastId();
  37. if (strpos($lastId, $this->getPrefix()) === 0) {
  38. $lastId = substr($lastId, strlen($this->getPrefix()));
  39. }
  40. $lastId = str_pad((string)$lastId, $this->getPadLength(), $this->getPadChar(), STR_PAD_LEFT);
  41. $nextId = '';
  42. $bumpNextChar = true;
  43. $chars = $this->getAllowedChars();
  44. $lchars = strlen($chars);
  45. $lid = strlen($lastId) - 1;
  46. for ($i = $lid; $i >= 0; $i--) {
  47. $p = strpos($chars, $lastId[$i]);
  48. if (false === $p) {
  49. throw new \Magento\Framework\Exception\LocalizedException(
  50. __('Invalid character encountered in increment ID: %1', $lastId)
  51. );
  52. }
  53. if ($bumpNextChar) {
  54. $p++;
  55. $bumpNextChar = false;
  56. }
  57. if ($p === $lchars) {
  58. $p = 0;
  59. $bumpNextChar = true;
  60. }
  61. $nextId = $chars[$p] . $nextId;
  62. }
  63. return $this->format($nextId);
  64. }
  65. }