Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Stache/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class Builder extends BaseBuilder
{
protected $store;
protected $randomize = false;
private static $hasOwnEqualityTest = [];

public function __construct(Store $store)
{
Expand Down Expand Up @@ -148,13 +149,31 @@ protected function getItems($keys)

protected function filterWhereBasic($values, $where)
{
if ($this->comparesEqualityAsStrings($where)) {
// What's being looked for is the same for every value, so it's lowercased once rather than for each of them.
$needle = strtolower($where['value'] ?? '');

return $values->filter(fn ($value) => strtolower($value ?? '') === $needle);
}

return $values->filter(function ($value) use ($where) {
$method = 'filterTest'.$this->operators[$where['operator']];

return $this->{$method}($value, $where['value']);
});
}

private function comparesEqualityAsStrings($where)
{
if ($where['operator'] !== '=' || ! (is_null($where['value']) || is_scalar($where['value']))) {
return false;
}

// A builder with its own idea of what equal means gets to use it.
return ! (self::$hasOwnEqualityTest[static::class]
??= (new \ReflectionMethod($this, 'filterTestEquals'))->getDeclaringClass()->getName() !== BaseBuilder::class);
}

protected function filterWhereIn($values, $where)
{
$lookup = array_flip(array_map($this->normalizeLookupValue(...), $where['values']));
Expand Down
63 changes: 63 additions & 0 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,69 @@ public function sorting_by_unsafe_method_does_not_invoke_it()

$this->assertCount($count, Entry::all());
}

#[Test]
#[DataProvider('equalsProvider')]
public function entries_are_found_by_what_a_value_equals($value, $expected)
{
Collection::make('posts')->save();
collect([
'a' => 'Red', 'b' => 'red', 'c' => 'RED ', 'd' => null, 'e' => '', 'f' => true, 'g' => false,
'h' => 1, 'i' => '1', 'j' => 0, 'k' => 1.5, 'l' => '1.5', 'm' => 'Ünï',
])->each(fn ($thing, $id) => EntryFactory::id($id)->slug($id)->collection('posts')->data(['thing' => $thing])->create());

$this->assertEquals($expected, Entry::query()->where('thing', $value)->get()->map->id()->all());
$this->assertEquals($expected, Entry::query()->where('thing', '=', $value)->get()->map->id()->all());
}

public static function equalsProvider()
{
return [
'string' => ['red', ['a', 'b']],
'string in another case' => ['RED', ['a', 'b']],
'string with a space' => ['red ', ['c']],
'empty string' => ['', ['d', 'e', 'g']],
'null' => [null, ['d', 'e', 'g']],
'true' => [true, ['f', 'h', 'i']],
'false' => [false, ['d', 'e', 'g']],
'one' => [1, ['f', 'h', 'i']],
'one as a string' => ['1', ['f', 'h', 'i']],
'zero' => [0, ['j']],
'float' => [1.5, ['k', 'l']],
'accented' => ['Ünï', ['m']],
];
}

#[Test]
public function a_date_is_found_by_the_date_it_equals()
{
Collection::make('posts')->dated(true)->save();
EntryFactory::id('a')->slug('a')->collection('posts')->date('2021-11-15')->create();
EntryFactory::id('b')->slug('b')->collection('posts')->date('2021-11-14')->create();

$this->assertEquals(['a'], Entry::query()->where('date', \Illuminate\Support\Carbon::parse('2021-11-15'))->get()->map->id()->all());
$this->assertEquals(['a'], Entry::query()->where('date', '2021-11-15 00:00:00')->get()->map->id()->all());
}

#[Test]
public function a_query_builder_with_its_own_idea_of_equal_gets_to_use_it()
{
Collection::make('posts')->save();
EntryFactory::id('a')->slug('a')->collection('posts')->data(['thing' => 'Red'])->create();
EntryFactory::id('b')->slug('b')->collection('posts')->data(['thing' => 'red'])->create();

$query = new CaseSensitiveEntryQueryBuilder(app(\Statamic\Stache\Stache::class)->store('entries'));

$this->assertEquals(['b'], $query->where('thing', 'red')->get()->map->id()->all());
}
}

class CaseSensitiveEntryQueryBuilder extends \Statamic\Stache\Query\EntryQueryBuilder
{
protected function filterTestEquals($item, $value)
{
return $item === $value;
}
}

class CustomScope extends Scope
Expand Down
Loading