filter() public method

Run a filter over each of the items.
public filter ( callable $callback = null ) : static
$callback callable
return static
 /**
  * Get an attribute by name
  * @param string $name Attribute name
  * @return ConfigurationAttribute|null
  * @since 1.0
  */
 public function getAttribute($name, $returnValue = true)
 {
     $attribute = $this->attributes->filter(function ($attr) use($name) {
         /** @var ConfigurationAttribute $attr */
         return $attr->getName() == $name;
     })->first();
     return $returnValue && $attribute ? $attribute->getValue() : $attribute;
 }
 protected function filterColumns($filters)
 {
     foreach ($filters as $key => $filter) {
         $this->collection = $this->collection->filter(function ($row) use($key, $filter) {
             return strpos(Str::lower($row[$key]), Str::lower($filter)) !== false;
         });
     }
 }
 /**
  * @param Account    $account
  * @param Collection $startSet
  * @param Collection $endSet
  * @param Collection $backupSet
  *
  * @return Account
  */
 public static function reportFilter(Account $account, Collection $startSet, Collection $endSet, Collection $backupSet)
 {
     // The balance for today always incorporates transactions made on today. So to get todays "start" balance, we sub one day.
     $account->startBalance = '0';
     $account->endBalance = '0';
     $currentStart = $startSet->filter(function (Account $entry) use($account) {
         return $account->id == $entry->id;
     });
     $currentBackup = $backupSet->filter(function (Account $entry) use($account) {
         return $account->id == $entry->id;
     });
     // first try to set from backup
     if (!is_null($currentBackup->first())) {
         $account->startBalance = $currentBackup->first()->balance;
     }
     // overrule with data from start
     if (!is_null($currentStart->first())) {
         $account->startBalance = $currentStart->first()->balance;
     }
     $currentEnd = $endSet->filter(function (Account $entry) use($account) {
         return $account->id == $entry->id;
     });
     if (!is_null($currentEnd->first())) {
         $account->endBalance = $currentEnd->first()->balance;
     }
     return $account;
 }
 /**
  * Perform column search.
  *
  * @return void
  */
 public function columnSearch()
 {
     $columns = $this->request->get('columns');
     for ($i = 0, $c = count($columns); $i < $c; $i++) {
         if ($this->request->isColumnSearchable($i)) {
             $this->isFilterApplied = true;
             $regex = $this->request->isRegex($i);
             $column = $this->getColumnName($i);
             $keyword = $this->request->columnKeyword($i);
             $this->collection = $this->collection->filter(function ($row) use($column, $keyword, $regex) {
                 $data = $this->serialize($row);
                 $value = Arr::get($data, $column);
                 if ($this->isCaseInsensitive()) {
                     if ($regex) {
                         return preg_match('/' . $keyword . '/i', $value) == 1;
                     } else {
                         return strpos(Str::lower($value), Str::lower($keyword)) !== false;
                     }
                 } else {
                     if ($regex) {
                         return preg_match('/' . $keyword . '/', $value) == 1;
                     } else {
                         return strpos($value, $keyword) !== false;
                     }
                 }
             });
         }
     }
 }
 /**
  * @param $name
  * @return Collection
  */
 public function getGroup($name)
 {
     $group = $this->collection->filter(function ($widget) use($name) {
         return $widget->group == $name;
     });
     return $group->sortBy('order');
 }
Example #6
0
 /**
  * @return Collection
  */
 public function generateMenu()
 {
     $menu = new Collection();
     if (!$this->auth->getUser()) {
         return $menu;
     }
     if (config('shapeshifter.menu')) {
         foreach (config('shapeshifter.menu') as $item) {
             $item = $this->parseItem($item);
             $menu->push($item);
         }
     } else {
         foreach ($this->modules->getOrdered() as $module) {
             $attributes = $module->json()->getAttributes();
             $item = $this->parseItem($attributes);
             $menu->push($item);
         }
     }
     return $menu->filter(function ($item) {
         return $this->hasAccessToRoute($item['route']);
     })->map(function ($item) {
         $item['children'] = array_filter($item['children'], function ($item) {
             return $this->hasAccessToRoute($item['route']);
         });
         return $item;
     })->filter(function ($item) {
         return $this->hasAccessToRoute('superuser') || count($item['children']) === 0 && $item['route'] !== null;
     });
 }
 /**
  * Apply given filters on media.
  *
  * @param \Illuminate\Support\Collection $media
  * @param array|callable                 $filter
  *
  * @return Collection
  */
 protected function applyFilterToMediaCollection(Collection $media, $filter) : Collection
 {
     if (is_array($filter)) {
         $filter = $this->getDefaultFilterFunction($filter);
     }
     return $media->filter($filter);
 }
Example #8
0
 /**
  * Delete all elements which are all keys is null
  * 
  * @return void
  */
 private function cleanCollection()
 {
     $this->collection = $this->collection->filter(function (Element $item) {
         if (!$item->allKeyNull()) {
             return true;
         }
     });
 }
 /**
  * @param string $needleClass
  *
  * @return string|null
  */
 public function getTypeByClassName($needleClass)
 {
     $type = $this->types->filter(function (\KodiCMS\Widgets\Contracts\WidgetType $type) use($needleClass) {
         return $type->getClass() == $needleClass or $this->isCorrupt($type->getClass());
     })->first();
     if ($type) {
         return $type->getType();
     }
 }
 /**
  * Return a Collection of all Commands that have not running the undo action
  * @return static
  */
 protected function getNotUndoRunningCommandCollection()
 {
     return $this->commands->filter(function ($item) {
         $value = $item->isUndoRun();
         if ($value === false) {
             return $item;
         }
     });
 }
 /**
  * Show the application welcome screen to the user.
  *
  * @return mixed
  */
 public function index()
 {
     $name = app('orchestra.app')->installed() ? 'welcome' : 'hello';
     $packages = new Collection(config('website.packages', []));
     $components = $packages->filter(function ($package) {
         return in_array('component', $package['type']);
     });
     return view($name, compact('packages', 'components'));
 }
Example #12
0
 /**
  * Activate a boolean property for the given fields.
  * @param $property string fillable|guarded|in_table, etc
  * @param $arguments array|string - * if all fields
  * @return $this
  */
 protected function updateFieldProperty($property, $arguments)
 {
     $fields = $arguments == "*" ? $this->fields : $this->fields->filter(function (FieldBlueprint $field) use($arguments) {
         return in_array($field->getName(), $arguments);
     });
     $fields->each(function (FieldBlueprint $field) use($property) {
         $field->setProperty($property, true);
     });
     return $this;
 }
Example #13
0
 protected function directoriesUsedByBackupJob() : array
 {
     return $this->backupDestinations->filter(function (BackupDestination $backupDestination) {
         return $backupDestination->filesystemType() === 'local';
     })->map(function (BackupDestination $backupDestination) {
         return $backupDestination->disk()->getDriver()->getAdapter()->applyPathPrefix('');
     })->each(function (string $localDiskRootDirectory) {
         $this->fileSelection->excludeFilesFrom($localDiskRootDirectory);
     })->push($this->temporaryDirectory->path())->toArray();
 }
Example #14
0
 /**
  * Search for any prefixes attached to this route.
  *
  * @return string
  */
 protected function getPrefixes()
 {
     $router = app(\Illuminate\Routing\Router::class);
     $this->prefixes = collect(explode('/', $router->getCurrentRoute()->getPrefix()));
     // Remove the last prefix if it matches the controller.
     $this->prefixes = $this->removeControllerFromPrefixes($this->prefixes);
     if ($this->prefixes->count() > 0) {
         $this->prefix = $this->prefixes->filter()->implode('.');
     }
 }
Example #15
0
 private function doInternalSearch(Collection $columns, array $searchColumns)
 {
     if (is_null($this->search) or empty($this->search)) {
         return;
     }
     $value = $this->search;
     $caseSensitive = $this->options['caseSensitive'];
     $toSearch = array();
     // Map the searchColumns to the real columns
     $ii = 0;
     foreach ($columns as $i => $col) {
         if (in_array($columns->get($i)->getName(), $searchColumns)) {
             $toSearch[] = $ii;
         }
         $ii++;
     }
     $self = $this;
     $this->workingCollection = $this->workingCollection->filter(function ($row) use($value, $toSearch, $caseSensitive, $self) {
         for ($i = 0; $i < count($row); $i++) {
             if (!in_array($i, $toSearch)) {
                 continue;
             }
             $column = $i;
             if ($self->getAliasMapping()) {
                 $column = $self->getNameByIndex($i);
             }
             if ($self->getOption('stripSearch')) {
                 $search = strip_tags($row[$column]);
             } else {
                 $search = $row[$column];
             }
             if ($caseSensitive) {
                 if ($self->exactWordSearch) {
                     if ($value === $search) {
                         return true;
                     }
                 } else {
                     if (str_contains($search, $value)) {
                         return true;
                     }
                 }
             } else {
                 if ($self->getExactWordSearch()) {
                     if (strtolower($value) === strtolower($search)) {
                         return true;
                     }
                 } else {
                     if (str_contains(strtolower($search), strtolower($value))) {
                         return true;
                     }
                 }
             }
         }
     });
 }
 /**
  * Show the help information for a single SignatureHandler.
  *
  * @param  Collection|SignatureHandler[] $handlers
  * @param  string $command
  * @return Response
  */
 protected function displayHelpForCommand(Collection $handlers, string $command) : Response
 {
     $helpRequest = clone $this->request;
     $helpRequest->text = $command;
     /** @var \Spatie\SlashCommand\Handlers $handler */
     $handler = $handlers->filter(function (HandlesSlashCommand $handler) use($helpRequest) {
         return $handler->canHandle($helpRequest);
     })->first();
     $field = AttachmentField::create($handler->getFullCommand(), $handler->getHelpDescription());
     return $this->respondToSlack('')->withAttachment(Attachment::create()->addField($field));
 }
 /**
  * @param array $all
  * @return array
  */
 public function getRegisterValues(array $all)
 {
     $values = Collection::make([]);
     $this->columns->filter(function (ColumnInterface $column) {
         return !$column->isLabel();
     })->each(function (ColumnInterface $column) use($all, $values) {
         $name = $column->getName();
         $values->put($name, array_get($all, $column->getName()));
     });
     return $values->toArray();
 }
Example #18
0
 /**
  *  取得满足 filter 条件的 集合
  *  filter 方法实际上是 走的 array_filter($this->items, $callback) 这个数组底层函数
  *
  */
 public function filter()
 {
     /**
      * array_filter($this->collection, function(){})
      */
     $filter = $this->collection->filter(function ($item) {
         return $item['price'] == 100;
     });
     debug($filter);
     return view('index');
 }
Example #19
0
 /**
  * Creates inexistant repositories.
  *
  * @param \Illuminate\Support\Collection $all
  * @param \Illuminate\Support\Collection $existing
  *
  * @return \Illuminate\Support\Collection
  */
 protected function create(Collection $all, Collection $existing)
 {
     return $all->filter(function ($repository) use($existing) {
         $existing = $existing->where('id', $repository->id)->first();
         if ($existing !== null) {
             $existing->update(['name' => $repository->name]);
             return false;
         }
         return true;
     });
 }
Example #20
0
 /**
  * Include only a subset of assets.
  *
  * @param  string|array  $assets
  * @return \Basset\Directory
  */
 public function only($assets)
 {
     $assets = array_flatten(func_get_args());
     // Store the directory instance on a variable that we can inject into the scope of
     // the closure below. This allows us to call the path conversion method.
     $directory = $this;
     $this->assets = $this->assets->filter(function ($asset) use($assets, $directory) {
         $path = $directory->getPathRelativeToDirectory($asset->getRelativePath());
         return in_array($path, $assets);
     });
     return $this;
 }
Example #21
0
 /**
  * Filter out duplicate items from a collection
  * 
  * @param Collection $results
  * @return \Illuminate\Support\Collection filtered results
  */
 public function filterById(Collection $results)
 {
     $ids = [];
     return $results->filter(function ($item) use(&$ids) {
         $itemId = $item->getId();
         if (!empty($itemId) && !in_array($itemId, $ids)) {
             $ids[] = $itemId;
             return true;
         }
         return false;
     });
 }
Example #22
0
 /**
  * findItemByHref
  *
  * @param $href
  *
  * @return \Codex\Menus\Node|null
  */
 public function findItemByHref($href)
 {
     /** @var Collection $found */
     $found = $this->items->filter(function (Node $item) use($href) {
         if ($item->hasAttribute('href') && $item->attribute('href') === $href) {
             return true;
         }
     });
     if ($found->isEmpty()) {
         return null;
     }
     return $found->first();
 }
Example #23
0
 public function getAllFixtures($team = null)
 {
     $response = $this->client->request('GET', self::FIXTURES_URL);
     $collection = new Collection(json_decode($response->getBody()));
     $fixtures = new Collection($collection->get('fixtures'));
     if (is_null($team)) {
         return $fixtures->where('status', 'FINISHED');
     } else {
         return $fixtures->filter(function ($value, $key) use($team) {
             return strtolower($value->homeTeamName) == strtolower($team) || strtolower($value->awayTeamName) == strtolower($team) ? $value : null;
         });
     }
 }
 /**
  * @inheritdoc
  */
 public function columnSearch()
 {
     $columns = $this->request->get('columns');
     for ($i = 0, $c = count($columns); $i < $c; $i++) {
         if ($this->request->isColumnSearchable($i)) {
             $column = $this->getColumnName($i);
             $keyword = $this->request->columnKeyword($i);
             $this->collection = $this->collection->filter(function ($row) use($column, $keyword) {
                 $data = $this->serialize($row);
                 if ($this->isCaseInsensitive()) {
                     return strpos(Str::lower($data[$column]), Str::lower($keyword)) !== false;
                 } else {
                     return strpos($data[$column], $keyword) !== false;
                 }
             });
         }
     }
 }
Example #25
0
 /**
  * @inheritdoc
  */
 public function doColumnSearch()
 {
     $columns = $this->input['columns'];
     for ($i = 0, $c = count($columns); $i < $c; $i++) {
         if ($columns[$i]['searchable'] != "true" || $columns[$i]['search']['value'] == '') {
             continue;
         }
         $column = $this->getColumnIdentity($columns, $i);
         $keyword = $columns[$i]['search']['value'];
         $this->collection = $this->collection->filter(function ($row) use($column, $keyword) {
             $data = $this->serialize($row);
             if ($this->isCaseInsensitive()) {
                 return strpos(Str::lower($data[$column]), Str::lower($keyword)) !== false;
             } else {
                 return strpos($data[$column], $keyword) !== false;
             }
         });
     }
 }
 /**
  * handle list navigation logic
  *
  * @return CommandResult
  */
 public function handle()
 {
     $registeredNavigations = new Collection($this->app['backend']->getNavigations());
     $navs = $registeredNavigations->filter(function ($nav) {
         if ($nav->isDropdown()) {
             $nav->subMenus = $nav->subMenus->filter(function ($subMenu) {
                 if (count($subMenu->getRequiredPermissions()) == 0) {
                     return true;
                 }
                 return $this->user->hasAnyPermission($subMenu->getRequiredPermissions());
             });
         }
         if (count($nav->getRequiredPermissions()) == 0) {
             return true;
         }
         return $this->user->hasAnyPermission($nav->getRequiredPermissions());
     });
     // all good
     return new CommandResult(true, "List navigation command successful.", $navs, 200);
 }
 /**
  * @param Column $column
  * @param Collection $foreignKeyColumns
  * @param Collection $foreignTables
  * @param Collection $indexes
  * @return ColumnInterface
  */
 protected function buildColumn(Column $column, Collection $foreignKeyColumns, Collection $foreignTables, Collection $indexes)
 {
     $uniqued = $indexes->filter(function (Index $index) use($column) {
         return $index->getColumns()[0] == $column->getName() && $index->isUnique();
     })->count() > 0;
     if ($column->getAutoincrement()) {
         return new ColumnAutoincrement($column, null, null, $uniqued);
     } else {
         if ($foreignKeyColumns->has($column->getName())) {
             $table = $foreignKeyColumns->get($column->getName());
             return new ColumnSelect($column, $table, $foreignTables->get($table), $uniqued);
         } else {
             if ($column->getType()->getName() == Type::INTEGER) {
                 return new ColumnNumericText($column, null, null, $uniqued);
             } else {
                 return new ColumnText($column, null, null, $uniqued);
             }
         }
     }
 }
Example #28
0
 /**
  * Revoke role.
  *
  * @param string      $role_name
  * @param object|null $resource
  *
  * @return bool
  */
 public function revokeRole($role_name, $resource = null)
 {
     // Search Roles in Cache
     $delete_roles = $this->roles->filter(function ($role) use($role_name, $resource) {
         return $this->checkRole($role, $role_name, $resource);
     });
     if (!$delete_roles->count()) {
         return true;
     }
     $model_type = get_class($this->model);
     $model_id = $this->model->getKey();
     $delete = Role::where('model_type', $model_type)->where('model_id', $model_id)->where('role_name', $role_name);
     if (is_object($resource)) {
         $resource_type = get_class($resource);
         $resource_id = $resource->getKey();
         $delete->where('resource_type', $resource_type)->where('resource_id', $resource_id);
     }
     $delete->delete();
     foreach ($delete_roles->pluck('id') as $id) {
         $this->roles->forget($id);
     }
     return $delete;
 }
 /**
  * @param Collection $entries
  *
  * @return array
  */
 public function frontpage(Collection $entries) : array
 {
     $data = ['count' => 0, 'labels' => [], 'datasets' => []];
     $left = [];
     $spent = [];
     $overspent = [];
     $filtered = $entries->filter(function ($entry) {
         return $entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0;
     });
     foreach ($filtered as $entry) {
         $data['labels'][] = $entry[0];
         $left[] = round($entry[1], 2);
         $spent[] = round(bcmul($entry[2], '-1'), 2);
         // spent is coming in negative, must be positive
         $overspent[] = round(bcmul($entry[3], '-1'), 2);
         // same
     }
     $data['datasets'][] = ['label' => trans('firefly.overspent'), 'data' => $overspent];
     $data['datasets'][] = ['label' => trans('firefly.left'), 'data' => $left];
     $data['datasets'][] = ['label' => trans('firefly.spent'), 'data' => $spent];
     $data['count'] = 3;
     return $data;
 }
Example #30
0
 /**
  * Get matching throttles after executing the condition of each throttle.
  *
  * @return \Illuminate\Support\Collection
  */
 protected function getMatchingThrottles()
 {
     return $this->throttles->filter(function ($throttle) {
         return $throttle->match($this->container);
     });
 }