8 * Convert a Name Value Pair (NVP) formatted string into
9 * an associative array taking care to urldecode array values
11 * @param string $nvpString
14 public static function nvpToMap($nvpString)
17 $params = explode("&", $nvpString);
18 foreach ($params as $p) {
19 list($k, $v) = explode("=", $p);
20 $ret[$k] = urldecode($v);
28 * Returns true if the array contains a key like $key
34 public static function array_match_key($map, $key)
36 $replace = str_replace(array(
46 $pattern = "/$replace*/";
48 foreach ($map as $k => $v) {
49 preg_match($pattern, $k, $matches);
50 if (count($matches) > 0) {
60 * Get the local IP address. The client address is a required
61 * request parameter for some API calls
63 public static function getLocalIPAddress()
65 if (array_key_exists("SERVER_ADDR", $_SERVER)) {
66 // SERVER_ADDR is available only if we are running the CGI SAPI
67 return $_SERVER['SERVER_ADDR'];
70 if (function_exists("gethostname")) {
71 // gethostname is available only in PHP >= v5.3
72 return gethostbyname(gethostname());
75 // fallback if nothing works
81 public static function xmlToArray($xmlInput)
83 $xml = simplexml_load_string($xmlInput);
85 $ns = $xml->getNamespaces(true);
86 $soap = $xml->children($ns['SOAP-ENV']);
87 $getChild = $soap->Body->children();
89 $ret = PPUtils::convertXmlObjToArr($getChild, $array);
95 private static function convertXmlObjToArr($obj, &$arr)
97 $children = $obj->children();
98 foreach ($children as $elementName => $node) {
99 $nextIdx = count($arr);
100 $arr[$nextIdx] = array();
101 $arr[$nextIdx]['name'] = strtolower((string)$elementName);
102 $arr[$nextIdx]['attributes'] = array();
103 $attributes = $node->attributes();
104 foreach ($attributes as $attributeName => $attributeValue) {
105 $attribName = strtolower(trim((string)$attributeName));
106 $attribVal = trim((string)$attributeValue);
107 $arr[$nextIdx]['attributes'][$attribName] = $attribVal;
109 $text = (string)$node;
111 if (strlen($text) > 0) {
112 $arr[$nextIdx]['text'] = $text;
114 $arr[$nextIdx]['children'] = array();
115 PPutils::convertXmlObjToArr($node, $arr[$nextIdx]['children']);
123 * Escapes invalid xml characters
125 * @param $textContent = xml data to be escaped
128 public static function escapeInvalidXmlCharsRegex($textContent)
130 return htmlspecialchars($textContent, (1 | 2), 'UTF-8', false);
137 * @param string $keyPrefix
140 public static function filterKeyPrefix(array $map, $keyPrefix)
143 foreach ($map as $key => $val) {
144 if (($pos = stripos($key, $keyPrefix)) !== 0) {
148 $filtered[substr_replace($key, '', 0, strlen($keyPrefix))] = $val;
157 * @var array|ReflectionProperty[]
159 private static $propertiesRefl = array();
162 * @var array|string[]
164 private static $propertiesType = array();
169 * @param string $class
170 * @param string $propertyName
171 * @throws RuntimeException
174 public static function propertyAnnotations($class, $propertyName)
176 $class = is_object($class) ? get_class($class) : $class;
177 if (!class_exists('ReflectionProperty')) {
178 throw new \RuntimeException("Property type of " . $class . "::{$propertyName} cannot be resolved");
181 if ($annotations =& self::$propertiesType[$class][$propertyName]) {
185 if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
186 $refl = new \ReflectionProperty($class, $propertyName);
189 // todo: smarter regexp
190 if (!preg_match_all('~\@([^\s@\(]+)[\t ]*(?:\(?([^\n@]+)\)?)?~i', $refl->getDocComment(), $annots, PREG_PATTERN_ORDER)) {
193 foreach ($annots[1] as $i => $annot) {
194 $annotations[strtolower($annot)] = empty($annots[2][$i]) ? TRUE : rtrim($annots[2][$i], " \t\n\r)");
201 * @param string $class
202 * @param string $propertyName
205 public static function isAttributeProperty($class, $propertyName) {
206 if (($annotations = self::propertyAnnotations($class, $property))) {
207 return $annotations['attribute'];
213 * @param string $class
214 * @param string $propertyName
217 public static function isPropertyArray($class, $propertyName) {
218 if (($annotations = self::propertyAnnotations($class, $propertyName))) {
219 if (isset($annotations['var']) && substr($annotations['var'], -2) === '[]') {
222 } elseif (isset($annotations['array'])) {
233 * @param string $class
234 * @param string $propertyName
235 * @throws RuntimeException
238 public static function propertyType($class, $propertyName)
240 if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['var'])) {
241 if (substr($annotations['var'], -2) === '[]') {
242 return substr($annotations['var'], 0, -2);
245 return $annotations['var'];
252 * @param object $object
255 public static function objectProperties($object)
258 foreach (get_object_vars($object) as $property => $default) {
259 $annotations = self::propertyAnnotations($object, $property);
260 if (isset($annotations['name'])) {
261 $props[strtolower($annotations['name'])] = $property;
264 $props[strtolower($property)] = $property;
273 * @param array $array
276 public static function lowerKeys(array $array)
279 foreach ($array as $key => $value) {
280 $ret[strtolower($key)] = $value;
291 * XMLToArray Generator Class
293 * @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
294 * Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
295 * URL: LINK2http://www.rupom.infoLINK2
298 * Purpose : Creating Hierarchical Array from XML Data
299 * Released : Under GPL
309 * Default Constructor
311 * @param $xml = xml data
314 function XmlToArray($xml)
322 * _struct_to_array($values, &$i)
324 * This is adds the contents of the return xml into the array for easier processing.
328 * @param array $values this is the xml data in an array
329 * @param int $i this is the current location in the array
332 function _struct_to_array($values, &$i)
335 if (isset($values[$i]['value'])) {
336 array_push($child, $values[$i]['value']);
339 while ($i++ < count($values)) {
340 switch ($values[$i]['type']) {
342 array_push($child, $values[$i]['value']);
346 $name = $values[$i]['tag'];
348 $child[$name] = ($values[$i]['value']) ? ($values[$i]['value']) : '';
349 if (isset($values[$i]['attributes'])) {
350 $child[$name] = $values[$i]['attributes'];
356 $name = $values[$i]['tag'];
357 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
358 $child[$name][$size] = $this->_struct_to_array($values, $i);
374 * This is adds the contents of the return xml into the array for easier processing.
379 function createArray()
385 $parser = xml_parser_create();
386 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
387 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
388 xml_parse_into_struct($parser, $xml, $values, $index);
389 xml_parser_free($parser);
391 $name = $values[$i]['tag'];
392 $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
393 $array[$name] = $this->_struct_to_array($values, $i);