1: <?php
2:
3: namespace Deimos\Helper\Helpers\Str;
4:
5: use Deimos\Helper\AbstractHelper;
6:
7: class Str extends AbstractHelper
8: {
9:
10: use DefaultTrait;
11:
12: const DIGITS = '0123456789';
13: const ALPHABET_LOW = 'abcdefghijklmnopqrstuvwxyz';
14: const ALPHABET_HIGH = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
15: const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
16:
17: const RAND_ALPHA_LOW = 1;
18: const RAND_ALPHA_HIGH = 2;
19: const RAND_ALPHA = 4;
20: const RAND_DIGITS = 8;
21: const RAND_ALL = 16;
22:
23: 24: 25:
26: protected $dictionary = [
27: 4 => self::ALPHABET . self::DIGITS,
28: 3 => self::DIGITS,
29: 2 => self::ALPHABET,
30: 1 => self::ALPHABET_HIGH,
31: 0 => self::ALPHABET_LOW,
32: ];
33:
34: 35: 36: 37: 38: 39: 40: 41: 42:
43: public function shorten($str, $length = 100, $end = '…')
44: {
45:
46: if (strlen($str) > $length)
47: {
48: $str = substr(trim($str), 0, $length);
49: $str = substr($str, 0, -strpos(strrev($str), ' '));
50: $str = trim($str . $end);
51: }
52:
53: return $str;
54: }
55:
56: 57: 58: 59: 60:
61: public function ucFirst($string)
62: {
63: $first = $this->sub($string, 0, 1);
64: $first = $this->upp($first);
65:
66: return $first . $this->sub($string, 1);
67: }
68:
69: 70: 71: 72: 73:
74: public function lcFirst($string)
75: {
76: $first = $this->sub($string, 0, 1);
77: $first = $this->low($first);
78:
79: return $first . $this->sub($string, 1);
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
92: public function random($length = 32, $type = self::RAND_ALL)
93: {
94: $string = '';
95:
96:
97: foreach ($this->dictionary as $pos => $item)
98: {
99: $key = (1 << $pos);
100: if ($type >= $key)
101: {
102: $string .= $item;
103: $type -= $key;
104: }
105: }
106:
107: if (empty($string))
108: {
109: throw new \InvalidArgumentException("Invalid random string type [{$type}].");
110: }
111:
112: return $this->rand($string, $length);
113: }
114:
115: 116: 117: 118: 119: 120:
121: protected function rand($chars, $length)
122: {
123: $string = '';
124: $max = $this->len($chars) - 1;
125:
126: for ($i = 0; $i < $length; $i++)
127: {
128: $string .= $chars[random_int(0, $max)];
129: }
130:
131: return $string;
132: }
133:
134: 135: 136:
137: public function uniqid()
138: {
139: return uniqid(mt_rand(), true);
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function fileSize($size, $decimals = 2)
149: {
150:
151: switch (true)
152: {
153: case $size >= ((1 << 50) * 10):
154: $postfix = 'PB';
155: $size /= (1 << 50);
156: break;
157:
158: case $size >= ((1 << 40) * 10):
159: $postfix = 'TB';
160: $size /= (1 << 40);
161: break;
162:
163: case $size >= ((1 << 30) * 10):
164: $postfix = 'GB';
165: $size /= (1 << 30);
166: break;
167:
168: case $size >= ((1 << 20) * 10):
169: $postfix = 'MB';
170: $size /= (1 << 20);
171: break;
172:
173: case $size >= ((1 << 10) * 10):
174: $postfix = 'KB';
175: $size /= (1 << 10);
176: break;
177:
178: default:
179: $postfix = 'B';
180: }
181:
182: return round($size, $decimals) . ' ' . $postfix;
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public function translit($string)
193: {
194: $string = strtr($string, [
195: 'ОАО ' => 'OJSC ',
196: 'ЗАО ' => 'CJSC ',
197: 'ООО ' => 'LLC ',
198: 'ГОУ ' => 'SEE ',
199: 'МОУ ' => 'MEE ',
200: 'НОУ ' => 'NEE ',
201: 'фонд ' => 'Fund ',
202: 'союз ' => 'union ',
203:
204: 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
205: 'е' => 'e', 'ё' => 'e', 'з' => 'z', 'и' => 'i', 'й' => 'y',
206: 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
207: 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u',
208: 'ф' => 'f', 'х' => 'h', 'ъ' => '\'', 'ы' => 'i', 'э' => 'e',
209: 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
210: 'Е' => 'E', 'Ё' => 'E', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y',
211: 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
212: 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U',
213: 'Ф' => 'F', 'Х' => 'H', 'Ъ' => '\'', 'Ы' => 'I', 'Э' => 'E',
214: 'ж' => 'zh', 'ц' => 'ts', 'ч' => 'ch', 'ш' => 'sh',
215: 'щ' => 'shch', 'ь' => '', 'ю' => 'yu', 'я' => 'ya',
216: 'Ж' => 'Zh', 'Ц' => 'Ts', 'Ч' => 'Ch', 'Ш' => 'Sh',
217: 'Щ' => 'Shch', 'Ь' => '', 'Ю' => 'Yu', 'Я' => 'Ya',
218: '№' => 'N',
219: ]
220: );
221:
222: return iconv(mb_internal_encoding(), 'ASCII//TRANSLIT', $string);
223: }
224:
225: }
226: