public function testStrReplaceFirst() { $key = 'foo.*.bar.*'; $results = Str::strReplaceFirst('*', '0', $key); $this->assertEquals('foo.0.bar.*', $results); $results = Str::strReplaceFirst('no', '0', $results); $this->assertEquals('foo.0.bar.*', $results); }
public function back() { $app = App::instance(); if (Str::startsWith($app->request->referrer, 'http://localhost-router/')) { header("Location: " . $this->request->referrer); exit; } header("Location: http://localhost-router/"); exit; }
/** * Accepts a dot-notation array key with wildcards and expands them * into an array of dot-notation array keys in numerical order. * * @param string $key - dot notation * @param int $size - number of keys to generate per wildcard. Defaults to 10. * @return array */ public static function expandKeys($key, $size = 10) { $keys = []; $original_key = $key; for ($i = 0; $i < $size; $i++) { $k = Str::strReplaceFirst('*', $i, $original_key); if (strpos($k, '*') !== false) { $sub_keys = static::expandKeys($k, $size); foreach ($sub_keys as $sk) { $keys[] = $sk; } } else { $keys[] = $k; } } return $keys; }
/** * Dispatch a Assets File Response. * * @return \Symfony\Component\HttpFoundation\Response|null */ public function dispatch(SymfonyRequest $request) { // For proper Assets serving, the file URI should be either of the following: // // /templates/default/assets/css/style.css // /modules/blog/assets/css/style.css // /assets/css/style.css if (!in_array($request->method(), array('GET', 'HEAD'))) { // The Request Method is not valid for Asset Files. return null; } // Calculate the Asset File path, looking for a valid one. $uri = $request->path(); if (preg_match('#^(templates|modules)/([^/]+)/assets/(.*)$#i', $uri, $matches)) { $folder = $matches[2]; // Adjust the name of the requested folder, the short ones becoming uppercase. $folder = strlen($folder) > 3 ? Str::studly($folder) : strtoupper($folder); // $path = str_replace('/', DS, $matches[3]); // $baseName = strtolower($matches[1]); $filePath = APPDIR . ucfirst($baseName) . DS . $folder . DS . 'Assets' . DS . $path; } else { if (preg_match('#^(assets|vendor)/(.*)$#i', $uri, $matches)) { $path = $matches[2]; // $baseName = strtolower($matches[1]); if ($baseName == 'vendor' && !Str::startsWith($path, $this->paths)) { // The current URI is not a valid Asset File path on Vendor. return null; } $filePath = ROOTDIR . $baseName . DS . str_replace('/', DS, $path); } else { // The current URI is not a valid Asset File path. return null; } } // Create a Response for the current Asset File path. $response = $this->serve($filePath, $request); // Prepare the Response instance. $response->prepare($request); return $response; }
/** * Determine if the relationship is nested. * * @param string $name * @param string $relation * @return bool */ protected function isNested($name, $relation) { $dots = str_contains($name, '.'); return $dots && Str::startsWith($name, $relation . '.'); }
/** * Add a single dynamic where clause statement to the query. * * @param string $segment * @param string $connector * @param array $parameters * @param int $index * @return void */ protected function addDynamic($segment, $connector, $parameters, $index) { $bool = strtolower($connector); $this->where(Str::snake($segment), '=', $parameters[$index], $bool); }
public function getModules() { if (isset(static::$modules)) { return static::$modules; } // $modules = $this->config->get('modules.modules', array()); $modules = array_map(function ($slug, $properties) { $autoload = array('config', 'events', 'filters', 'routes'); $options = array_get($properties, 'autoload', array()); if (!empty($options)) { $autoload = array_intersect($options, $autoload); } array_push($autoload, 'bootstrap'); // $namespace = isset($properties['namespace']) ? $properties['namespace'] : Str::studly($slug); return array_merge(array('slug' => $slug, 'name' => isset($properties['name']) ? $properties['name'] : $namespace, 'namespace' => $namespace, 'enabled' => isset($properties['enabled']) ? $properties['enabled'] : true, 'order' => isset($properties['order']) ? $properties['order'] : 9001, 'autoload' => $autoload), $properties); }, array_keys($modules), $modules); return static::$modules = Collection::make($modules)->sortBy('order'); }
/** * Determine if the given path is a valid URL. * * @param string $path * @return bool */ public function isValidUrl($path) { if (Str::startsWith($path, ['#', '//', 'mailto:', 'tel:', 'http://', 'https://'])) { return true; } return filter_var($path, FILTER_VALIDATE_URL) !== false; }
/** * Dynamically bind flash data in the session. * * @param string $method * @param array $parameters * @return void * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (str_starts_with($method, 'with')) { return $this->with(Str::snake(substr($method, 4)), $parameters[0]); } throw new \BadMethodCallException("Method [{$method}] does not exist on Redirect."); }
/** * Handle dynamic calls to class methods. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { $rule = Str::snake(substr($method, 8)); if (isset($this->extensions[$rule])) { return $this->callExtension($rule, $parameters); } throw new \BadMethodCallException("Method [{$method}] does not exist."); }
/** * Attempt to guess the name of the inverse of the relation. * * @return string */ protected function guessInverseRelation() { $relation = Str::plural(class_basename($this->getParent())); return Str::camel($relation); }
/** * Determine if a set mutator exists for an attribute. * * @param string $key * @return bool */ public function hasSetMutator($key) { return method_exists($this, 'set' . Str::studly($key) . 'Attribute'); }
/** * Create a new file Download Response. * * @param \SplFileInfo|string $file * @param string $name * @param array $headers * @param null|string $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public static function download($file, $name = null, array $headers = array(), $disposition = 'attachment') { $response = new BinaryFileResponse($file, 200, $headers, true, $disposition); if (!is_null($name)) { return $response->setContentDisposition($disposition, $name, Str::ascii($name)); } return $response; }
/** * Determine if the MAC for the given payload is valid. * * @param array $payload * @return bool * * @throws \RuntimeException */ protected function validMac(array $payload) { $bytes = Str::randomBytes(16); $calcMac = hash_hmac('sha256', $this->hash($payload['iv'], $payload['value']), $bytes, true); return Str::equals(hash_hmac('sha256', $payload['mac'], $bytes, true), $calcMac); }
/** * Generate a random alpha-numeric string. * * @param int $length * @return string * * @throws \RuntimeException */ function str_random($length = 16) { return Str::random($length); }
/** * Parse the connection into an array of the name and read / write type. * * @param string $name * @return array */ protected function parseConnectionName($name) { $name = $name ?: $this->getDefaultConnection(); return Str::endsWith($name, ['::read', '::write']) ? explode('::', $name, 2) : [$name, null]; }