Scalr\Service\OpenStack\OpenStack::decamelize PHP Method

decamelize() public static method

Decamelizes a string
public static decamelize ( string $str ) : string
$str string A string "FooName"
return string Returns decamelized string "foo_name"
    public static function decamelize($str)
    {
        return strtolower(preg_replace_callback('/([a-z])([A-Z]+)/', function ($m) {
            return $m[1] . '_' . $m[2];
        }, $str));
    }

Usage Example

Beispiel #1
0
 /**
  * Gets the query string for the fields
  *
  * @param   array  $fields The fields list looks like (fild1, field2, .. or fieldN => uriParameterAlias)
  * @return  string Returns the query string
  */
 protected function _getQueryStringForFields(array $fields = null)
 {
     $str = '';
     $reflProperties = $this->_getReflectionProperties();
     if ($fields === null) {
         //Trying to determine fields from reflection class
         $fields = array();
         foreach ($reflProperties as $prop) {
             $fields[$prop->getName()] = OpenStack::decamelize($prop->getName());
         }
     }
     foreach ($fields as $index => $prop) {
         if (!is_numeric($index)) {
             $uriProp = $prop;
             $prop = $index;
         } else {
             $uriProp = $prop;
         }
         if (!isset($reflProperties[$prop])) {
             continue;
         }
         $refProp = $reflProperties[$prop];
         $value = $refProp->getValue($this);
         if ($value !== null) {
             if (is_array($value) || $value instanceof \Traversable) {
                 foreach ($value as $v) {
                     if ($v instanceof \DateTime) {
                         $v = $v->format('c');
                     }
                     $str .= '&' . $uriProp . '=' . rawurlencode((string) $v);
                 }
             } else {
                 if ($value instanceof \DateTime) {
                     $value = $value->format('c');
                 }
                 $str .= '&' . $uriProp . '=' . rawurlencode((string) $value);
             }
         }
         unset($uriProp);
     }
     return $str;
 }