Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Codegenerator.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  const DEFAULT_LENGTH_MIN = 16;
14 
18  const DEFAULT_LENGTH_MAX = 32;
19 
23  const SYMBOLS_COLLECTION = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
24 
28  const DEFAULT_DELIMITER = '-';
29 
35  public function generateCode()
36  {
37  $alphabet = $this->getAlphabet() ? $this->getAlphabet() : static::SYMBOLS_COLLECTION;
38  $length = $this->getActualLength();
39  $code = '';
40  for ($i = 0, $indexMax = strlen($alphabet) - 1; $i < $length; ++$i) {
41  $code .= substr($alphabet, random_int(0, $indexMax), 1);
42  }
43 
44  return $code;
45  }
46 
52  protected function getActualLength()
53  {
54  $lengthMin = $this->getLengthMin() ? $this->getLengthMin() : static::DEFAULT_LENGTH_MIN;
55  $lengthMax = $this->getLengthMax() ? $this->getLengthMax() : static::DEFAULT_LENGTH_MAX;
56 
57  return $this->getLength() ? $this->getLength() : random_int($lengthMin, $lengthMax);
58  }
59 
65  public function getDelimiter()
66  {
67  return $this->hasData('delimiter') ? $this->getData('delimiter') : static::DEFAULT_DELIMITER;
68  }
69 }
getData($key='', $index=null)
Definition: DataObject.php:119
$i
Definition: gallery.phtml:31
$code
Definition: info.phtml:12