Blame view
app/Queries/Article/ArticleQuery.php
1.66 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php namespace FootyRoom\Queries\Article; use MongoDB\BSON\ObjectId; use FootyRoom\Support\AutoMapper; use FootyRoom\Support\MongoClient; class ArticleQuery { /** @var \FootyRoom\Support\MongoClient */ protected $mongo; public function __construct(MongoClient $mongo) { $this->mongo = $mongo; } /** * @return \FootyRoom\Queries\Article\Article[] */ public function find(int $limit, int $offset = 0): array { return $this->findBy([], $limit, $offset); } /** * @return \FootyRoom\Queries\Article\Article[] */ public function findPublished(int $limit, int $offset = 0): array { return $this->findBy(['status' => 'published'], $limit, $offset); } public function findOne(string $id): ?Article { $article = $this->mongo->footyroom->articles->findOne(['_id' => new ObjectId($id)]); if (!$article) { return null; } $article->id = (string) $article->_id; $article = AutoMapper::map($article, Article::class); return $article; } /** * @return \FootyRoom\Queries\Article\Article[] */ private function findBy($query, int $limit, int $offset = 0): array { $entities = []; $articles = $this->mongo->footyroom->articles->find($query, [ 'limit' => $limit, 'skip' => $offset, 'sort' => ['isFeatured' => -1, 'createdAt' => -1, '_id' => -1], ]); foreach ($articles as $article) { $article->id = (string) $article->_id; $entities[] = AutoMapper::map($article, Article::class); } return $entities; } } |