Skip to content

Commit 58adf04

Browse files
authored
Create Can.php
1 parent 324fa85 commit 58adf04

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

php/Lib/Can.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/*
3+
* CAN - Código Alfa Numérico
4+
* Classe para converter um valor numérico em CAN e de volta à numérico.
5+
* @copyright Bill Rocha - http://plus.google.com/+BillRocha
6+
* @license MIT & GLP2
7+
* @author Bill Rocha - prbr@ymail.com
8+
* @version 0.0.1
9+
* @package Lib
10+
* @access public
11+
* @since 0.0.4
12+
* @date 2015/08/30-19:00:00
13+
14+
15+
digitos - resolução
16+
17+
1 = 64
18+
2 = 4.096
19+
3 = 262.144
20+
4 = 16.777.216
21+
5 = 1.073.741.824
22+
6 = 68.719.476.736 ( 68 bilhões )
23+
7 = 4.398.046.511.104
24+
8 = 281.474.976.710.656
25+
9 = 18.014.398.509.481.984
26+
10 = 1.152.921.504.606.846.976 ( 1 quinquilhão )
27+
28+
29+
Exemplo:
30+
31+
$can = new Can();
32+
$can->encode(50000) => "ntW"
33+
$can->decode("ntW") => 50000
34+
35+
Depende do arquivo com a chave aleatória (gerada na instalação da aplicação)
36+
define('_CONFIG', 'path/to/config/file/');
37+
38+
Use "Lib" para gerar uma chave aleatória:
39+
>> php Lib key:generate [enter]
40+
41+
*/
42+
43+
namespace Lib;
44+
45+
class Can
46+
{
47+
48+
private $number = 0;
49+
private $can = '';
50+
private $resolution = 10;
51+
private $useExtra = false;
52+
53+
54+
static $base = ['I','u','h','5','B','A','r','i','7','9','z','d','n','t','F','2','W','X','f','e','x','v','_','8','m','T','N','R','L','c','6','P','k','Q','q','j','Y','M','4','S','G','o','0','$','K','s','g','H','E','b','a','J','U','Z','l','1','O','3','y','p','V','D','C','w'];
55+
56+
static $extra_base = ['$','!','#','%','&','*','+','-','?','@','(',')','/','\\','[',']','_','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
57+
58+
/*
59+
*
60+
*/
61+
62+
function __construct($resolution = 10, $extra = false){
63+
if((0 + $resolution) > 0){
64+
$this->resolution = 0 + $resolution;
65+
}
66+
67+
if($extra) $this->useExtra = true;
68+
69+
//get "base" key OR use default
70+
if(file_exists(_CONFIG.'Key/can.key')){
71+
$base = file_get_contents(_CONFIG.'Key/can.key');
72+
static::$base = [];
73+
static::$extra_base = [];
74+
$base = explode("\n", $base);
75+
76+
for($i = 0; $i < strlen($base[0]); $i ++){
77+
static::$base[] = $base[0][$i];
78+
}
79+
for($i = 0; $i < strlen($base[1]); $i ++){
80+
static::$extra_base[] = $base[1][$i];
81+
}
82+
}
83+
}
84+
85+
/* Codifica um valor numérico em CAN ( ex.: 63.145 > GQT )
86+
* Parametro forceWidth: true - completa com base[0] os campos com valor "0" à esquerda
87+
* false - mostra somente os caracteres que fazem diferença.
88+
* Ex.: encode(1) => 'u'
89+
* encode(1, true) => 'IIIIIIIIIu';
90+
*
91+
*/
92+
function encode($num = null, $forceWidth = false){
93+
if($num !== null && (0 + $num) >= 0) $this->number = 0 + $num;
94+
95+
//overflow...
96+
if($this->number >= bcpow(64, $this->resolution)) return false;
97+
98+
$res = '';
99+
$num = $this->number;
100+
$himem = false;
101+
102+
for($i = $this->resolution; $i >= 1; $i--){
103+
if($num <= 0) {
104+
$res .= $this->useExtra ? static::$extra_base[0] : static::$base[0];
105+
continue;
106+
}
107+
$ind = bcpow(64, $i-1);
108+
$a = intval($num/$ind);
109+
if($a > 0) $himem = true;
110+
$num = $num - ($a*$ind);
111+
if($himem || $forceWidth) $res .= $this->useExtra ? static::$extra_base[$a] : static::$base[$a];
112+
}
113+
$this->can = $res;
114+
return $this->can;
115+
}
116+
117+
//Decodifica uma string CAN para um valor numérico ( ex.: GQT > 63.145 )
118+
function decode($can = null){
119+
if($can != null && is_string($can)) $this->can = $can;
120+
121+
$len = strlen($this->can) -1;
122+
$valor = 0;
123+
for($i = $len; $i >= 0; $i--){
124+
$peso = bcpow(64, $i);
125+
$d = substr($this->can, $len-$i, 1);
126+
$c = array_search($d, $this->useExtra ? static::$extra_base : static::$base);
127+
128+
if($c === false) return false;
129+
$valor += $peso * $c;
130+
}
131+
132+
$this->number = $valor;
133+
return $this->number;
134+
}
135+
}

0 commit comments

Comments
 (0)