Ejemplo 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(''));
 }
 protected function applyInternal(ComponentInterface $component, array $operation)
 {
     foreach ($operation as $key => $value) {
         if ($value instanceof Closure) {
             $func = $value;
             $arguments = [$component, $this->helper];
         } else {
             $methodName = lcfirst(Str::toCamelCase($key));
             if (!method_exists($this->helper, $methodName)) {
                 return;
             }
             $func = [$this->helper, $methodName];
             $arguments = array_merge([$component], is_array($value) ? $value : [$value]);
         }
         call_user_func_array($func, $arguments);
     }
 }
 public static function getUri($controller, $method)
 {
     $prefix = static::getPrefix($controller);
     $separator = $prefix ? '/' : '';
     return $prefix . $separator . Str::toSnakeCase($method, '-');
 }
 protected function getConditionMethod($key)
 {
     return 'check' . Str::toCamelCase($key) . 'Condition';
 }
Ejemplo n.º 5
0
/**
 * Extracts value specified by property / field / method name from object or array by reference if possible.
 *
 * This function acts like `mp\getValue` with only difference that value will be returned by reference if possible.
 *
 * @experimental
 * @param array|object $src
 * @param string $propertyName
 * @param mixed $default
 * @param string|null $delimiter
 * @return mixed|null
 */
function &getValueByRef(&$src, $propertyName, $default = null, $delimiter = '.')
{
    if (is_array($src) && array_key_exists($propertyName, $src)) {
        return $src[$propertyName];
    }
    $isObject = is_object($src);
    if ($isObject && isset($src->{$propertyName})) {
        // if it's not magic method, return reference
        if (property_exists($src, $propertyName)) {
            return $src->{$propertyName};
            // otherwise (it's __get()) calling $src->{$propertyName} will generate PHP notice:
            // indirect modification of overloaded property has no effect.
            // Therefore we return link to temp variable instead of link to variable itself.
        } else {
            $tmp = $src->{$propertyName};
            return $tmp;
        }
    }
    if ($delimiter && ($pos = strpos($propertyName, $delimiter))) {
        // head(a.b.c) = a
        // tail(a.b.c) = b.c
        $head = substr($propertyName, 0, $pos);
        $tail = substr($propertyName, $pos + 1);
        return getValueByRef(getValueByRef($src, $head, $default, null), $tail, $default, $delimiter);
    }
    if ($isObject) {
        $camelPropName = Str::toCamelCase($propertyName);
        $methods = ['get' . $camelPropName, $propertyName, $camelPropName, 'is' . $camelPropName];
        foreach ($methods as $method) {
            if (method_exists($src, $method)) {
                $result = call_user_func([$src, $method]);
                return $result;
            }
        }
    }
    return $default;
}
Ejemplo n.º 6
0
 /**
  * Extracts value.
  *
  * If $propName = 'prop_name', this method will try to extract data in following order from:
  * - $src['prop_name']
  * - $src->prop_name
  * - $src->getPropName()
  * - $src->prop_name()
  * - $src->isPropName()
  *
  * @experimental
  * @param $src
  * @param string $propName
  * @param $default
  * @return mixed
  */
 public static function getValue($src, $propName, $default = null)
 {
     if (is_array($src)) {
         if (array_key_exists($propName, $src)) {
             return $src[$propName];
         }
     } elseif (is_object($src)) {
         if (isset($src->{$propName})) {
             return $src->{$propName};
         }
         $camelPropName = Str::toCamelCase($propName);
         $methods = ['get' . $camelPropName, $propName, $camelPropName, 'is' . $camelPropName];
         foreach ($methods as $method) {
             if (method_exists($src, $method)) {
                 return call_user_func([$src, $method]);
             }
         }
     }
     return $default;
 }