From fb10c442070f946540d6c6ac1b1cd793c7b5d180 Mon Sep 17 00:00:00 2001 From: Thomas Alrek Date: Mon, 2 Dec 2019 13:19:20 +0100 Subject: [PATCH] Add support for $_fetch_unpublished modifier --- src/cli.php | 3 ++ src/model/Structure.php | 62 ++++++++++++++++++++++++++++++------ src/model/StructureQuery.php | 6 +++- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/cli.php b/src/cli.php index ea901e9..00ca95e 100644 --- a/src/cli.php +++ b/src/cli.php @@ -1,6 +1,9 @@ use_time) { + $start = null; + + try { + $start = Carbon::parse($this->start); + } catch (Exception $ex) { + $start = Carbon::parse(0); + } + + try { + $stop = $this->stop ? $this->stop : PHP_INT_MAX; + $stop = Carbon::parse($stop); + } catch (Exception $ex) { + $stop = Carbon::parse(PHP_INT_MAX); + } + + $now = Carbon::now(); + + return $now->gte($start) && $now->lte($stop); + } + + return (bool) $published; + } + public function save() { static::performHookOn($this, 'saving'); @@ -273,7 +307,7 @@ public static function all() $cacheKey = 'entry/' . $entry['id']; NF::$cache->save($cacheKey, $entry); return static::generateObject($entry); - }, $response))->values(); + }, $response))->filter()->values(); } public static function find($id) @@ -386,7 +420,7 @@ public static function resolveOrFail($slug) public static function query(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'query'], $args); } @@ -394,7 +428,7 @@ public static function query(...$args) public static function count() { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'count'], []); } @@ -402,7 +436,7 @@ public static function count() public static function where(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'where'], $args); } @@ -410,7 +444,7 @@ public static function where(...$args) public static function pluck(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'pluck'], $args); } @@ -418,7 +452,7 @@ public static function pluck(...$args) public static function whereBetween(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'whereBetween'], $args); } @@ -426,7 +460,7 @@ public static function whereBetween(...$args) public static function orderBy(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'orderBy'], $args); } @@ -434,7 +468,7 @@ public static function orderBy(...$args) public static function first() { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'firstOrFail'], []); } @@ -442,7 +476,7 @@ public static function first() public static function paginate(...$args) { $structureId = (new static)->directory; - $query = new StructureQuery($structureId, new static); + $query = new StructureQuery($structureId, new static, static::$_fetch_unpublished); return call_user_func_array([$query, 'paginate'], $args); } @@ -462,7 +496,15 @@ public static function generateObject($data) return $revision; } - return new static($data); + $entry = new static($data); + + if ($entry) { + if (!$_mode && static::$_fetch_unpublished === false || static::$_fetch_unpublished === false && $_mode === 'cli') { + return $entry->published ? $entry : null; + } + + return $entry; + } } private static function bootUnlessBooted() diff --git a/src/model/StructureQuery.php b/src/model/StructureQuery.php index f65086c..0564346 100644 --- a/src/model/StructureQuery.php +++ b/src/model/StructureQuery.php @@ -26,13 +26,17 @@ class StructureQuery private $_firstPageSize = null; private $_fieldsModified = false; - public function __construct($directory, $class) + public function __construct($directory, $class, $fetchUnpublished = true) { $this->_class = $class; $this->_directory = $directory; $this->_search = new ElasticSearch(); $this->_search->directory($this->_directory); $this->_search->limit(10000); + + if (!$fetchUnpublished) { + $this->_search->where('published', true); + } } private function isArgsArray($args)