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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ if ($rule = $firewall->getLastMatchedRule()) {

### Building Conditions

Conditions can be created fluently or by parsing JSON definitions:
Conditions can be created fluently or by encoding/decoding JSON definitions:

```php
$condition = Condition::and([
Condition::equal('ip', ['10.0.0.1']),
Condition::notEqual('path', '/health'),
]);

$json = $condition->toString();
$parsed = Condition::parse($json);
$json = $condition->encode();
$parsed = Condition::decode($json);
```

Available operators mirror the database query builder: `equal`, `notEqual`, `lessThan`, `greaterThan`, `contains`, `between`, `startsWith`, `endsWith`, `isNull`, `and`, `or`, and more.
Expand Down
6 changes: 3 additions & 3 deletions src/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public static function isMethod(string $value): bool
}

/**
* Parses a JSON encoded condition.
* Decode a JSON encoded condition string.
*/
public static function parse(string $payload): self
public static function decode(string $payload): self
{
try {
$decoded = \json_decode($payload, true, flags: JSON_THROW_ON_ERROR);
Expand Down Expand Up @@ -223,7 +223,7 @@ public function toArray(): array
*
* @throws ConditionException
*/
public function toString(): string
public function encode(): string
{
try {
return \json_encode($this->toArray(), flags: JSON_THROW_ON_ERROR);
Expand Down
6 changes: 3 additions & 3 deletions tests/ConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public function testConditionSerializationRoundTrip(): void
]),
]);

$json = $condition->toString();
$parsed = Condition::parse($json);
$json = $condition->encode();
$parsed = Condition::decode($json);

$this->assertTrue($parsed->matches(['ip' => '127.0.0.1', 'path' => '/api/users']));
$this->assertTrue($parsed->matches(['ip' => '127.0.0.1', 'path' => '/status.json']));
Expand All @@ -185,6 +185,6 @@ public function testParseRejectsInvalidJson(): void
{
$this->expectException(ConditionException::class);

Condition::parse('{"method":');
Condition::decode('{"method":');
}
}