/** * Retorna o utltimo id informado. * @return mixed */ public static function getRouteId($name = null) { $route = $name === null ? Route::current() : Route::getRoutes()->getByName($name); $last = Str::last(Str::before($route->uri())); $id_name = Str::startsWith($last, '{') ? str_replace(['{', '}'], '', $last) : sprintf('%s_id', $last); return Route::input($id_name); }
/** * Retorna informações do XML com base no caminho separado por ponto. * * @param $key * @return XMLGetResponse */ public function get($key) { if (Str::contains($key, '|')) { $opcoes = explode('|', $key); foreach ($opcoes as $op) { $achado = $this->get($op); if (!$achado->isNull()) { return $achado; } } return new XMLGetResponse(null); } $base = $this; $niveis = $key == '' ? [] : explode('.', $key); foreach ($niveis as $i => $nivel) { // Verificar se base eh nula if (is_null($base)) { return new XMLGetResponse(null); } $elem = $base->getElementsByTagName($nivel); $base = $elem->length > 0 ? $elem->item(0) : null; } // Retornar o conteúdo do ultimo nível if (!is_null($base)) { return new XMLGetResponse($base); } return new XMLGetResponse(null); }
/** * Get the services provided by the provider. * * @return array */ public function provides() { $list = []; // Carregar lista de provider dos comandos $list = array_merge($list, Str::format(array_keys($this->commands), 'command.%s')); // Carregar lista de providers $list = array_merge($list, array_keys($this->providers)); return $list; }
/** * Valida um convite. * * @param string $numero Numero do convite * @param null|string $id Identificacao do utilizado se convite restrito * @return bool */ public function check($numero, $id = null) { $convite = $this->get($numero); // Verificar se convite esta restrito a algum id if (Str::is($convite->restrict, trim($id)) != true) { error('Você não tem permissão para utilizar este convite (%s)', $numero); } return $convite; }
/** * Montar estrutura de busca pelo $searchQuery. * * @param $searchQuery * * @return array */ public function execute($searchQuery) { if (count($this->fields) == 0) { return []; } if ($searchQuery == '') { return []; } $searchQuery = Str::ascii($searchQuery); $tokens = $this->tokens($searchQuery); $list = []; foreach ($this->fields as $key => $field) { $type = $this->model->getAttrCast($key); $method = sprintf('field%s', Str::studly($type)); $field = $this->prepareField($field); if (method_exists($this, $method)) { if (array_key_exists($key, $list) != true) { $list[$key] = []; } call_user_func_array([$this, $method], [&$list[$key], $field, $tokens]); } } return $list; }
/** * Ligacao um-para-um. * @param $related * @param null $foreignKey * @param null $relationUri * @param null $relation * @return BelongsTo */ public function belongsTo($related, $foreignKey = null, $relationUri = null, $relation = null) { // Se relation for nulo, descobrir campo de ligacao if (is_null($relation)) { list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $relation = $caller['function']; } // Se foreign key for nula, auto assumir pelo relation if (is_null($foreignKey)) { $foreignKey = Str::snake($relation) . '_id'; } $instance = new $related(); // Se uri for nula, deve pegar do related if (is_null($relationUri)) { $relationUri = sprintf('%s/{key}', $instance->getResource()); } return new BelongsTo($this, $instance, $foreignKey, $relationUri, $relation); }
/** * Registrar metodos do modelo. * * @param $uri * @param array $options * @param string $breadcrumbs */ public static function register($uri, array $options = []) { $controller = '\\' . get_called_class(); $methods = ['index', 'create', 'show', 'store', 'update', 'command', 'delete']; $methods = self::getResourceMethods($methods, $options); $id_name = array_key_exists('id_name', $options) ? $options['id_name'] : Str::last($uri) . '_id'; $middlewares = array_key_exists('middlewares', $options) ? $options['middlewares'] : []; $middleware = array_key_exists('middleware', $options) ? (array) $options['middleware'] : []; // Index if (in_array('index', $methods)) { $args = ['uses' => $controller . '@apiIndex']; $middle = array_merge([], $middleware, isset($middlewares['index']) ? $middlewares['index'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::get($uri, $args); } // Create if (in_array('create', $methods)) { $args = ['uses' => $controller . '@apiCreate']; $middle = array_merge([], $middleware, isset($middlewares['create']) ? $middlewares['create'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::get($uri . '/create', $args); } // Show if (in_array('show', $methods)) { $args = ['uses' => $controller . '@apiShow']; $middle = array_merge([], $middleware, isset($middlewares['show']) ? $middlewares['show'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::get($uri . '/{' . $id_name . '}', $args)->where([$id_name => '[0-9]+']); } // Store if (in_array('store', $methods)) { $args = ['uses' => $controller . '@apiStore']; $middle = array_merge([], $middleware, isset($middlewares['store']) ? $middlewares['store'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::post($uri, $args); } // Update if (in_array('update', $methods)) { $args = ['uses' => $controller . '@apiUpdate']; $middle = array_merge([], $middleware, isset($middlewares['update']) ? $middlewares['update'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::post($uri . '/{' . $id_name . '}', $args)->where([$id_name => '[0-9]+']); } // Command if (in_array('command', $methods)) { $args = ['uses' => $controller . '@apiCommand']; $middle = array_merge([], $middleware, isset($middlewares['command']) ? $middlewares['command'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::post($uri . '/{' . $id_name . '}/{command}', $args)->where([$id_name => '[0-9]+']); } // Delete if (in_array('delete', $methods)) { $args = ['uses' => $controller . '@apiDelete']; $middle = array_merge([], $middleware, isset($middlewares['delete']) ? $middlewares['delete'] : []); if (count($middle) > 0) { $args['middleware'] = $middle; } Route::delete($uri . '/{ids}', $args); } }
/** * Verificar se um campo deve ser ignorado na hora de aplicar do request no model. * * @param $name * * @return bool */ protected function hasApply($name) { foreach ($this->applyIgnores as $pattern) { if (Str::is($pattern, $name)) { return false; } } return true; }
public function testLast() { $str = Str::last('NetForce/Sistemas/Ultima'); $this->assertEquals('Ultima', $str); }
/** * POST: Command /{id}/{comand}. */ public function command() { return DB::transaction(function () { // Carregar parametros $id = $this->getRouteId(); $command = Route::input('command'); // Carregar model $this->model = $this->getModel($id); $method = 'command' . Str::studly($command); if (method_exists($this, $method) != true) { error('Comando %s não foi implementado', $command); } return call_user_func_array([$this, $method], [$id]); }); }