Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BattleNetGuessProductCode implements Iterator {
- private $_type;
- private $_code;
- private $_guessCharOffset;
- private $_position;
- private $_guessStr = "ABCDEFGHIJKLMNOPQRSTUVXYWZ0123456789";
- function __construct($code)
- {
- //if (preg_match('/\A(?P<code>(?:[^-]{6})-(?:[^-]{4})-(?:[^-]{6})-(?:[^-]{4})-(?:[^-]{6}))\Z/', $code)) {
- if (preg_match('/\A(?<![\w-])(?P<code>(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*)-(?:[\w\*\#\?]*))(?![\w-])/', $code)) {
- // Change any odd characters to unknown char
- $patterns = array();
- $patterns[] = '/-/';
- $patterns[] = '/zero/';
- $patterns[] = '/one/';
- $patterns[] = '/two/';
- $patterns[] = '/three/';
- $patterns[] = '/four/';
- $patterns[] = '/five/';
- $patterns[] = '/six/';
- $patterns[] = '/seven/';
- $patterns[] = '/eight/';
- $patterns[] = '/nine/';
- $patterns[] = '/[^-A-Z0-9]/';
- $replacements = array();
- $replacements[] = '';
- $replacements[] = '0';
- $replacements[] = '1';
- $replacements[] = '2';
- $replacements[] = '3';
- $replacements[] = '4';
- $replacements[] = '5';
- $replacements[] = '6';
- $replacements[] = '7';
- $replacements[] = '8';
- $replacements[] = '9';
- $replacements[] = '*';
- $code = preg_replace($patterns, $replacements, $code);
- if (!preg_match('/\A(?P<code>(?:[A-Z0-9\*]{26}))\Z/', $code)) {
- throw new BattleNetCodeInvalidException($code);
- }
- if (substr_count($code, "*") == 0)
- {
- $this->_code = $code;
- $this->_type = BattleNetGuessProductCodeType::SINGLE;
- }
- else
- {
- $this->_type = BattleNetGuessProductCodeType::MULTI;
- if (substr_count($code, "*") > 2)
- throw new Exception("Too many unknowns");
- for ($i=0;$i<substr_count($code, "*");$i++)
- {
- $offset = ($i > 0) ? $this->_guessCharOffset[$i-1]+1 : 0;
- $this->_guessCharOffset[] = strpos($code, "*", $offset);
- }
- $this->_code = $code;
- }
- } else {
- throw new Exception("Not a valid BattleNet product code");
- }
- $this->_position = 0;
- }
- function getType()
- {
- return $this->_type;
- }
- function rewind() {
- $this->_position = 0;
- }
- function current() {
- if (!$this->_guessCharOffset)
- return new BattleNetProductCode($this->_code);
- else
- return new BattleNetProductCode($this->getGuessCode($this->_position));
- }
- function key() {
- return $this->_position;
- }
- function next() {
- ++$this->_position;
- }
- function valid() {
- if (!$this->_guessCharOffset)
- return $this->_position < 1;
- else
- return $this->_position < pow(strlen($this->_guessStr), count($this->_guessCharOffset));
- }
- function getGuessCode($offset)
- {
- $str = $this->_code;
- foreach ($this->_guessCharOffset as $index => $charOffset)
- {
- $str[$charOffset] = $this->_guessStr[($offset / pow(strlen($this->_guessStr), $index)) % strlen($this->_guessStr)];
- }
- return $str;
- }
- function get()
- {
- return $this->_code;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement