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

Add "not" operators to text condition rules #16276

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
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: 6 additions & 0 deletions src/base/conditions/BaseConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ abstract class BaseConditionRule extends Component implements ConditionRuleInter
protected const OPERATOR_GT = '>';
protected const OPERATOR_GTE = '>=';
protected const OPERATOR_BEGINS_WITH = 'bw';
protected const OPERATOR_NOT_BEGINS_WITH = '!bw';
protected const OPERATOR_ENDS_WITH = 'ew';
protected const OPERATOR_NOT_ENDS_WITH = '!ew';
protected const OPERATOR_CONTAINS = '**';
protected const OPERATOR_NOT_CONTAINS = '!**';
protected const OPERATOR_IN = 'in';
protected const OPERATOR_NOT_IN = 'ni';
protected const OPERATOR_EMPTY = 'empty';
Expand Down Expand Up @@ -168,8 +171,11 @@ protected function operatorLabel(string $operator): string
self::OPERATOR_GT => Craft::t('app', 'is greater than'),
self::OPERATOR_GTE => Craft::t('app', 'is greater than or equals'),
self::OPERATOR_BEGINS_WITH => Craft::t('app', 'begins with'),
self::OPERATOR_NOT_BEGINS_WITH => Craft::t('app', 'does not begin with'),
self::OPERATOR_ENDS_WITH => Craft::t('app', 'ends with'),
self::OPERATOR_NOT_ENDS_WITH => Craft::t('app', 'does not end with'),
self::OPERATOR_CONTAINS => Craft::t('app', 'contains'),
self::OPERATOR_NOT_CONTAINS => Craft::t('app', 'does not contain'),
self::OPERATOR_IN => Craft::t('app', 'is one of'),
self::OPERATOR_NOT_IN => Craft::t('app', 'is not one of'),
self::OPERATOR_EMPTY => Craft::t('app', 'is empty'),
Expand Down
10 changes: 10 additions & 0 deletions src/base/conditions/BaseTextConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ protected function operators(): array
{
return [
self::OPERATOR_EQ,
self::OPERATOR_NE,
self::OPERATOR_BEGINS_WITH,
self::OPERATOR_NOT_BEGINS_WITH,
self::OPERATOR_ENDS_WITH,
self::OPERATOR_NOT_ENDS_WITH,
self::OPERATOR_CONTAINS,
self::OPERATOR_NOT_CONTAINS,
self::OPERATOR_NOT_EMPTY,
self::OPERATOR_EMPTY,
];
Expand Down Expand Up @@ -132,8 +136,11 @@ protected function paramValue(): ?string

return match ($this->operator) {
self::OPERATOR_BEGINS_WITH => "$value*",
self::OPERATOR_NOT_BEGINS_WITH => "not $value*",
self::OPERATOR_ENDS_WITH => "*$value",
self::OPERATOR_NOT_ENDS_WITH => "not *$value",
self::OPERATOR_CONTAINS => "*$value*",
self::OPERATOR_NOT_CONTAINS => "not *$value*",
default => "$this->operator $value",
};
}
Expand Down Expand Up @@ -165,8 +172,11 @@ protected function matchValue(mixed $value): bool
self::OPERATOR_GT => $value > $this->value,
self::OPERATOR_GTE => $value >= $this->value,
self::OPERATOR_BEGINS_WITH => is_string($value) && StringHelper::startsWith($value, $this->value),
self::OPERATOR_NOT_BEGINS_WITH => !(is_string($value) && StringHelper::startsWith($value, $this->value)),
self::OPERATOR_ENDS_WITH => is_string($value) && StringHelper::endsWith($value, $this->value),
self::OPERATOR_NOT_ENDS_WITH => !(is_string($value) && StringHelper::endsWith($value, $this->value)),
self::OPERATOR_CONTAINS => is_string($value) && StringHelper::contains($value, $this->value),
self::OPERATOR_NOT_CONTAINS => !(is_string($value) && StringHelper::contains($value, $this->value)),
default => throw new InvalidConfigException("Invalid operator: $this->operator"),
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,9 @@
'days ago' => 'days ago',
'days from now' => 'days from now',
'days' => 'days',
'does not being with' => 'does not being with',
'does not contain' => 'does not contain',
'does not end with' => 'does not end with',
'does not equal' => 'does not equal',
'draft' => 'draft',
'drafts' => 'drafts',
Expand Down