/** * Fetch a string representation of the collection. * * The string may not describe all elements of the collection, but should at least * provide information on the type and state of the collection. * * @return string A string representation of the collection. */ public function __toString() { if ($this->isEmpty()) { return '<PriorityQueue 0>'; } return sprintf('<PriorityQueue %d [next: %s]>', $this->size(), Repr::repr($this->next())); }
/** * Adapt a value into a coroutine. * * @param StrandInterface $strand The currently executing strand. * @param mixed $value The value to adapt. * * @return CoroutineInterface * @throws InvalidArgumentException if now valid adaptation can be made. */ public function adapt(StrandInterface $strand, $value) { while ($value instanceof CoroutineProviderInterface) { $value = $value->coroutine($strand); } if ($value instanceof CoroutineInterface) { return $value; } elseif ($value instanceof Generator) { return new GeneratorCoroutine($value); } elseif ($value instanceof PromiseInterface) { return new PromiseCoroutine($value); } elseif (is_array($value)) { return Recoil::all($value); } elseif (null === $value) { return Recoil::cooperate(); } throw new InvalidArgumentException('Unable to adapt ' . Repr::repr($value) . ' into a coroutine.'); }
/** * Serialize a table or array field. * * @param mixed $value * * @return string The serialized value. */ private function serializeField($value) { if (is_string($value)) { // @todo Could be decimal (D) or byte array (x) // @see https://github.com/recoilphp/amqp/issues/25 return 'S' . $this->serializeLongString($value); } elseif (is_integer($value)) { // @todo Could be timestamp (T) // @see https://github.com/recoilphp/amqp/issues/25 if ($value >= 0) { if ($value < 0x80) { return 'b' . $this->serializeSignedInt8($value); } elseif ($value < 0x8000) { return 's' . $this->serializeSignedInt16($value); } elseif ($value < 2147483648.0) { return 'I' . $this->serializeSignedInt32($value); } } else { if ($value >= -0x80) { return 'b' . $this->serializeSignedInt8($value); } elseif ($value >= -0x8000) { return 's' . $this->serializeSignedInt16($value); } elseif ($value >= -2147483648.0) { return 'I' . $this->serializeSignedInt32($value); } } return 'l' . $this->serializeSignedInt64($value); } elseif (true === $value) { return "t"; } elseif (false === $value) { return "t"; } elseif (null === $value) { return 'V'; } elseif (is_double($value)) { return 'd' . $this->serializeDouble($value); } elseif (is_array($value)) { return $this->serializeArrayOrTable($value); } else { throw new InvalidArgumentException('Could not serialize value (' . Repr::repr($value) . ').'); } }
/** * @param mixed $reason The reason that the promise was rejected. */ public function __construct($reason) { $this->reason = $reason; parent::__construct('Promise was rejected: ' . Repr::repr($reason) . '.'); }
/** * Get an iterator for any traversable type. * * @param mixed<mixed> $collection * * @return Iterator * @throws InvalidArgumentException if no iterator can be produced from the given collection. */ public static function getIterator($collection) { if (is_array($collection)) { $collection = new ArrayIterator($collection); } else { while ($collection instanceof IteratorAggregate) { $collection = $collection->getIterator(); } } if (!$collection instanceof Iterator) { throw new InvalidArgumentException('Could not produce an iterator for ' . Repr::repr($collection) . '.'); } return $collection; }
/** * @param mixed $key The unknown key. * @param Exception|null $previous The previous exception, if any. */ public function __construct($key, Exception $previous = null) { parent::__construct('Key ' . Repr::repr($key) . ' already exists.', 0, $previous); }
/** * Fetch a string representation of the collection. * * The string may not describe all elements of the collection, but should at least * provide information on the type and state of the collection. * * @return string A string representation of the collection. */ public function __toString() { if ($this->isEmpty()) { return '<Map 0>'; } elseif ($this->size() > 3) { $format = '<Map %d [%s, ...]>'; } else { $format = '<Map %d [%s]>'; } $elements = array(); foreach ($this->elements->slice(0, 3) as $element) { list($key, $value) = $element; $elements[] = Repr::repr($key) . ' => ' . Repr::repr($value); } return sprintf($format, $this->size(), implode(', ', $elements)); }