Skip to content

Commit

Permalink
Its a bit yuck but
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Dec 2, 2024
1 parent 98d5bef commit 4aeb464
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidQueryDateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Statamic\Exceptions;

/**
* When an action for one type of entry is performed on another.
* For example, trying to access the date of an entry when it is ordered numerically.
*/
class InvalidQueryDateException extends \Exception
{
}
17 changes: 13 additions & 4 deletions src/Stache/Query/QueriesEntryStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Statamic\Stache\Query;

use Illuminate\Support\Collection;
use Statamic\Exceptions\InvalidQueryDateException;
use Statamic\Query\EmptyQueryBuilder;

trait QueriesEntryStatus
{
Expand All @@ -26,7 +28,14 @@ public function whereStatus(string $status)

return $this->where(fn ($query) => $this
->getCollectionsForStatusQuery()
->each(fn ($collection) => $query->orWhere(fn ($q) => $this->addCollectionStatusLogicToQuery($q, $status, $collection))));
->each(function ($collection) use ($query, $status) {
try {
return $query->orWhere(fn ($q) => $this->addCollectionStatusLogicToQuery($q, $status, $collection));
} catch (InvalidQueryDateException $e) {
return new EmptyQueryBuilder();
}
})
);
}

private function addCollectionStatusLogicToQuery($query, $status, $collection): void
Expand All @@ -35,7 +44,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection):

if (! $collection->dated() || ($collection->futureDateBehavior() === 'public' && $collection->pastDateBehavior() === 'public')) {
if ($status === 'scheduled' || $status === 'expired') {
$query->where('date', 'invalid'); // intentionally trigger no results.
throw new InvalidQueryDateException(); // intentionally trigger no results.
}

return;
Expand All @@ -47,7 +56,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection):
: $query->where('date', '<', now());

if ($status === 'expired') {
$query->where('date', 'invalid'); // intentionally trigger no results.
throw new InvalidQueryDateException(); // intentionally trigger no results.
}
}

Expand All @@ -57,7 +66,7 @@ private function addCollectionStatusLogicToQuery($query, $status, $collection):
: $query->where('date', '>', now());

if ($status === 'scheduled') {
$query->where('date', 'invalid'); // intentionally trigger no results.
throw new InvalidQueryDateException(); // intentionally trigger no results.
}
}
}
Expand Down

0 comments on commit 4aeb464

Please sign in to comment.