CodeGenerator.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 by Dmitry E. Demidov                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class CodeGenerator
00016     {
00017         private $length             = null;
00018         
00019         private $lowerAllowed       = true;
00020         private $upperAllowed       = true;
00021         private $numbersAllowed     = true;
00022         private $similarAllowed     = true;
00023         
00024         static private $similarSymbols  = array('0', 'o', '1', 'l');
00025         
00029         public static function create()
00030         {
00031             return new self;
00032         }
00033         
00034         public function generate()
00035         {
00036             $code = null;
00037             
00038             for ($i = 0; $i < $this->length; ++$i)
00039                 $code .= $this->generateOneSymbol();
00040             
00041             return $code;
00042         }
00043         
00047         public function setLength($length)
00048         {
00049             $this->length = $length;
00050             
00051             return $this;
00052         }
00053         
00057         public function setLowerAllowed($lowerAllowed = true)
00058         {
00059             $this->lowerAllowed = $lowerAllowed;
00060             
00061             return $this;
00062         }
00063         
00067         public function setUpperAllowed($upperAllowed = true)
00068         {
00069             $this->upperAllowed = $upperAllowed;
00070             
00071             return $this;
00072         }
00073         
00077         public function setSimilarAllowed($similarAllowed = true)
00078         {
00079             $this->similarAllowed = $similarAllowed;
00080             
00081             return $this;
00082         }
00083         
00087         public function setNumbersAllowed($numbersAllowed = true)
00088         {
00089             $this->numbersAllowed = $numbersAllowed;
00090             
00091             return $this;
00092         }
00093         
00097         public function setCharactersAllowed($charactersAllowed = true)
00098         {
00099             $this->setLowerAllowed($charactersAllowed);
00100             $this->setUpperAllowed($charactersAllowed);
00101             
00102             return $this;
00103         }
00104         
00105         private function generateOneSymbol()
00106         {
00107             $variants = array();
00108             
00109             Assert::isTrue(
00110                 $this->lowerAllowed
00111                 || $this->upperAllowed
00112                 || $this->numbersAllowed,
00113                 
00114                 'what exactly should i generate?'
00115             );
00116             
00117             do {
00118                 if ($this->lowerAllowed)
00119                     $variants[] = $this->randomChar();
00120                 
00121                 if ($this->upperAllowed)
00122                     $variants[] = strtoupper($this->randomChar());
00123                 
00124                 if ($this->numbersAllowed)
00125                     $variants[] = $this->randomNumber();
00126                 
00127                 shuffle($variants);
00128                 
00129                 $symbol = $variants[0];
00130                 
00131             } while (
00132                 (!$this->similarAllowed)
00133                 && (in_array($symbol, self::$similarSymbols))
00134             );
00135             
00136             return $symbol;
00137         }
00138         
00139         private function randomNumber()
00140         {
00141             return mt_rand(0,9);
00142         }
00143         
00144         private function randomChar()
00145         {
00146             return chr(mt_rand(ord('a'), ord('z')));
00147         }
00148     }
00149 ?>