/** * Scrape URL and collect output * @param string $url * @param OutputInterface $output */ public function scrape($url, OutputInterface $output) { $this->collection->exchangeArray([]); try { $initialPage = $this->client->get($url)->send(); } catch (BadResponseException $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); throw new \RuntimeException('Unable to load initial page'); } $xml = $this->getSimpleXml($initialPage->getBody()); $items = $xml->xpath($this->config['collection']); foreach ($items as $item) { $title = $item->xpath($this->config['title']); $unitPrice = $item->xpath($this->config['unitPrice']); $itemUrl = $item->xpath($this->config['itemUrl']); $itemSize = 0; $itemDesc = null; if (isset($itemUrl[0]->attributes()['href'])) { try { $itemPage = $this->client->get((string) $itemUrl[0]->attributes()['href'])->send(); $itemPageBody = $this->getSimpleXml($itemPage->getBody()); $itemSize = $itemPage->getContentLength(); $itemDesc = $itemPageBody->xpath($this->config['desc']); } catch (BadResponseException $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } } if ($title && $unitPrice) { $parsedPrice = (double) \trim(\str_replace('£', null, $unitPrice[0])); $this->collection->append(['title' => \trim($title[0]), 'unit_price' => $parsedPrice, 'description' => \trim($itemDesc[0]), 'size' => \round($itemSize / 1024) . 'kb']); } } return $this->collection; }
public static function buildCollection($filters, $order = null) { // filters must be field=>value $collection = new Collection(); $sql = $params = array(); foreach ($filters as $key => $filter) { if (!is_array($filter)) { $params[":{$key}"] = $filter; $sql[] = "`{$key}` = :{$key}"; } else { $params[":{$key}"] = $filter['value']; $sql[] = "`{$key}` {$filter['operator']} :{$key}"; } } if ($sql) { $sql = "WHERE " . implode(' AND ', $sql); } else { $sql = ''; } $sql = "SELECT * FROM `" . static::TABLE_NAME . "` {$sql}"; $stmt = static::getConnection()->prepAndExecute(new \ArtfulRobot\PDO_Query("Fetch records from " . static::TABLE_NAME, $sql, $params)); if ($stmt->errorCode() != '00000') { throw new Exception("PDO error: " . print_r($stmt->errorInfo(), 1)); } while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { // create an object of the class $obj = new static(); $obj->loadFromArray($row, false, self::CAST_DB); // these lines differ from main code $id = implode("\\A", $obj->getPK()); $collection->append($obj, $id); unset($obj); } return $collection; }
/** * Appends the specified element to the end of this collection. * @param mixed * @return void * @throws \Nette\InvalidArgumentException */ public function append($item) { // collection was not loaded before, now it is only manualy set collection if (!$this->isLoaded()) { $this->loadable = false; } return parent::append($item); }
public static function setUpBeforeClass() { static::$collection = new Collection(); static::$collection->append('part1', ['item1' => 'stuff']); static::$collection->freeze('*'); static::assertTrue(static::$collection->immutable('part1')); static::$collection->thaw('*'); static::assertFalse(static::$collection->immutable('part1')); }
/** * Converts this iterator into an instance of Morph_Collection * * Note that this means all objects will be held in memory so * you need to be a bit careful not to exceed memory limits * * @return Morph_Collection */ public function toCollection() { $collection = new Collection(); $collection->setPermissableType(\get_class($this->type)); $collection->setTotalCount($this->totalCount()); $this->rewind(); foreach ($this as $object) { $collection->append($object); } return $collection; }
/** * @param ResourceInterface $resource * @return $this */ public function add(ResourceInterface $resource) { $filename = $resource->getFilename(); if (in_array($filename, $this->resources)) { // skip duplicates return $this; } $this->resources[] = $filename; $resource->setAssetsRevision($this->assetsRevision); $this->collection->append($resource); return $this; }
/** * build a Collection object with objects of this class based on filters * * @param Array $filters * @param string|null $order literal SQL appended to "ORDER BY " if used. * @returns Collection object */ public static function buildCollection($filters, $order = null) { Debug::log(">>" . get_called_class() . ":: " . __FUNCTION__ . " called with filters:", $filters); $collection = new Collection(); $sql = static::buildCollectionSql($params, $filters, $order); $stmt = static::getConnection()->prepAndExecute(new \ArtfulRobot\PDO_Query("Fetch records from " . static::TABLE_NAME, $sql, $params)); if ($stmt->errorCode() != '00000') { throw new Exception("PDO error: " . print_r($stmt->errorInfo(), 1)); } while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { // create an object of the class $obj = new static(); $obj->loadFromArray($row, false, self::CAST_DB); $collection->append($obj, $obj->id); unset($obj); } Debug::log("<< Returning collection with " . $collection->count() . " entries"); return $collection; }
/** * Return a collection of subfolders * * @param array $ignore * @param boolean $plain * @return Collection */ public function children($ignore = null, $plain = false) { $raw = $this->scan($ignore); if ($plain) { $content = array(); foreach ($raw as $file) { if (is_dir($this->root . DS . $file)) { $content[] = $file; } } } else { $content = new Collection(); foreach ($raw as $file) { if (is_dir($this->root . DS . $file)) { $content->append($file, new static($this->root . DS . $file)); } } } return $content; }
public function append($value) { $this->collection->append($value); parent::append($value); $this->refreshPositions(); }
public function files() { $files = new Collection(); foreach ($this->data as $page) { foreach ($page->files() as $file) { $files->append($page->id() . '/' . strtolower($file->filename()), $file); } } return $files; }