forked from craue/CraueFormFlowBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtil.php
69 lines (54 loc) · 1.61 KB
/
StringUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Craue\FormFlowBundle\Util;
use Craue\FormFlowBundle\Exception\InvalidTypeException;
/**
* @author Christian Raue <[email protected]>
* @copyright 2011-2022 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class StringUtil {
private function __construct() {}
/**
* @param int $length
* @return string
*/
public static function generateRandomString($length) {
if (!is_int($length)) {
throw new InvalidTypeException($length, 'int');
}
if ($length < 0) {
throw new \InvalidArgumentException(sprintf('Length must be >= 0, "%s" given.', $length));
}
return substr(rtrim(strtr(base64_encode(random_bytes($length)), '+/', '-_'), '='), 0, $length);
}
/**
* @param string $input
* @param int $length
* @return bool
*/
public static function isRandomString($input, $length) {
if (!is_string($input)) {
throw new InvalidTypeException($input, 'string');
}
if (!is_int($length)) {
throw new InvalidTypeException($length, 'int');
}
if ($length < 0) {
throw new \InvalidArgumentException(sprintf('Length must be >= 0, "%s" given.', $length));
}
return preg_match(sprintf('/^[a-zA-Z0-9-_]{%u}$/', $length), $input) === 1;
}
/**
* @param string $fqcn FQCN
* @return string|null flow name or null if not a FQCN
*/
public static function fqcnToFlowName($fqcn) {
if (!is_string($fqcn)) {
throw new InvalidTypeException($fqcn, 'string');
}
if (preg_match('/([^\\\\]+?)(flow)?$/i', $fqcn, $matches)) {
return lcfirst(preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1\\2', $matches[1]));
}
return null;
}
}