Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change method names for new WIldCard Strings handler #1814

Merged
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
6 changes: 3 additions & 3 deletions docs/filter-types/filters-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Text Filters
weight: 10
---

Text filters are just simple text fields.
Text filters are just simple text filters, allowing you to pass a string value into a builder query.

```php
public function filters(): array
Expand Down Expand Up @@ -133,7 +133,7 @@ public function filters(): array
}
```

#### setField
#### setFieldName
An optional method for setting the field to use when filtering, if used, you may omit the field from the above methods, for example:

```php
Expand All @@ -145,7 +145,7 @@ public function filters(): array
'placeholder' => 'Search Name',
'maxlength' => '25',
])
->setField('users.name')
->setFieldName('users.name')
->contains(),
];
}
Expand Down
28 changes: 28 additions & 0 deletions src/Views/Traits/Filters/HandlesApplyingFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Filters;

trait HandlesApplyingFilter
{
use HandlesFieldName;

protected bool $hasAppliedFilterAlready = false;

protected function shouldApplyFilter(?string $fieldName = null): bool
{
if (isset($fieldName)) {
$this->setFieldName($fieldName);
}
if ($this->hasFieldName()) {
if (! $this->hasAppliedFilterAlready) {
$this->hasAppliedFilterAlready = true;

return true;
} else {
return false;

Check warning on line 22 in src/Views/Traits/Filters/HandlesApplyingFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/HandlesApplyingFilter.php#L22

Added line #L22 was not covered by tests
}
}

return false;

Check warning on line 26 in src/Views/Traits/Filters/HandlesApplyingFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/HandlesApplyingFilter.php#L26

Added line #L26 was not covered by tests
}
}
14 changes: 7 additions & 7 deletions src/Views/Traits/Filters/HandlesFieldName.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

trait HandlesFieldName
{
protected ?string $field_name;
protected ?string $fieldName;

public function setField(string $field_name): self
public function setFieldName(string $fieldName): self
{
$this->field_name = $field_name;
$this->fieldName = $fieldName;

return $this;
}

public function hasField(): bool
public function hasFieldName(): bool
{
return isset($this->field_name);
return isset($this->fieldName);

}

public function getField(): string
public function getFieldName(): string
{
return $this->field_name;
return $this->fieldName;

}
}
64 changes: 19 additions & 45 deletions src/Views/Traits/Filters/HandlesWildcardStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,69 @@

trait HandlesWildcardStrings
{
use HandlesFieldName;
use HandlesApplyingFilter;

protected bool $hasRun = false;

public function contains(?string $field = null): self
public function contains(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->where($this->field_name, 'like', '%'.$value.'%');
$builder->where($this->getFieldName(), 'like', '%'.$value.'%');
});
$this->hasRun = true;
}

return $this;
}

public function notContains(?string $field = null): self
public function notContains(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->whereNot($this->field_name, 'like', '%'.$value.'%');
$builder->whereNot($this->getFieldName(), 'like', '%'.$value.'%');
});
$this->hasRun = true;
}

return $this;
}

public function startsWith(?string $field = null): self
public function startsWith(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->where($this->field_name, 'like', $value.'%');
$builder->where($this->getFieldName(), 'like', $value.'%');
});
$this->hasRun = true;
}

return $this;
}

public function notStartsWith(?string $field = null): self
public function notStartsWith(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->whereNot($this->field_name, 'like', $value.'%');
$builder->whereNot($this->getFieldName(), 'like', $value.'%');
});
$this->hasRun = true;
}

return $this;
}

public function endsWith(?string $field = null): self
public function endsWith(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->where($this->field_name, 'like', '%'.$value);
$builder->where($this->getFieldName(), 'like', '%'.$value);
});
$this->hasRun = true;
}

return $this;
}

public function notEndsWith(?string $field = null): self
public function notEndsWith(?string $fieldName = null): self
{
if (isset($field)) {
$this->setField($field);
}
if (! $this->hasRun) {
if ($this->shouldApplyFilter($fieldName)) {
$this->filter(function (Builder $builder, string $value) {
$builder->whereNot($this->field_name, 'like', '%'.$value);
$builder->whereNot($this->getFieldName(), 'like', '%'.$value);
});
$this->hasRun = true;
}

return $this;
Expand Down
12 changes: 6 additions & 6 deletions tests/Traits/Visuals/Filters/TextFilterVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->endsWith(),
];
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->notEndsWith(),
];
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->startsWith(),
];
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->notStartsWith(),
];
}
Expand Down Expand Up @@ -225,7 +225,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->contains(),
];
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public function filters(): array
{
return [
TextFilter::make('name')
->setField('name')
->setFieldName('name')
->notContains(),
];
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Views/Filters/TextFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ public function test_can_set_text_filter_wireable_live(): void
public function test_has_field_name(): void
{
$filter = TextFilter::make('BreedID');
$this->assertFalse($filter->hasField());
$this->assertFalse($filter->hasFieldName());

$filter->setField('breed_id');
$this->assertTrue($filter->hasField());
$filter->setFieldName('breed_id');
$this->assertTrue($filter->hasFieldName());

}

public function test_get_field_name(): void
{
$filter = TextFilter::make('BreedID');
$this->assertFalse($filter->hasField());
$this->assertFalse($filter->hasFieldName());

$filter->setField('breed_id');
$this->assertTrue($filter->hasField());
$this->assertSame('breed_id', $filter->getField());
$filter->setFieldName('breed_id');
$this->assertTrue($filter->hasFieldName());
$this->assertSame('breed_id', $filter->getFieldName());
}
}
Loading