/** * @param array|\Traversable|ElementInterface $elementOrFieldset * @param array $flags * @return void|\Zend\Form\Fieldset|\Zend\Form\FieldsetInterface|\Zend\Form\FormInterface */ public function add($elementOrFieldset, array $flags = array()) { if ($elementOrFieldset instanceof ElementInterface) { $elementOrFieldset->setForm($this); } parent::add($elementOrFieldset, $flags); }
/** * @param Node|string|array|\Traversable|array $symbol * @return Node|string|array|\Traversable|array */ public function push($symbol) { if ($symbol === false || $symbol === null || $symbol === '') { return $symbol; } if ($symbol instanceof Buffer) { $this->push($symbol->toValue()); } elseif ($symbol instanceof Node) { $this->parts[] = $symbol; $this->length++; } elseif (is_string($symbol)) { $len = count($this->parts); if ($len > 0 && is_string($this->parts[$len - 1])) { $this->parts[$len - 1] .= $symbol; } else { $this->parts[] = $symbol; } $this->length += strlen($symbol); $this->strLength += strlen($symbol); } elseif ($symbol instanceof \Traversable || is_array($symbol)) { $ret = []; foreach ($symbol as $part) { $ret[] = $this->push($part); } return $ret; } else { throw new \InvalidArgumentException(sprintf('Part type unsupported: %s', get_class($symbol))); } return $symbol; }
/** * constructor * * @param \Traversable|array $elements * @param string $sourceType optional */ private function __construct($elements, string $sourceType = null) { $this->elements = $elements; if ($elements instanceof SelfDescribing) { $this->type = $sourceType . ' ' . $elements->description(); } elseif (is_array($elements)) { $this->type = 'of array'; } else { $this->type = 'from ' . get_class($elements); } }
/** * @param \Traversable $iterator * * @return IGenerator */ public static function adapter(\Traversable $iterator) { if ($iterator instanceof IGenerator) { return $iterator; } elseif ($iterator instanceof IIterator) { return new IIteratorAdapter($iterator); } elseif ($iterator instanceof \ArrayIterator) { return new ArrayIteratorAdapter($iterator); } elseif ($iterator instanceof \IteratorAggregate) { return static::adapter($iterator->getIterator()); } else { return new IteratorAdapter($iterator); } }
/** * Decorates a collection of entities as Nodes * * @param Traversable|array $input * @return Traversable|array */ public function getNodes($input) { if ($input instanceof PersistentCollection) { // Return instance of ArrayCollection instead of PersistentCollection $hm = $this; return $input->unwrap()->map(function ($node) use($hm) { return $hm->getNode($node); }); } elseif (is_array($input) || $input instanceof Traversable) { foreach ($input as $key => $entity) { $input[$key] = $this->getNode($entity); } return $input; } throw new \InvalidArgumentException('Input to getNodes should be a PersistentCollection or a ' . 'Traversable/array, ' . gettype($input) . ' provided.'); }
/** * @param array|\Traversable $xs * @return \Iterator * @throws \InvalidArgumentException */ public static function iterator($xs) { if ($xs instanceof \Iterator) { return $xs; } else { if ($xs instanceof \IteratorAggregate) { return $xs->getIterator(); } else { if (is_array($xs)) { return new \ArrayIterator($xs); } else { $t = gettype($xs); throw new \InvalidArgumentException("'{$t}' object is not iterable"); } } } }
/** * @param \Traversable $x * @param int $chunkSize */ public function __construct($x, $chunkSize) { if ($x instanceof \Iterator) { $this->it = $x; } else { if ($x instanceof \IteratorAggregate) { $this->it = $x->getIterator(); } else { if (is_array($x)) { $this->it = new \ArrayIterator($x); } else { $t = gettype($x); throw new \InvalidArgumentException(sprintf('%s object is not iterable', $t)); } } } if ($chunkSize < 1) { throw new \InvalidArgumentException('chunkSize must be greater than 0'); } $this->chunkSize = $chunkSize; }
private function sort() { if (!$this->iteratorSorted && $this->isSorted()) { if (!$this->iterator instanceof \ArrayIterator) { $this->iterator = new \ArrayIterator(iterator_to_array($this->iterator)); } $this->iterator->uasort(function ($a, $b) { return $this->sortCriteria()->compare($a, $b); }); $this->iteratorSorted = true; } }
/** * @param \Traversable $traversable * @return array|\Traversable */ public static function copyTransversable($traversable) { // Allow for generators to be used once. Better than always throwing an error. // Best to assume the user knows how to use Generators correctly, // and might even be sending an empty iterator if ($traversable instanceof \Generator) { // We should NOT check the generator before it enters a foreach // As with any PHP built in, observing it changes the outcome. // Touching any method on an empty generator will cause a foreach to // Throw rather than silently not iterating. return $traversable; } return $traversable instanceof \IteratorAggregate ? $traversable->getIterator() : clone $traversable; }
/** * Check if current position is valid. * * @return bool */ public function valid() { $valid = $this->_iterator->valid(); if (true === $valid) { return true; } if (null !== $this->_body) { $handle =& $this->_body; $handle($this->_i); } $this->rewind(); if ($this->_n <= $this->_i++) { $this->_i = 1; return false; } return true; }
public static function findMinMax(\Traversable $it, callback $extractor = null) { if ($extractor === null) { $extractor = function ($value) { return $value; }; } $min = $max = $extractor($it->current()); $it->next(); $value = $extractor($it->current()); while ($it->valid()) { if ($value > $max) { $max = $value; } elseif ($value < $min) { $min = $value; } $it->next(); $value = $it->current(); } return array($min, $max); }
/** * Upload a meeting document. * * @param array|Traversable $post * @param array|Traversable $files * * @return boolean If uploading was a success */ public function uploadDocument($post, $files) { $form = $this->getDocumentForm(); $data = array_merge_recursive($post->toArray(), $files->toArray()); $form->setData($data); if (!$form->isValid()) { return false; } $data = $form->getData(); $path = $this->getFileStorageService()->storeUploadedFile($data['upload']); $meeting = explode('/', $data['meeting']); $meeting = $this->getMeeting($meeting[0], $meeting[1]); $document = new MeetingDocument(); $document->setPath($path); $document->setName($data['name']); $document->setMeeting($meeting); $this->getMeetingMapper()->persistDocument($document); return true; }
/** * Utility function for push without variable args. * Push one Property instance or array|\Traversable of such instances. * @return void * @param Property|array|\Traversable $item * @throws \UnexpectedValueException If any individual value is not a * Property. */ private function pushOneArg($item) { if ($item instanceof Property) { if ('uid' === $item->getName()) { $this->setUID($item->getValue()); } elseif ($item->getSpecification()->requiresSingleProperty()) { $this->data[$item->getName()] = $item; } else { $this->data[$item->getName()][] = $item; } } elseif (is_array($item) || $item instanceof \Traversable) { foreach ($item as $property) { $this->pushOneArg($property); } } else { throw new \UnexpectedValueException('Not a property'); } }
public function rewind() { $this->input->rewind(); }
/** * Check if current position is valid. * * @return bool */ public function valid() { return $this->_iterator->valid(); }
/** * Increments the iterator to the next value * * @return NULL */ function next() { if (isset($this->offset)) { $this->inner->next(); } }
/** * @return bool */ public final function isArrayCompatible() { return $this->source instanceof IIterator ? $this->source->isArrayCompatible() : false; }
/** * Just utils to convert any iterable to iterator object, used when slicing. * * @param \Traversable|array $iterable */ private function toIterator($iterable) { if ($iterable instanceof Iterator) { return $iterable; } if ($iterable instanceof IteratorAggregate) { return $iterable->getIterator(); } if (is_array($iterable)) { return new \ArrayIterator($iterable); } throw new \InvalidArgumentException('Argument must be iterable'); }
/** * Set the list iterator to that of the current container shard */ protected function setIteratorFromCurrentShard() { $this->iter = $this->listFromShard($this->container . $this->shardSuffixes[$this->curShard], $this->directory, $this->params); // Start loading results so that current() works if ($this->iter) { $this->iter instanceof Iterator ? $this->iter->rewind() : reset($this->iter); } }
/** * Upload a meeting document. * * @param array|Traversable $post * @param array|Traversable $files * * @return boolean If uploading was a success */ public function uploadDocument($post, $files) { $form = $this->getDocumentForm(); $data = array_merge_recursive($post->toArray(), $files->toArray()); $form->setData($data); if (!$form->isValid()) { return false; } $data = $form->getData(); $config = $this->getServiceManager()->get('config'); $config = $config['meeting-documents']; $filename = $data['meeting'] . '/' . $data['upload']['name']; $path = $config['upload_dir'] . '/' . $filename; if (file_exists($path)) { $form->setError(Notes::ERROR_FILE_EXISTS); return false; } $meeting = explode('/', $data['meeting']); $meeting = $this->getMeeting($meeting[0], $meeting[1]); $document = new MeetingDocument(); $document->setPath($data['upload']['name']); $document->setName($data['name']); $document->setMeeting($meeting); // finish upload and save in the database if (!is_dir(dirname($path))) { mkdir(dirname($path), $config['dir_mode'], true); } move_uploaded_file($data['upload']['tmp_name'], $path); $this->getMeetingMapper()->persistDocument($document); return true; }
/** * Example of applying a callback to capitalize the first letter. * * Note that you must return TRUE from the callback function to continue * iterating. This can be useful if you wish to stop iteration under * certain conditions. * * @param Iterator $it * @return bool */ function addDbPrefix(Traversable $it, $prefix = 'test') { echo $it[$it->key()] = $prefix . '_' . $it->current(); echo PHP_EOL; return true; }
/** * Implentation of the Iterator SPL class for Valid(), * Checks if the current item is a valid item for processing * NULL keys represent an invalid item * * @access public */ public function valid() { return $this->datasource->valid(); }