public static function create(callable $handler = null) : self { $stack = new self($handler ?: choose_handler()); $stack->push(Middleware::httpErrors()); $stack->push(Middleware::prepareBody()); return $stack; }
/** * Creates a default handler stack that can be used by clients. * * The returned handler will wrap the provided handler or use the most * appropriate default handler for you system. The returned HandlerStack has * support for cookies, redirects, HTTP error exceptions, and preparing a body * before sending. * * The returned handler stack can be passed to a client in the "handler" * option. * * @param callable $handler HTTP handler function to use with the stack. If no * handler is provided, the best handler for your * system will be utilized. * * @return HandlerStack */ public static function create(callable $handler = null) { $stack = new self($handler ?: choose_handler()); $stack->push(Middleware::httpErrors(), 'http_errors'); $stack->push(Middleware::redirect(), 'allow_redirects'); $stack->push(Middleware::cookies(), 'cookies'); $stack->push(Middleware::prepareBody(), 'prepare_body'); return $stack; }
static function range(date $first, date $last) { $week = new self(); $day = $first; $week->push($day); do { $day = $day->tomorrow(); $week->push($day); } while (!$day->is_equal($last)); return $week; }
/** * Only used for tests. * @param string[] $displayNames * @return array|CurrentPath */ public static function fromArray(array $displayNames) { $path = new self(); foreach ($displayNames as $v) { $path->push(new ResourceField(new ResourceDefinition(null), $v)); } return $path; }
/** * @param Traversable $from * * @return Collection */ public static function from(Traversable $from) { $self = new self(); foreach ($from as $item) { $self->push($item); } return $self; }
public function reduce(callable $callback, $preserveKeys = false) { $collection = new self(); foreach ($this->collection as $key => $item) { if ($callback($item)) { $collection->push($item, $preserveKeys ? $key : null); } } return $collection; }
public function whereRecip($email) { $coll = new self(); foreach ($this as $msg) { if ($msg->recipients->contains('email', $email)) { $coll->push($msg); } } return $coll; }
/** * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence * @return $this */ public function sequence(array $sequence) { $new = new self($this->math, $this->opcodes, null); foreach ($sequence as $operation) { if (is_int($operation)) { if (!$this->opcodes->offsetExists($operation)) { throw new \RuntimeException('Unknown opcode'); } $new->script .= chr($operation); } elseif ($operation instanceof Number) { $new->push($operation->getBuffer()); } elseif ($operation instanceof BufferInterface) { $new->push($operation); } else { throw new \RuntimeException('Input was neither an opcode or BufferInterfacecc'); } } $this->concat($new->getScript()); return $this; }
private function copyFromContext(LinkedNode $context) { $list = new self(); for ($n = $context; $n !== $this->tail; $n = $n->next()) { /** * @var LinkedDataNode $n */ $list->push($n->value()); } return $list; }
/** * Get all the unique values in the collection. * * @param bool $preserveKeys * * @return CollectionInterface */ public function unique($preserveKeys = true) { $values = new self(); $this->each(function ($item) use($values, $preserveKeys) { if (!$values->contains($item)) { if ($preserveKeys) { } $values->push($item); } }); return $values; }
public function diff(Delta $other) { $delta = new self(); if ($this->getOperations() === $other->getOperations()) { return $delta; } $strings = array_map(function (OperationCollection $operations) use($other) { return implode(array_map(function (OperationInterface $operation) use($operations, $other) { if ($operation->getType() === OperationInterface::TYPE_INSERT) { return is_string($operation->getValue()) ? $operation->getValue() : chr(0); } throw new \RuntimeException(sprintf('diff() called %s non-document', $operations === $other->getOperations() ? 'on' : 'with')); }, $operations->toArray())); }, [$this->getOperations(), $other->getOperations()]); $diffResults = FastDiff::diff($strings[0], $strings[1]); $thisIterator = $this->getOperations()->getIterator(); $otherIterator = $other->getOperations()->getIterator(); foreach ($diffResults as $diffResult) { $length = mb_strlen($diffResult[1]); while ($length > 0) { $operationLength = 0; switch ($diffResult[0]) { case FastDiff::DIFF_INSERT: $operationLength = min($otherIterator->getPeekLength(), $length); $delta->push($otherIterator->next($operationLength)); break; case FastDiff::DIFF_DELETE: $operationLength = min($length, $thisIterator->getPeekLength()); $thisIterator->next($operationLength); $delta->delete($operationLength); break; case FastDiff::DIFF_EQUAL: $operationLength = min($thisIterator->getPeekLength(), $otherIterator->getPeekLength(), $length); $thisOperation = $thisIterator->next($operationLength); $otherOperation = $otherIterator->next($operationLength); if ($thisOperation->getValue() === $otherOperation->getValue()) { $delta->retain($operationLength, $this->diffAttributes($thisOperation->getAttributes(), $otherOperation->getAttributes())); } else { $delta->push($otherOperation)->delete($operationLength); } break; } $length -= $operationLength; } } return $delta->chop(); }
static function fromSlice(array $tokens) : self { $ts = new self(); $ts->first = new NodeStart(); $ts->last = new NodeEnd(); self::link($ts->first, $ts->last); foreach ($tokens as $token) { $ts->push($token); } $ts->reset(); return $ts; }
public function map($callback) { if (!is_callable($callback)) { throw new \InvalidArgumentException('Invalid (not callable) callback given'); } $result = new self(); for ($i = 0; $i < $this->count(); $i++) { $value = $callback($this->storage[$i]); if (null !== $value) { $result->push($value); } } return $result; }
public function filter($var, $value) { $result = new self(); foreach ($this as $e) { if ($e->{$var} == $value) { $result->push($e); } } return $result; }