public function testPopPushPutShiftUnshiftInject() { // pop $_3 = $this->collection->pop(); $this->assertEquals($this->_3, $_3); $this->assertEquals($this->_2, $this->collection->last()); $this->assertEquals(3, $this->collection->count()); // push $this->collection->push($_3); $this->assertEquals($this->_3, $this->collection->last()); // put $this->collection->put(2, 'test'); $this->assertEquals('test', $this->collection->get(2)); // shift $_0 = $this->collection->shift(); $this->assertEquals($this->_0, $_0); $this->assertEquals($this->_1, $this->collection->first()); $this->assertEquals(3, $this->collection->count()); // unshift $this->collection->unshift($_0); $this->assertEquals($this->_0, $this->collection->first()); // inject $this->collection->inject(2, 'test2'); $this->assertEquals('test2', $this->collection->get(2)); $this->assertEquals(5, $this->collection->count()); }
public function put($key, $item = NULL) { if (!$item instanceof Word) { $item = new Word($key); } parent::put($key, $item); }
/** * Dynamically retrieve attributes on the model. * * @param string $key * * @return Collection */ public function __get($key) { $newCollection = new Collection(); foreach ($this->items as $item) { if ($item instanceof Collection) { foreach ($item as $subItem) { $newCollection->put($newCollection->count(), $subItem->{$key}); } } elseif (is_object($item) && !$item instanceof Collection && $item->{$key} instanceof Collection) { foreach ($item->{$key} as $subItem) { $newCollection->put($newCollection->count(), $subItem); } } else { $newCollection->put($newCollection->count(), $item->{$key}); } } return $newCollection; }
public function encode($script) { $this->search($script); $this->words->sort(); $encoded = new Collection(); // a dictionary of base62 -> base10 $size = $this->words->size(); for ($i = 0; $i < $size; $i++) { $encoded->put(Packer::encode62($i), $i); } $index = 0; foreach ($this->words as $word) { if ($encoded->has($word)) { $word->index = $encoded->get($word); $word->clear(); } else { while ($this->words->has(Packer::encode62($index))) { $index++; } $word->index = $index++; if ($word->count == 1) { $word->clear(); } } $word->replacement = Packer::encode62($word->index); if (strlen($word->replacement) == strlen($word)) { $word->clear(); } } // sort by encoding $this->words->sort(array('Base62', 'sorter')); // trim unencoded words $this->words = $this->words->slice(0, preg_match_all('/\\|/', $this->getKeyWords(), $matches) + 1); $script = preg_replace_callback($this->getPattern(), array(&$this, '_word_replacement'), $script); /* build the packed script */ $p = $this->escape($script); $a = '[]'; $c = max($this->words->size(), 1); $k = $this->getKeyWords(); $e = $this->getEncoder(); $d = $this->getDecoder(); // the whole thing return $this->format(Base62::$UNPACK, $p, $a, $c, $k, $e, $d); }
/** * С помощью данного метода можно подгрузить загруженные файлы (Upload) к элементам коллекции по их ID, хранящемся в поле * В качестве третьего параметра можно передать название поля элемента коллекции, например связи один-ко-многим. * * Пример вызова: * $specials = DicLib::loadUploads($specials, ['upload_id']); * * @param $collection * @param string $key * @param string/null $field * @return bool */ public static function loadUploads($collection, $key = 'upload_id', $field = null) { #Helper::tad($collection); if (!is_array($key)) { $key = (array) $key; } #Helper::ta(get_class($collection)); #Helper::tad($collection instanceof Collection); #Helper::ta((int)($collection instanceof \Illuminate\Pagination\Paginator)); #dd($collection); #var_dump($collection); $single_mode = false; $paginator_mode = false; #die($collection instanceof Collection); if ($collection instanceof Collection || $collection instanceof Illuminate\Database\Eloquent\Collection) { ## all ok } elseif ($collection instanceof \Illuminate\Pagination\Paginator) { $paginator_mode = true; $paginator = clone $collection; $collection = $collection->getItems(); } else { $single_mode = true; $temp = $collection; $collection = new Collection(); $collection->put(0, $temp); } #Helper::tad('single: ' . $single_mode . ', paginator: ' . $paginator_mode . ', key: ' . print_r($key, 1)); #Helper::tad($collection); #dd($collection); if (!count($collection) || !count($key)) { return $collection; } $upload_ids = array(); /** * Перебираем все объекты в коллекции */ foreach ($collection as $obj) { /** * Если при вызове указано поле (связь) - берем ее вместо текущего объекта */ $work_obj = $field ? $obj->{$field} : $obj; #dd($work_obj->$key[0]); #dd($work_obj); /** * Перебираем все переданные ключи с ID изображений */ foreach ($key as $attr) { #Helper::ta($attr . ' - ' . is_numeric($work_obj->$attr)); if (!is_object($work_obj)) { dd($work_obj); } if (is_numeric($work_obj->{$attr})) { /** * Собираем ID изображений - в общий список и в список с разбиением по ключу */ $upload_ids_attr[$attr][] = $work_obj->{$attr}; $upload_ids[] = $work_obj->{$attr}; } } } #Helper::dd($upload_ids); #Helper::d($upload_ids_attr); $images = []; $uploads = []; if (count($upload_ids)) { $uploads = Upload::whereIn('id', $upload_ids)->get(); $uploads = self::modifyKeys($uploads, 'id'); #Helper::tad($uploads); } #dd($collection); if (count($uploads)) { /** * Перебираем все объекты в коллекции */ foreach ($collection as $o => $obj) { /** * Если при вызове указано поле (связь) - берем ее вместо текущего объекта */ $work_obj = $field ? $obj->{$field} : $obj; /** * Перебираем все переданные ключи с ID файлов */ foreach ($key as $attr) { if (is_object($work_obj) && is_numeric($work_obj->{$attr})) { if (@$uploads[$work_obj->{$attr}]) { $tmp = $work_obj->{$attr}; $upload = $uploads[$tmp]; $work_obj->setAttribute($attr, $upload); } } } if ($field) { $obj->{$field} = $work_obj; #} else { # $obj = $work_obj; } if (is_object($collection)) { $collection->put($o, $obj); } else { $collection[$o] = $obj; } } } #dd($single_mode); if ($paginator_mode) { $paginator->setItems($collection); $collection = $paginator; } else { if ($single_mode) { $collection = $collection[0]; } } #Helper::tad($collection); #dd($collection); #var_dump($collection); #Helper::ta('<hr/>'); return $collection; }
public function put($key, $item = NULL) { if (!$item instanceof RegGrpItem) { $item = new RegGrpItem($key, $item); } parent::put($key, $item); }
/** * @param mixed $key * @param mixed $item * @return void */ public function put($key, $item) { $this->assertStrictType($item); parent::put($key, $item); }