Beispiel #1
0
 /** @inheritDoc */
 public function generate(DocumentManager $dm, $document)
 {
     $id = parent::generate($dm, $document);
     $index = $this->awkwardSafeMode ? $this->awkwardSafeChars : $this->chars;
     $base = strlen($index);
     $out = '';
     do {
         $out = $index[bcmod($id, $base)] . $out;
         $id = bcdiv($id, $base);
     } while (bccomp($id, 0) == 1);
     if (is_numeric($this->pad)) {
         $out = str_pad($out, $this->pad, '0', STR_PAD_LEFT);
     }
     return $out;
 }
Beispiel #2
0
 /** @inheritDoc */
 public function generate(DocumentManager $dm, $document)
 {
     $id = parent::generate($dm, $document);
     $index = $this->awkwardSafeMode ? $this->awkwardSafeChars : $this->chars;
     $base = strlen($index);
     $out = "";
     for ($t = floor(log10($id) / log10($base)); $t >= 0; $t--) {
         $a = floor($id / pow($base, $t));
         $out = $out . substr($index, $a, 1);
         $id = $id - $a * pow($base, $t);
     }
     if (is_numeric($this->pad)) {
         $out = str_pad($out, $this->pad, "0", STR_PAD_LEFT);
     }
     return $out;
 }
 private function completeIdGeneratorMapping(ClassMetadataInfo $class)
 {
     $idGenOptions = $class->generatorOptions;
     switch ($class->generatorType) {
         case ClassMetadata::GENERATOR_TYPE_AUTO:
             $class->setIdGenerator(new AutoGenerator());
             break;
         case ClassMetadata::GENERATOR_TYPE_INCREMENT:
             $incrementGenerator = new IncrementGenerator();
             if (isset($idGenOptions['key'])) {
                 $incrementGenerator->setKey($idGenOptions['key']);
             }
             if (isset($idGenOptions['collection'])) {
                 $incrementGenerator->setCollection($idGenOptions['collection']);
             }
             $class->setIdGenerator($incrementGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_UUID:
             $uuidGenerator = new UuidGenerator();
             isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']);
             $class->setIdGenerator($uuidGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_ALNUM:
             $alnumGenerator = new AlnumGenerator();
             if (isset($idGenOptions['pad'])) {
                 $alnumGenerator->setPad($idGenOptions['pad']);
             }
             if (isset($idGenOptions['chars'])) {
                 $alnumGenerator->setChars($idGenOptions['chars']);
             } elseif (isset($idGenOptions['awkwardSafe'])) {
                 $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']);
             }
             $class->setIdGenerator($alnumGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_CUSTOM:
             if (empty($idGenOptions['class'])) {
                 throw MappingException::missingIdGeneratorClass($class->name);
             }
             $customGenerator = new $idGenOptions['class']();
             unset($idGenOptions['class']);
             if (!$customGenerator instanceof AbstractIdGenerator) {
                 throw MappingException::classIsNotAValidGenerator(get_class($customGenerator));
             }
             $methods = get_class_methods($customGenerator);
             foreach ($idGenOptions as $name => $value) {
                 $method = 'set' . ucfirst($name);
                 if (!in_array($method, $methods)) {
                     throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name);
                 }
                 $customGenerator->{$method}($value);
             }
             $class->setIdGenerator($customGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_NONE:
             break;
         default:
             throw new MappingException('Unknown generator type: ' . $class->generatorType);
     }
 }