Esempio n. 1
0
 public function testToSnake()
 {
     self::assertEquals('snake_case', Str::toSnakeCase('SnakeCase'));
     self::assertEquals('snake-case', Str::toSnakeCase('SnakeCase', '-'));
     self::assertEquals('simple', Str::toSnakeCase('Simple'));
     self::assertEquals('', Str::toSnakeCase(''));
 }
 public static function getUri($controller, $method)
 {
     $prefix = static::getPrefix($controller);
     $separator = $prefix ? '/' : '';
     return $prefix . $separator . Str::toSnakeCase($method, '-');
 }
Esempio n. 3
0
/**
 * Returns names of writable properties for objects and classes or existing keys for arrays.
 *
 * Only public object properties and properties having setters considered writable.
 *
 * For setters, this function will return property names based on setter names
 * (setter names are converted to snake case, 'set' prefixes are removed).
 *
 * Detecting properties by setters can be disabled by specifying second argument as FALSE.
 *
 * @param object|string|array $target object or class name or array
 * @param bool $useSetters (optional, default value: true) if true, properties having setters will be added to results
 * @return string[] names of writable properties
 */
function getWritable($target, $useSetters = true)
{
    static $writable = [];
    if (is_array($target)) {
        return array_keys($target);
    }
    $class = is_object($target) ? get_class($target) : $target;
    $cacheKey = $class . ($useSetters ? '+s' : '');
    if (!array_key_exists($cacheKey, $writable)) {
        $writable[$cacheKey] = array_keys(get_class_vars($class));
        if ($useSetters) {
            $setters = getSetters($class);
            foreach ($setters as $setter) {
                $writable[$cacheKey][] = Str::toSnakeCase(substr($setter, 3));
            }
        }
    }
    return $writable[$cacheKey];
}
Esempio n. 4
0
 /**
  * Returns names of writable properties.
  *
  * For arrays, keys will be returned.
  *
  * If used with $useSetters option, corresponding property names in snake case will returned.
  *
  * @param object|string|array $src object or class name or array
  * @param bool $useSetters
  * @return array
  */
 public static function getWritable($src, $useSetters = true)
 {
     if (is_array($src)) {
         return array_keys($src);
     }
     $class = is_object($src) ? get_class($src) : $src;
     $cacheKey = $class . ($useSetters ? '+s' : '');
     if (!array_key_exists($cacheKey, self::$writable)) {
         self::$writable[$cacheKey] = array_keys(get_class_vars($class));
         if ($useSetters) {
             $setters = self::getSetters($class);
             foreach ($setters as $setter) {
                 self::$writable[$cacheKey][] = Str::toSnakeCase(substr($setter, 3));
             }
         }
     }
     return self::$writable[$cacheKey];
 }