Set or get the returnMetadata flag
public returnMetadata ( boolean $returnMetadata = null ) : boolean | self | ||
$returnMetadata | boolean | Give this a value to set the returnMetadata flag |
리턴 | boolean | self |
/** * {@inheritdoc} */ public function getImages($publicKey, Query $query, Images $model) { $images = array(); $qb = $this->getConnection()->createQueryBuilder(); $qb->select('*')->from($this->tableNames['imageinfo'], 'i')->where('i.publicKey = :publicKey')->setParameter(':publicKey', $publicKey); if ($sort = $query->sort()) { // Fields valid for sorting $validFields = array('size' => true, 'publicKey' => true, 'imageIdentifier' => true, 'extension' => true, 'mime' => true, 'added' => true, 'updated' => true, 'width' => true, 'height' => true, 'checksum' => true, 'originalChecksum' => true); foreach ($sort as $f) { if (!isset($validFields[$f['field']])) { throw new InvalidArgumentException('Invalid sort field: ' . $f['field'], 400); } $qb->addOrderBy($f['field'], $f['sort']); } } else { $qb->orderBy('added', 'DESC'); } $from = $query->from(); $to = $query->to(); if ($from || $to) { if ($from !== null) { $qb->andWhere('added >= :from')->setParameter(':from', $from); } if ($to !== null) { $qb->andWhere('added <= :to')->setParameter(':to', $to); } } if ($imageIdentifiers = $query->imageIdentifiers()) { $expr = $qb->expr(); $composite = $expr->orX(); foreach ($imageIdentifiers as $i => $id) { $composite->add($expr->eq('i.imageIdentifier', ':imageIdentifier' . $i)); $qb->setParameter(':imageIdentifier' . $i, $id); } $qb->andWhere($composite); } if ($checksums = $query->checksums()) { $expr = $qb->expr(); $composite = $expr->orX(); foreach ($checksums as $i => $id) { $composite->add($expr->eq('i.checksum', ':checksum' . $i)); $qb->setParameter(':checksum' . $i, $id); } $qb->andWhere($composite); } if ($originalChecksums = $query->originalChecksums()) { $expr = $qb->expr(); $composite = $expr->orX(); foreach ($originalChecksums as $i => $id) { $composite->add($expr->eq('i.originalChecksum', ':originalChecksum' . $i)); $qb->setParameter(':originalChecksum' . $i, $id); } $qb->andWhere($composite); } // Create a querybuilder that will be used to fetch the hits number, and update the model $hitsQb = clone $qb; $hitsQb->select('COUNT(i.id)'); $stmt = $hitsQb->execute(); $model->setHits((int) $stmt->fetchColumn()); if ($limit = $query->limit()) { $qb->setMaxResults($limit); } if ($page = $query->page()) { $offset = (int) $query->limit() * ($page - 1); $qb->setFirstResult($offset); } $stmt = $qb->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $returnMetadata = $query->returnMetadata(); foreach ($rows as $row) { $image = array('extension' => $row['extension'], 'added' => new DateTime('@' . $row['added'], new DateTimeZone('UTC')), 'updated' => new DateTime('@' . $row['updated'], new DateTimeZone('UTC')), 'checksum' => $row['checksum'], 'originalChecksum' => isset($row['originalChecksum']) ? $row['originalChecksum'] : null, 'publicKey' => $row['publicKey'], 'imageIdentifier' => $row['imageIdentifier'], 'mime' => $row['mime'], 'size' => (int) $row['size'], 'width' => (int) $row['width'], 'height' => (int) $row['height']); if ($returnMetadata) { $image['metadata'] = $this->getMetadata($publicKey, $row['imageIdentifier']); } $images[] = $image; } return $images; }
public function testGetImagesAndReturnMetadata() { $this->insertImages(); $query = new Query(); $query->returnMetadata(true); $images = $this->adapter->getImages('publickey', $query, $this->getMock('Imbo\\Model\\Images')); foreach ($images as $image) { $this->assertArrayHasKey('metadata', $image); } $this->assertSame(array('key5' => 'value5'), $images[0]['metadata']); $this->assertSame(array('key4' => 'value4'), $images[1]['metadata']); $this->assertSame(array('key3' => 'value3'), $images[2]['metadata']); $this->assertSame(array('key2' => 'value2'), $images[3]['metadata']); $this->assertSame(array('key1' => 'value1'), $images[4]['metadata']); $this->assertSame(array('key0' => 'value0'), $images[5]['metadata']); }
/** * @covers Imbo\Resource\Images\Query::returnMetadata */ public function testReturnMetadata() { $this->assertFalse($this->query->returnMetadata()); $this->assertSame($this->query, $this->query->returnMetadata(true)); $this->assertTrue($this->query->returnMetadata()); }
/** * {@inheritdoc} */ public function getImages($publicKey, Query $query, Images $model) { // Initialize return value $images = []; // Query data $queryData = ['publicKey' => $publicKey]; $from = $query->from(); $to = $query->to(); if ($from || $to) { $tmp = []; if ($from !== null) { $tmp['$gte'] = $from; } if ($to !== null) { $tmp['$lte'] = $to; } $queryData['added'] = $tmp; } $imageIdentifiers = $query->imageIdentifiers(); if (!empty($imageIdentifiers)) { $queryData['imageIdentifier']['$in'] = $imageIdentifiers; } $checksums = $query->checksums(); if (!empty($checksums)) { $queryData['checksum']['$in'] = $checksums; } $originalChecksums = $query->originalChecksums(); if (!empty($originalChecksums)) { $queryData['originalChecksum']['$in'] = $originalChecksums; } // Sorting $sort = ['added' => -1]; if ($querySort = $query->sort()) { $sort = []; foreach ($querySort as $s) { $sort[$s['field']] = $s['sort'] === 'asc' ? 1 : -1; } } // Fields to fetch $fields = array_fill_keys(['extension', 'added', 'checksum', 'originalChecksum', 'updated', 'publicKey', 'imageIdentifier', 'mime', 'size', 'width', 'height'], true); if ($query->returnMetadata()) { $fields['metadata'] = true; } try { $cursor = $this->getImageCollection()->find($queryData, $fields)->limit($query->limit())->sort($sort); // Skip some images if a page has been set if (($page = $query->page()) > 1) { $skip = $query->limit() * ($page - 1); $cursor->skip($skip); } foreach ($cursor as $image) { unset($image['_id']); $image['added'] = new DateTime('@' . $image['added'], new DateTimeZone('UTC')); $image['updated'] = new DateTime('@' . $image['updated'], new DateTimeZone('UTC')); $images[] = $image; } // Update model $model->setHits($cursor->count()); } catch (MongoException $e) { throw new DatabaseException('Unable to search for images', 500, $e); } return $images; }