/** * Creates an ArrayCollection instance * * @param Arrayable|Traversable|array $items The items to collect * * @return ArrayCollection * * @throws DomainException When the items are not valid */ function collect($items = []) : ArrayCollection { if (!is_array($items)) { if ($items instanceof Arrayable) { $items = $items->toArray(); } elseif ($items instanceof Traversable) { $items = iterator_to_array($items); } else { $message = sprintf('Invalid items: %s', VarPrinter::toString($items)); throw new DomainException($message); } } return ArrayCollection::create($items); }
/** * Checks if a value is valid * * @param mixed $value The value * * @return bool */ protected function isValid($value) : bool { $type = gettype($value); switch ($type) { case 'string': case 'integer': case 'double': case 'boolean': case 'NULL': return true; break; case 'array': return ArrayCollection::create($value)->every(function ($v) { return $this->isValid($v); }); break; default: break; } return false; }
/** * @expectedException \Novuso\System\Exception\DomainException */ public function test_that_random_throws_exception_when_request_too_many_items() { $data = ['foo', 'bar', 'baz', 'buz']; ArrayCollection::create($data)->random(5); }
/** * {@inheritdoc} */ public function toString() : string { return sprintf('{%s}', Collection::create()->push(sprintf('id:%s', $this->id->toString()))->push(sprintf('type:%s', $this->type->value()))->push(sprintf('timestamp:%s', $this->timestamp->toString()))->push(sprintf('meta_data:{%s}', Collection::create($this->metaData->toArray())->implode(',', function ($value, $key) { return sprintf('%s:%s', $key, VarPrinter::toString($value)); })))->push(sprintf('payload_type:%s', $this->payloadType->toString()))->push(sprintf('payload:{%s}', Collection::create($this->payload->toArray())->implode(',', function ($value, $key) { return sprintf('%s:%s', $key, VarPrinter::toString($value)); })))->implode(',')); }