urlshortener/lib/IdIncreaser.class.php

65 lines
1.2 KiB
PHP
Raw Normal View History

2013-09-26 16:03:50 +00:00
<?php
/**
* Class to increase an ID
*/
class IdIncreaser {
private $chars = "0123456789abcdefghijklmnopqrstuvwxyz";
private $c = NULL;
function __construct($current, $alphabet = "") {
if (strlen($alphabet) > 0) {
$this->chars = $alphabet;
}
$this->c = new OneChar($current, $this->chars);
}
function increase() {
$this->c->increase();
}
function build() {
return $this->c->build();
}
}
class OneChar {
private $chars;
private $otherChar = NULL;
private $pos = 0;
function __construct($s, $ch) {
$this->chars = $ch;
$current = substr($s, -1, 1);
$this->pos = strpos($this->chars, $current);
$s = substr($s, 0, strlen($s)-1);
if (strlen($s) > 0) {
$this->otherChar = new OneChar($s, $ch);
}
}
function increase() {
$this->pos = $this->pos + 1;
if ($this->pos >= strlen($this->chars)) {
$this->pos = 0;
if ($this->otherChar != NULL) {
$this->otherChar->increase();
} else {
$this->otherChar = new OneChar($this->chars[0], $this->chars);
}
}
}
function build() {
if ($this->otherChar != NULL) {
return $this->otherChar->build() . $this->chars[$this->pos];
}
return $this->chars[$this->pos];
}
}