/** * Handle the command. * * @param ModuleCollection $modules * @param Decorator $decorator * @param Repository $config * @param Container $container * @param Request $request * @param Search $search * @return LengthAwarePaginator */ public function handle(ModuleCollection $modules, Decorator $decorator, Repository $config, Container $container, Request $request, Search $search) { /* @var Query $query */ $query = $search->index($this->criteria->option('index', 'default')); $constraint = $this->criteria->option('in'); if (!empty($constraint) && is_string($constraint)) { $query = $query->search('stream', $constraint, ['required' => true]); } if (!empty($constraint) && is_array($constraint)) { /* @var Module $module */ foreach ($modules->withConfig('search') as $module) { foreach ($config->get($module->getNamespace('search')) as $model => $definition) { /* @var EntryInterface $model */ $model = $container->make($model); $stream = $model->getStreamNamespace() . '.' . $model->getStreamSlug(); if (!in_array($stream, $constraint)) { $query = $query->search('stream', $stream, ['required' => false, 'prohibited' => true]); } } } } foreach ($this->criteria->getOperations() as $operation) { $query = call_user_func_array([$query, $operation['name']], $operation['arguments']); } $page = $request->get('page', 1); $perPage = $this->criteria->option('paginate', $this->criteria->option('limit', 15)); $query->limit($perPage, ($page - 1) * $perPage); $collection = new SearchCollection(array_map(function ($result) use($decorator) { return $decorator->decorate(new SearchItem($result)); }, $query->get())); return (new LengthAwarePaginator($collection, $query->count(), $perPage, $page))->setPath($request->path())->appends($request->all()); }
/** * Handle the command. * * @param FolderRepositoryInterface $folders * @param Decorator $decorator * @return null|FolderPresenter */ public function handle(FolderRepositoryInterface $folders, Decorator $decorator) { if (is_numeric($this->identifier)) { return $decorator->decorate($folders->find($this->identifier)); } if (is_string($this->identifier)) { return $decorator->decorate($folders->findBySlug($this->identifier)); } return null; }
/** * Return labels for each entry. * * @param null $text * @param string $context * @param string $size * @return array */ public function labels($text = null, $context = null, $size = null) { $decorator = new Decorator(); return array_map(function ($entry) use($decorator, $text, $context, $size) { /* @var EntryPresenter $entry */ if (!$entry instanceof EntryPresenter) { $entry = $decorator->decorate($entry); } return $entry->label($text, $context, $size); }, $this->all()); }
/** * Handle the command. * * @param FileRepositoryInterface $files * @return \Anomaly\FilesModule\File\Contract\FileInterface|null */ public function handle(FileRepositoryInterface $files, FolderRepositoryInterface $folders, Decorator $decorator) { if (is_numeric($this->identifier)) { return $decorator->decorate($files->find($this->identifier)); } if (is_string($this->identifier)) { list($folder, $name) = explode('/', $this->identifier); if (!($folder = $folders->findBySlug($folder))) { return null; } return $decorator->decorate($files->findByNameAndFolder($name, $folder)); } return null; }
/** * Make a value from the parameters and entry. * * @param $parameters * @param $payload * @param array $payload * @return mixed|string */ public function make($parameters, $entry, $term = 'entry', $payload = []) { $payload[$term] = $entry; /* * If a flat value was sent in * then convert it to an array. */ if (!is_array($parameters)) { $parameters = ['value' => $parameters]; } $value = array_get($parameters, 'value'); /* * If the value is a view path then return a view. */ if ($view = array_get($parameters, 'view')) { return view($view, ['value' => $value, $term => $entry]); } /* * If the value uses a template then parse it. */ if ($template = array_get($parameters, 'template')) { return $this->template->render($template, ['value' => $value, $term => $entry]); } /* * If the entry is an instance of EntryInterface * then try getting the field value from the entry. */ if ($entry instanceof EntryInterface && $entry->getField($value)) { /* @var EntryInterface $relation */ if ($entry->assignmentIsRelationship($value) && ($relation = $entry->{camel_case($value)})) { if ($relation instanceof EloquentModel) { $value = $relation->getTitle(); } } else { $value = $entry->getFieldValue($value); } } /* * Decorate the entry object before * sending to decorate so that data_get() * can get into the presenter methods. */ $payload[$term] = $entry = $this->decorator->decorate($entry); /* * If the value matches a dot notation * then parse it as a template. */ if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { $value = $this->template->render("{{ {$value}|raw }}", $payload); } /* * If the value matches a method in the presenter. */ if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { if (method_exists($entry, camel_case($match[1]))) { $value = $entry->{camel_case($match[1])}(); } } $payload[$term] = $entry; /* * By default we can just pass the value through * the evaluator utility and be done with it. */ $value = $this->evaluator->evaluate($value, $payload); /* * Lastly, prepare the entry to be * parsed into the string. */ if ($entry instanceof Arrayable) { $entry = $entry->toArray(); } /* * Parse the value with the entry. */ if ($wrapper = array_get($parameters, 'wrapper')) { $value = $this->parser->parse($wrapper, ['value' => $value, $term => $entry]); } /* * Parse the value with the value too. */ if (is_string($value)) { $value = $this->parser->parse($value, ['value' => $value, $term => $entry]); } /* * If the value looks like a language * key then try translating it. */ if (is_string($value) && str_is('*.*.*::*', $value)) { $value = trans($value); } /* * If the value looks like a render-able * string then render it. */ if (is_string($value) && str_contains($value, '{{')) { $value = $this->template->render($value, [$term => $entry]); } return $value; }