/** * @return $this */ public function withImages() { if (!$this->extended->contains("images")) { return $this->extend('images'); } return $this; }
/** * Determine if a key exists in the collection. * * @param mixed $key * @param mixed $value * @return bool */ public function contains($key, $value = null) { if (func_num_args() == 1) { return !is_null($this->find($key)); } return parent::contains($key, $value); }
/** * Generate documentation with the name and version. * * @param string $name * @param string $version * * @return bool */ public function generate(Collection $controllers, $name, $version) { $resources = $controllers->map(function ($controller) use($version) { $controller = $controller instanceof ReflectionClass ? $controller : new ReflectionClass($controller); $actions = new Collection(); // Spin through all the methods on the controller and compare the version // annotation (if supplied) with the version given for the generation. // We'll also build up an array of actions on each resource. foreach ($controller->getMethods() as $method) { if ($versionAnnotation = $this->reader->getMethodAnnotation($method, Annotation\Versions::class)) { if (!in_array($version, $versionAnnotation->value)) { continue; } } if ($annotations = $this->reader->getMethodAnnotations($method)) { if (!$actions->contains($method)) { $actions->push(new Action($method, new Collection($annotations))); } } } $annotations = new Collection($this->reader->getClassAnnotations($controller)); return new Resource($controller->getName(), $controller, $annotations, $actions); }); return $this->generateContentsFromResources($resources, $name); }
/** * @param Buyable $item * @param int $amount * @return $this */ public function addItem(Buyable $item, $amount = 1) { //注意此处$value传入引用, 导致filter()返回值是已被修改过amount的. //然而, 由于filter在对数组$this->_items进行foreach操作的时候 //$key和$value都是传值, 所以$this->_items并不会被改变. //可以通过修改filter函数来达到目的, 但这样并不是很elegant的做法 //这里通过获取到$key值,来做到在原数组中手动去修改. $has_same_items = $this->_items->contains(function ($key, $value) use($item, $amount) { if ($value['buyable']['id'] == $item->getIdentifer()) { $this->addExistsIntoItems($key, $amount, $item); return true; } }); if (!$has_same_items) { $this->addNewIntoItems($item, $amount); } return $this; }
/** * Recursively build collection of ancestor category IDs. * * @return \Illuminate\Support\Collection */ public function getAncestors() { $ancestors = new Collection(); $ancestor = $this; while (($ancestor = $ancestor->parent) && !$ancestors->contains($ancestor)) { $ancestors->push($ancestor); break; } return $ancestors; }
protected function handleAlteredFile($file_path) { if ($this->baseline->contains($file_path) && ($this->baseline[$file_path]['hash'] != $this->current[$file_path]['hash'] || $this->baseline[$file_path]['last_modified'] != $this->current[$file_path]['last_modified'])) { $this->altered->put($file_path, $this->current[$file_path]); //-- add the baseline_hash $this->altered[$file_path]['baseline_hash'] = $this->baseline[$file_path]['hash']; //-- update altered file in baseline table BaselineFile::updateFromFile($this->current[$file_path], $this->account); $this->saveAlteredFileToHistory($file_path); } }
/** * Determine if a key exists in the collection. * * @param mixed $key * @param mixed $value * @return bool */ public function contains($key, $value = null) { if (func_num_args() == 1 && !$key instanceof Closure) { $key = $key instanceof Model ? $key->getKey() : $key; return $this->filter(function ($m) use($key) { return $m->getKey() === $key; })->count() > 0; } elseif (func_num_args() == 2) { return $this->where($key, $value)->count() > 0; } return parent::contains($key); }
/** * Determine if a key exists in the collection. * * @param mixed $key * @param mixed $value * @return bool */ public function contains($key, $value = null) { if (func_num_args() == 2) { return parent::contains($key, $value); } if ($this->useAsCallable($key)) { return parent::contains($key); } $key = $key instanceof Model ? $key->getKey() : $key; return parent::contains(function ($k, $m) use($key) { return $m->getKey() == $key; }); }
/** * Get all the controller instances. * * @return array */ protected function getControllers() { $controllers = new Collection(); foreach ($this->router->getRoutes() as $collections) { foreach ($collections as $route) { if ($controller = $route->getController()) { if (!$controllers->contains($controller)) { $controllers->push($controller); } } } } return $controllers; }
protected function createMissingBooks(Author $author, Collection $authorData, Collection $existingBooks) { $missingBooks = []; debug('Creating missing books', $authorData); foreach ($authorData as $googleBookId => $book) { if ($existingBooks->contains('google_books_id', $googleBookId)) { continue; } $newBook = $this->addBook($book, $googleBookId); foreach ($book['authors'] as $authorName) { if ($authorName != $author->name) { $author = Author::firstOrCreate(['name' => $authorName]); } $newBook->authors()->create(['author_id' => $author->id, 'book_id' => $newBook->id]); } $missingBooks[] = $newBook; } return $missingBooks; }
function getDiffFiles(array $files, \Illuminate\Support\Collection $seeded, \LaravelSeed\Contracts\ProviderInterface $provider) { $edited = []; array_map(function ($seed) use($files, &$edited, $provider) { $fullPath = getFullPathSource($seed->name, $provider); if (!in_array($fullPath, $files)) { return false; } $key = array_search($fullPath, $files); $filemtime = filemtime($files[$key]); if ($filemtime > $seed->hash) { $edited[] = $fullPath; } return false; }, $seeded->toArray()); $diff = []; array_walk($files, function ($file) use($seeded, &$diff) { $filename = pathinfo($file)['filename']; if (!$seeded->contains('name', $filename)) { $diff[] = $file; } }); return array_merge($diff, $edited); }
public function contains($key) { return $this->data->contains($key); }
public function getNumProtected($projectUuid) { $packages = new Collection(); if (!strpos($projectUuid, '+')) { // collect packages shared with a single project // $packageVersionSharings = PackageVersionSharing::where('project_uuid', '=', $projectUuid)->get(); for ($i = 0; $i < sizeof($packageVersionSharings); $i++) { $packageVersion = PackageVersion::where('package_version_uuid', '=', $packageVersionSharings[$i]->package_version_uuid)->first(); $package = Package::where('package_uuid', '=', $packageVersion->package_uuid)->first(); if ($package && !$packages->contains($package)) { $packages->push($package); // add to packages query // if (!isset($packagesQuery)) { $packagesQuery = Package::where('package_uuid', '=', $package->package_uuid); } else { $packagesQuery = $packagesQuery->orWhere('package_uuid', '=', $package->package_uuid); } // add filters // $packagesQuery = PackageTypeFilter::apply($packagesQuery); $packagesQuery = DateFilter::apply($packagesQuery); $packagesQuery = LimitFilter::apply($packagesQuery); } } } else { // collect packages shared with multiple projects // $projectUuids = explode('+', $projectUuid); foreach ($projectUuids as $projectUuid) { $packageVersionSharings = PackageVersionSharing::where('project_uuid', '=', $projectUuid)->get(); for ($i = 0; $i < sizeof($packageVersionSharings); $i++) { $packageVersion = PackageVersion::where('package_version_uuid', '=', $packageVersionSharings[$i]->package_version_uuid)->first(); $package = Package::where('package_uuid', '=', $packageVersion->package_uuid)->first(); if ($package && !$packages->contains($package)) { $packages->push($package); // add to packages query // if (!isset($packagesQuery)) { $packagesQuery = Package::where('package_uuid', '=', $package->package_uuid); } else { $packagesQuery = $packagesQuery->orWhere('package_uuid', '=', $package->package_uuid); } // add filters // $packagesQuery = PackageTypeFilter::apply($packagesQuery); $packagesQuery = DateFilter::apply($packagesQuery); $packagesQuery = LimitFilter::apply($packagesQuery); } } } } // perform query // if (isset($packagesQuery)) { return $packagesQuery->count(); } else { return 0; } }
/** * Sort the given middleware by priority. * * @param \Illuminate\Support\Collection $middlewares * @return \Illuminate\Support\Collection */ protected function sortMiddleware(Collection $middlewares) { $priority = collect($this->middlewarePriority); $sorted = collect(); foreach ($middlewares as $middleware) { if ($sorted->contains($middleware)) { continue; } if (($index = $priority->search($middleware)) !== false) { $sorted = $sorted->merge($priority->take($index)->filter(function ($middleware) use($middlewares, $sorted) { return $middlewares->contains($middleware) && !$sorted->contains($middleware); })); } $sorted[] = $middleware; } return $sorted; }
/** * Get the ID of the ability that matches one of the applicable abilities. * * @param \Illuminate\Support\Collection $abilityMap * @param \Illuminate\Support\Collection $applicable * @return int|null */ protected function getMatchedAbilityId(Collection $abilityMap, Collection $applicable) { foreach ($abilityMap as $id => $identifier) { if ($applicable->contains($identifier)) { return $id; } } }
/** * Find out if this element is set to render. * * @return bool that is true only if all conditions for rendering this element evaluates to true */ public function willRenderInHtml() { return !$this->render_in_html->contains(function ($key, $value) { return !$this->evaluate($value); }); }
/** * Determine if the flash has the level. * * @param string $level * @return boolean */ public function has($level) { return $this->current->contains('level', $level); }
/** * Hash any hashable columns */ protected function hashColumns(Collection $columns) { if (is_array($this->hashable) && !empty($this->hashable)) { foreach ($this->hashable as $hashable) { if ($columns->contains($hashable)) { $columns->put($hashable, bcrypt($columns[$hashable])); } } } return $columns; }
public function testContains() { $c = new Collection([1, 3, 5]); $this->assertTrue($c->contains(1)); $this->assertFalse($c->contains(2)); $this->assertTrue($c->contains(function ($value) { return $value < 5; })); $this->assertFalse($c->contains(function ($value) { return $value > 5; })); $c = new Collection([['v' => 1], ['v' => 3], ['v' => 5]]); $this->assertTrue($c->contains('v', 1)); $this->assertFalse($c->contains('v', 2)); $c = new Collection(['date', 'class', (object) ['foo' => 50]]); $this->assertTrue($c->contains('date')); $this->assertTrue($c->contains('class')); $this->assertFalse($c->contains('foo')); }
/** * Sort the given middleware by priority. * * @param \Illuminate\Support\Collection $middlewares * @return array */ protected function sortMiddleware(Collection $middlewares) { $priority = $this->middlewarePriority; $sorted = []; foreach ($middlewares as $middleware) { if (in_array($middleware, $sorted)) { continue; } if (($index = array_search($middleware, $priority)) !== false) { $sorted = array_merge($sorted, array_filter(array_slice($priority, 0, $index), function ($middleware) use($middlewares, $sorted) { return $middlewares->contains($middleware) && !in_array($middleware, $sorted); })); } $sorted[] = $middleware; } return $sorted; }
/** * Get the full tag list. * * - tag name * - tag slug * - number of article(s) for a tag * * @return \Illuminate\Support\Collection */ public function getTagsList() { $tags = new Collection(); $this->getArticles()->each(function ($article) use(&$tags) { foreach ($article->getTags() as $tag) { if (!$tags->contains('name', $tag)) { $tags->push(['name' => $tag, 'slug' => str_slug($tag), 'articles' => $this->getArticlesByTag($tag)->count()]); } } }); return $tags; }
/** * Get a subset of lines where the key matches at least one of the given glob-style patterns. * * @param string|array|Collection $patterns * * @return static */ public function whereKeysLike($patterns) { if (!$patterns instanceof Collection) { $patterns = new Collection((array) $patterns); } /* @var Collection $patterns */ if ($patterns->filter()->isEmpty()) { return new static($this->all()); } /** * Return a subset of pairs that match any of the given patterns. */ return $this->pairs()->filter(function (LineInterface $line) use($patterns) { return $patterns->contains(function ($index, $pattern) use($line) { return fnmatch($pattern, $line->key()); }); }); }
public function selectBanner() { $banners = new IlluminateCollection(['banners/parlamentares/bg_fotos01.jpg', 'banners/parlamentares/bg_fotos02.jpg', 'banners/parlamentares/bg_fotos03.jpg', 'banners/parlamentares/bg_fotos04.jpg', 'banners/parlamentares/bg_fotos05.jpg', 'banners/parlamentares/bg_fotos06.jpg', 'banners/parlamentares/bg_fotos07.jpg', 'banners/parlamentares/bg_fotos08.jpg', 'banners/parlamentares/bg_fotos09.jpg', 'banners/parlamentares/bg_fotos10.jpg']); $usedBanners = Session::get('used_banners') ?: []; if (count($usedBanners) >= $banners->count()) { $usedBanners = []; } $usedBanners = new IlluminateCollection($usedBanners); while (true) { $banner = $banners->random(); if (!$usedBanners->contains($banner)) { break; } } $usedBanners->put(null, $banner); Session::put('used_banners', $usedBanners->toArray()); return $banner; }