filtering results

@param   string|closure  $key
@param   mixed           $value
@return  Samurai\Onikiri\Entities
174423
public function filter($key, $value = null)
    {
        if (! $key instanceof \Closure) {
            $closure = function($entity) use ($key, $value) {
                return $entity->$key == $value;
            };
        } else {
            $closure = $key;
        }
        
        $filtered = new Entities();
        foreach ($this->_entities as $entity) {
            if ($closure($entity)) {
                $filtered->add($entity);
            }
        }
        return $filtered;
    }
911621
public function filter(callable $fn, $key = false)
    {
        $this->prepare();

        $data = [];
        foreach ($this->_map as $k => $val) {
            if ($fn($val)) {
                if ($key) {
                    $data[$k] = $val;
                } else {
                    $data[] = $val;
                }
            }
        }

        return $this->prepareResult($data, false);
    }
363704
public function filter(Closure $p)
    {
        $this->guardAgainstInvalidGivenType();
        $elements = $this->collection->filter($p)->toArray();

        return $this->create($elements);
    }
1043539
function filter(callable $closure) {
        $args = $this->baseStorageArgs;
        $args[] = parent::filter($closure)->all();
        
        return (new static(...$args));
    }
383897
public function filter(Closure $callback)
    {
        $filtered_map = new static;

        foreach ($this->items as $key => $item) {
            if ($callback($item) === true) {
                $filtered_map->setItem($key, $item);
            }
        }

        return $filtered_map;
    }
82401
public function where($key, $value, $strict = true)
    {
        return $this->filter(function ($item) use ($key, $value, $strict) {
            return $strict ? ArrayHelper::getValue($item, $key) === $value
                : ArrayHelper::getValue($item, $key) == $value;
        });
    }
1084716
public function removeFilterByValue($value)
    {
        return $this->removeFilterByFunction(
            function($key, $query) use ($value) {
                $query = $query->getQuery();
                return $query == $value;
            }
        );
    }
1268270
public function where($key, $operator = null, $value = null)
    {
        return $this->filter($this->operatorForWhere(...func_get_args()));
    }
645229
public function getAll($key, $value = null)
    {
        $collection = new self();

        foreach ($this->items as $item) {
            if ($item->{$key} == $value) {
                $collection->push($item);
            }
        }

        return $collection;
    }
276456
public function query($key, $value = null)
    {
        if ($value) {
            return $this->replicate(['query' => [$key => $value]]);
        }

        return $this->replicate(['query' => $key]);
    }
1022838
public function pluck($value, $key = null)
    {
        $this->newQuery();

        $lists = $this->query->pluck($value, $key);

        if (is_array($lists)) {
            return $lists;
        }

        return $lists->all();
    }
491069
protected function filterRelation($key, $value)
    {
        $key = str_replace('~', '.', $key);
        $parts = explode('.', $key);

        $realKey = array_pop($parts);
        $relation = implode('.', $parts);

        if (! in_array($relation, array_keys($this->with))) {
            return;
        }

        extract($this->formatParam($realKey, $value));

        if (! $this->canFilter($column)) {
            return;
        }

        $this->builder->whereHas($relation, function ($q) use ($column, $comparator, $value) {
            if ($comparator == 'IN') {
                $values = explode(',', $value);
                $q->whereIn($column, $values);
            } elseif ($comparator == 'NOT IN') {
                $values = explode(',', $value);
                $q->whereNotIn($column, $values);
            } else {
                $values = explode('|', $value);
                if (count($values) > 1) {
                    $q->where(function ($query) use ($column, $comparator, $values) {
                        foreach ($values as $value) {
                            if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                                $value = preg_replace('/(^\*|\*$)/', '%', $value);
                            }
                            // Link the filters with AND of there is a "not" and with OR if there's none
                            if ($comparator == '!=' || $comparator == 'NOT LIKE') {
                                $query->where($column, $comparator, $value);
                            } else {
                                $query->orWhere($column, $comparator, $value);
                            }
                        }
                    });
                } else {
                    $value = $values[0];
                    if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                        $value = preg_replace('/(^\*|\*$)/', '%', $value);
                    }
                    if ($comparator == 'NULL' || $comparator == 'NOT NULL') {
                        $q->whereNull($column, 'and', $comparator == 'NOT NULL');
                    } else {
                        $q->where($column, $comparator, $value);
                    }
                }
            }
        });
    }
861791
protected function doFilter($value, Closure $callback)
    {
        return true === $this->multiByteEnabled ?
            mb_convert_case($value, $this->mode, $this->encoding) : $callback($value);
    }
46017
public function close($value)
	{
		if (!($value instanceof Closure))
		{
			$value = function($uri) use ($value)
			{
				if (is_callable($value))
				{
					return call_user_func_array($value, array($uri));
				}
				return $value;
			};
		}

		return $value;
	}
867253
private static function flattenArgument (&$value, $key) {
        if ($value instanceof Closure) {
            $closureReflection = new ReflectionFunction($value);
            $value = sprintf('(Closure at %s:%s)', $closureReflection->getFileName(), $closureReflection->getStartLine());
        } elseif (is_object($value)) {
            $value = sprintf('object(%s)', get_class($value));
        } elseif (is_resource($value)) {
            $value = sprintf('resource(%s)', get_resource_type($value));
        }
    }
1133153
protected function filter($key, $value)
    {
        extract($this->formatParam($key, $value));

        if (! $this->canFilter($column)) {
            return;
        }

        if ($comparator == 'IN') {
            $values = explode(',', $value);
            $this->getHandler()->whereIn($column, $values);
        } elseif ($comparator == 'NOT IN') {
            $values = explode(',', $value);
            $this->getHandler()->whereNotIn($column, $values);
        } else {
            $values = explode('|', $value);
            if (count($values) > 1) {
                $this->getHandler()->where(function ($query) use ($column, $comparator, $values) {
                    foreach ($values as $value) {
                        if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                            $value = preg_replace('/(^\*|\*$)/', '%', $value);
                        }
                        // Link the filters with AND of there is a "not" and with OR if there's none
                        if ($comparator == '!=' || $comparator == 'NOT LIKE') {
                            $query->where($column, $comparator, $value);
                        } else {
                            $query->orWhere($column, $comparator, $value);
                        }
                    }
                });
            } else {
                $value = $values[0];
                if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                    $value = preg_replace('/(^\*|\*$)/', '%', $value);
                }
                if ($comparator == 'NULL' || $comparator == 'NOT NULL') {
                    $this->getHandler()->whereNull($column, 'and', $comparator == 'NOT NULL');
                } else {
                    $this->getHandler()->where($column, $comparator, $value);
                }
            }
        }
    }
1278793
public function where($key, $value, $strict = true)
    {
        return $this->filter(function ($item) use ($key, $value, $strict)
        {
            return ($strict ? (data_get($item, $key) === $value) : (data_get($item, $key) == $value));
        });
    }