a tiny mvc framework for php using php-activerecord
at master 168 lines 4.2 kB view raw
1<?php 2/* 3 internal utility functions 4*/ 5 6namespace HalfMoon; 7 8class Utils { 9 /* treat $data as an array, whatever it is */ 10 static function A($data, $index) { 11 return $data[$index]; 12 } 13 14 /* get result options for only/except option hashes */ 15 static function options_for_key_from_options_hash($key, $options) { 16 if (empty($options)) 17 return array(); 18 elseif (!is_array($options)) 19 return array($options); 20 21 /* if options is a hash, then the result should just be the keys that 22 * match. otherwise, it should contain all of the values that match. */ 23 $is_assoc = Utils::is_assoc($options); 24 25 $result = array(); 26 27 foreach ($options as $k => $opthash) { 28 if (Utils::is_assoc($opthash)) { 29 $apply = false; 30 31 if (isset($opthash["only"])) { 32 if (!is_array($opthash["only"])) 33 $opthash["only"] = array($opthash["only"]); 34 35 foreach ($opthash["only"] as $pkey) 36 if (Utils::strcasecmp_or_preg_match($pkey, $key)) { 37 $apply = true; 38 break; 39 } 40 } elseif (isset($opthash["except"])) { 41 $apply = true; 42 43 if (!is_array($opthash["except"])) 44 $opthash["except"] = array($opthash["except"]); 45 46 foreach ($opthash["except"] as $pkey) 47 if (Utils::strcasecmp_or_preg_match($pkey, $key)) { 48 $apply = false; 49 break; 50 } 51 } 52 53 if ($apply) { 54 if ($is_assoc) 55 array_push($result, $k); 56 else { 57 unset($opthash["only"]); 58 unset($opthash["except"]); 59 array_push($result, $opthash); 60 } 61 } 62 } else 63 array_push($result, $opthash); 64 } 65 66 return $result; 67 } 68 69 /* get a true or false for only/except option hashes */ 70 static function option_applies_for_key($key, $options) { 71 $noptions = array( 72 true => $options 73 ); 74 75 $ret = Utils::options_for_key_from_options_hash($key, $noptions); 76 77 return !empty($ret); 78 } 79 80 /* determine the current controller class by looking at a backtrace */ 81 static function current_controller() { 82 $controller = null; 83 84 foreach (debug_backtrace() as $stack) 85 if (isset($stack["object"]) && 86 preg_match("/^(HalfMoon\\\\)?ApplicationController$/", 87 get_parent_class($stack["object"]))) { 88 $controller = $stack["object"]; 89 break; 90 } 91 92 if ($controller) 93 return $controller; 94 else 95 throw new HalfMoonException("couldn't determine current " 96 . "controller"); 97 } 98 99 /* return the class type of the current controller object */ 100 static function current_controller_name() { 101 return get_class(Utils::current_controller()); 102 } 103 104 /* return an array of public methods for a given class */ 105 static function get_public_class_methods($class) { 106 $methods = array(); 107 108 foreach (get_class_methods($class) as $method) { 109 $reflect = new \ReflectionMethod($class, $method); 110 111 if ($reflect->isPublic()) 112 array_push($methods, $method); 113 } 114 115 return $methods; 116 } 117 118 /* like is_array() but tells whether it's an associative array */ 119 static function is_assoc($a) { 120 return is_array($a) && array_diff_key($a, array_keys(array_keys($a))); 121 } 122 123 /* like empty() but does not treat "0" as empty */ 124 function is_blank($value) { 125 return empty($value) && !is_numeric($value); 126 } 127 128 /* return true if $int is inclusively between an array of $low and $high */ 129 static function is_or_between($int, $low_and_high) { 130 return ($int >= $low_and_high[0] && $int <= $low_and_high[1]); 131 } 132 133 /* return a 40-character random string */ 134 static function random_hash() { 135 return sha1(pack("N10", 136 mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), 137 mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand())); 138 } 139 140 /* if passed a regular expression, do preg_match() on $string, otherwise do 141 * a case-insensitive match */ 142 static function strcasecmp_or_preg_match($check, $string) { 143 if (substr($check, 0, 1) == "/") 144 return preg_match($check, $string); 145 else 146 return !strcasecmp($check, $string); 147 } 148 149 /* when passed a closure containing print/<?=?> code, execute it, capture the 150 * output, and return it as a string */ 151 static function to_s($obj, $closure, $bind = null) { 152 ob_start(); 153 154 if (version_compare(PHP_VERSION, "5.4.0") >= 0) 155 /* setup $this */ 156 if (!empty($bind)) 157 $closure->bindTo($bind); 158 159 $closure($obj); 160 161 $str = ob_get_contents(); 162 ob_end_clean(); 163 164 return $str; 165 } 166} 167 168?>