Example #1
0
 /**
  * Recursively converts an array to an object.
  *
  * @param array $array                  The array to convert
  * @param bool  $maintainNumericIndices If true, numeric indecies are retained
  * @param string|null $className        Class to create for the returned object,
  *                                      if null, stdClass is used
  *
  * @return object                       The array as an object instance
  */
 public static function toObject($array, $maintainNumericIndices = false, $className = null)
 {
     if (!is_array($array)) {
         return $array;
     }
     $className = $className ?: 'stdClass';
     $hasNumericIndices = array_values($array) === $array && $maintainNumericIndices;
     $new = $hasNumericIndices ? array() : new $className();
     foreach ($array as $key => $value) {
         if (is_array($value)) {
             $value = self::toObject($value, $maintainNumericIndices);
         }
         if ($hasNumericIndices) {
             $new[$key] = $value;
         } else {
             $new->{Text::toCamelCaps($key)} = $value;
         }
     }
     return $new;
 }
Example #2
0
 /**
  * Converts string to a slug, i.e. a lowercase string where non-url friendly characters are replaced with a
  * hyphen
  *
  * Taken from http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php
  *
  * @param $text     The variable to be cast
  *
  * @return string
  */
 public function slug($text)
 {
     return TextFunction::toSlug($text, false);
 }