1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem; |
4 | | | |
5 | | | use Phem\Core\Collection; |
6 | | | |
7 | | | /** |
8 | | | * General purpose tools |
9 | | | * |
10 | | | * @author Jakub PetrĹžĂlka <petrzilka@czweb.net> |
11 | | | */ |
12 | | | class ToolSuite |
13 | | | { |
14 | | | |
15 | | | public static function getRelativeClassName($obj) |
16 | | | { |
17 | | | $classPathArray = explode('\\', get_class($obj)); |
18 | | | return end($classPathArray); |
19 | | | } |
20 | | | |
21 | | | public static function genUuid() |
22 | | | { |
23 | | | return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
24 | | | ); |
25 | | | } |
26 | | | |
27 | | | public static function changeDateFormat($date, $format) |
28 | | | { |
29 | | | $timestamp = strtotime($date); |
30 | | | return date($format, $timestamp); |
31 | | | } |
32 | | | |
33 | | | public static function roundToAny($n, $x = 5) |
34 | | | { |
35 | | | return round($n / $x) * $x; |
36 | | | } |
37 | | | |
38 | | | public static function generateRandomString($length = 10) |
39 | | | { |
40 | | | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
41 | | | $randomString = ''; |
42 | | | for ($i = 0; $i < $length; $i++) |
43 | | | { |
44 | | | $randomString .= $characters[rand(0, strlen($characters) - 1)]; |
45 | | | } |
46 | | | return $randomString; |
47 | | | } |
48 | | | |
49 | | | public static function calculateAgeFromBirthDate($birthDate) |
50 | | | { |
51 | | | $birthday_timestamp = strtotime($birthDate); |
52 | | | return date('md', $birthday_timestamp) > date('md') ? date('Y') - date('Y', $birthday_timestamp) - 1 : date('Y') - date('Y', $birthday_timestamp); |
53 | | | } |
54 | | | |
55 | | | /** ZjiĹĄtÄnĂ data narozenĂ z rodnĂŠho ÄĂsla |
56 | | | * @param string $rodne_cislo rodnĂŠ ÄĂslo ve formĂĄtu rrmmdd/xxxx |
57 | | | * @return string datum ve formĂĄtu rrrr-mm-dd |
58 | | | * @copyright Jakub VrĂĄna, http://php.vrana.cz |
59 | | | */ |
60 | | | public static function getBirthDateFromCZPersonalNumber($rodne_cislo) |
61 | | | { |
62 | | | if (preg_match('~^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3,4})$~', $rodne_cislo, $match)) |
63 | | | { |
64 | | | return (strlen($match[4]) < 4 || $match[1] >= 54 ? "19" : "20") . "$match[1]-" . sprintf("%02d", $match[2] % 50) . "-$match[3]"; |
65 | | | } |
66 | | | return false; |
67 | | | } |
68 | | | |
69 | | | public static function getObjectValueFromPath($object, $path) |
70 | | | { |
71 | | | $attrNames = explode('/', $path); |
72 | | | $lastRef = $object; |
73 | | | |
74 | | | foreach ($attrNames as $atrrName) |
75 | | | { |
76 | | | $getterName = 'get' . $atrrName; |
77 | | | if (method_exists($lastRef, $getterName)) |
78 | | | { |
79 | | | $lastRef = $lastRef->$getterName(); |
80 | | | } else |
81 | | | { |
82 | | | return null; |
83 | | | } |
84 | | | } |
85 | | | return $lastRef; |
86 | | | } |
87 | | | |
88 | | | public static function getSubCollection($iterable, $valuePath, $keyPath = null) |
89 | | | { |
90 | | | $collection = new Collection(); |
91 | | | |
92 | | | foreach ($iterable as $item) |
93 | | | { |
94 | | | $value = ToolSuite::getObjectValueFromPath($item, $valuePath); |
95 | | | if ($keyPath != null) |
96 | | | { |
97 | | | $key = ToolSuite::getObjectValueFromPath($item, $keyPath); |
98 | | | $collection->put($key, $value); |
99 | | | } else |
100 | | | { |
101 | | | $collection->add($value); |
102 | | | } |
103 | | | } |
104 | | | |
105 | | | return $collection; |
106 | | | } |
107 | | | |
108 | | | public static function populateObjectFromAnother($source, $destination) |
109 | | | { |
110 | | | $setters = get_class_methods($destination); |
111 | | | foreach ($setters as $setter) |
112 | | | { |
113 | | | if (ToolSuite::startsWith($setter, 'set') !== false) |
114 | | | { |
115 | | | $getter = str_replace('set', 'get', $setter); |
116 | | | if (method_exists($source, $getter)) |
117 | | | { |
118 | | | $destination->$setter($source->$getter()); |
119 | | | } |
120 | | | } |
121 | | | } |
122 | | | } |
123 | | | |
124 | | | public static function startsWith($haystack, $needle) |
125 | | | { |
126 | | | return !strncmp($haystack, $needle, strlen($needle)); |
127 | | | } |
128 | | | |
129 | | | public static function endsWith($haystack, $needle) |
130 | | | { |
131 | | | $length = strlen($needle); |
132 | | | if ($length == 0) |
133 | | | { |
134 | | | return true; |
135 | | | } |
136 | | | |
137 | | | return (substr($haystack, -$length) === $needle); |
138 | | | } |
139 | | | |
140 | | | function makeDir($path,$mode = 0770,$recursive = true) |
141 | | | { |
142 | | | if (! is_dir($path)) |
143 | | | { |
144 | | | $old = umask(0); |
145 | | | mkdir($path,$mode,$recursive); |
146 | | | umask($old); |
147 | | | |
148 | | | } |
149 | | | |
150 | | | } |
151 | | | |
152 | | | public static function getStringBetween($string, $start, $end) |
153 | | | { |
154 | | | $string = " " . $string; |
155 | | | $ini = strpos($string, $start); |
156 | | | if ($ini == 0) |
157 | | | return ""; |
158 | | | $ini += strlen($start); |
159 | | | $len = strpos($string, $end, $ini) - $ini; |
160 | | | return substr($string, $ini, $len); |
161 | | | } |
162 | | | |
163 | | | public static function getLastArrayItem($array) |
164 | | | { |
165 | | | end($array); |
166 | | | $key = key($array); |
167 | | | return $array[$key]; |
168 | | | } |
169 | | | |
170 | | | } |