1 <?php
2 /**
3 * Util.php
4 *
5 * @author Elvyrra SAA
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This trait contains utilities method that cen be used anywhere in the core
13 *
14 * @package Utils
15 */
16 trait Utils{
17 /**
18 * Display variables for development debug
19 *
20 * @param mixed $var The variable to display
21 * @param bool $exit if set to true, exit the script
22 */
23 public static function debug($var, $exit = false){
24 if(DEBUG_MODE) {
25 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
26 echo "<pre>" ,
27 var_export($var, true) , PHP_EOL ,
28 $trace['file'], ":", $trace['line'], PHP_EOL,
29 "</pre>";
30
31 if($exit) {
32 exit;
33 }
34 }
35 }
36
37
38 /**
39 * Serialize a version number as integer.
40 * Example :
41 * <code>
42 * <?php
43 * echo Utils::getSerializedVersion('1.15.12'); // Output "01151200"
44 * ?>
45 * </code>
46 *
47 * @param string $version The version number to serialize, in the format gg.rr.cc(.pp)
48 *
49 * @return string The serialized version
50 */
51 public static function getSerializedVersion($version){
52 $digits = explode('.', $version);
53 $number = '';
54 foreach(range(0, 3) as $i) {
55 if(empty($digits[$i])) {
56 $digits[$i] = '00';
57 }
58 $number .= str_pad($digits[$i], 2, '0', STR_PAD_LEFT);
59 }
60 return $number;
61 }
62
63 /**
64 * Map an array data to the object that use this trait
65 *
66 * @param array $array The data to map to the object
67 * @param Object $object The object to map. If not set, get $this in the execution context of the method
68 */
69 public function map($array, $object = null){
70 if($object === null) {
71 $object = $this;
72 }
73 foreach($array as $key => $value){
74 $object->$key = $value;
75 }
76 }
77 }