Skip to content

Commit 810854e

Browse files
authored
Update README (#28)
1 parent c39b7da commit 810854e

1 file changed

Lines changed: 50 additions & 32 deletions

File tree

README.md

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,40 @@ A PHP library that parses and evaluates boolean expressions using a JavaScript-l
1313
Via Composer
1414

1515
```bash
16-
$ composer require nicoswd/php-rule-parser
16+
composer install nicoswd/php-rule-parser
1717
```
1818

1919
## Usage Examples
2020

21-
### E-commerce: Validate coupon eligibility
21+
E-commerce: Validate coupon eligibility
2222
```php
2323
$variables = [
2424
'cart_total' => 120,
2525
'user_tier' => 'gold',
2626
'is_blacklisted' => false,
2727
];
2828

29-
$rule = new Rule('cart_total >= 50 && user_tier in ["gold", "platinum"] && !is_blacklisted', $variables);
30-
var_dump($rule->isTrue()); // bool(true) — eligible for discount
31-
```
29+
$rule = new Rule('
30+
cart_total >= 50 &&
31+
user_tier in ["gold", "platinum"] &&
32+
!is_blacklisted
33+
', $variables);
3234

33-
### Content moderation: Flag suspicious posts
34-
```php
35-
$variables = [
36-
'body' => 'Check this out http://spam.com',
37-
'is_trusted_author' => false,
38-
];
39-
40-
$rule = new Rule('body.test(/(https?:\/\/[^\s]+){3,}/) && !is_trusted_author', $variables);
41-
var_dump($rule->isTrue()); // bool(true) — flagged as spam
35+
var_dump($rule->isTrue()); // bool(true) — eligible for discount
4236
```
4337

44-
### Access control: Check user permissions
38+
Access control: Check user permissions
4539
```php
4640
$variables = [
4741
'role' => 'editor',
48-
'status' => 'active',
4942
'is_suspended' => false,
5043
];
5144

52-
$rule = new Rule('role in ["admin", "editor"] && status == "active" && !is_suspended', $variables);
45+
$rule = new Rule('role in ["admin", "editor"] && !is_suspended', $variables);
5346
var_dump($rule->isTrue()); // bool(true) — access granted
5447
```
5548

56-
### Pricing: Calculate order total with conditions
49+
Pricing: Calculate order total with conditions
5750
```php
5851
$variables = [
5952
'base_price' => 29.99,
@@ -65,42 +58,45 @@ $rule = new Rule('(base_price + (base_price * tax_rate / 100)) * quantity', $var
6558
var_dump($rule->result()); // float(108.8571...) — total with tax
6659
```
6760

68-
### Form validation: Check input constraints
61+
Form validation: Check input constraints
6962
```php
7063
$variables = [
71-
'email' => 'user@example.com',
7264
'age' => 25,
7365
'country' => 'US',
7466
];
7567

76-
$rule = new Rule('email.test(/^[^@\s]+@[^@\s]+\.[^@\s]+$/) && age >= 18 && country in ["US", "CA", "UK"]', $variables);
68+
$rule = new Rule('age >= 18 && country in ["US", "CA", "UK"]', $variables);
7769
var_dump($rule->isTrue()); // bool(true) — valid registration
7870
```
7971

80-
### Notification routing: Target specific users
72+
Notification routing: Target specific users
8173
```php
8274
$variables = [
8375
'plan' => 'pro',
8476
'last_login' => 3,
8577
'notification_opt_out' => false,
8678
];
8779

88-
$rule = new Rule('plan in ["pro", "enterprise"] && last_login < 7 && !notification_opt_out', $variables);
80+
$rule = new Rule('
81+
plan in ["pro", "enterprise"] &&
82+
last_login < 7 &&
83+
!notification_opt_out
84+
', $variables);
85+
8986
var_dump($rule->isTrue()); // bool(true) — send notification
9087
```
9188

92-
### Feature flags: Roll out features gradually
89+
Feature flags: Roll out features gradually
9390
```php
9491
$variables = [
95-
'user_id' => 7,
96-
'environment' => 'production',
92+
'user_id' => 7,
9793
];
9894

99-
$rule = new Rule('user_id % 10 < 3 && environment == "production"', $variables);
95+
$rule = new Rule('user_id % 10 < 3', $variables);
10096
var_dump($rule->isTrue()); // bool(true) — feature enabled for this user
10197
```
10298

103-
### String manipulation: Format user data
99+
String manipulation: Format user data
104100
```php
105101
$variables = [
106102
'firstName' => 'John',
@@ -111,7 +107,7 @@ $rule = new Rule('firstName.toUpperCase() + " " + lastName.toUpperCase()', $vari
111107
var_dump($rule->result()); // string("JOHN DOE")
112108
```
113109

114-
### Object method calls: Evaluate complex conditions
110+
Object method calls: Evaluate complex conditions
115111
```php
116112
class Subscription
117113
{
@@ -134,9 +130,31 @@ $rule = new Rule('subscription.isActive() && subscription.daysUntilExpiry() > 7'
134130
var_dump($rule->isTrue()); // bool(true) — subscription is active and not expiring soon
135131
```
136132

137-
For security reasons, PHP's magic methods like __construct and __destruct cannot be
138-
called from within rules. However, __call will be invoked automatically if available,
139-
unless the called method is defined.
133+
Arithmetic: Operator precedence
134+
```php
135+
$rule = new Rule('2 + 3 * 4 == 14');
136+
var_dump($rule->isTrue()); // bool(true) - multiplication before addition
137+
138+
$rule = new Rule('(2 + 3) * 4 == 20');
139+
var_dump($rule->isTrue()); // bool(true) - parentheses override precedence
140+
```
141+
142+
Arithmetic: Unary operators
143+
```php
144+
$rule = new Rule('-5 * 3 == -15');
145+
var_dump($rule->isTrue()); // bool(true) - unary minus
146+
147+
$rule = new Rule('!false');
148+
var_dump($rule->isTrue()); // bool(true) - logical NOT
149+
150+
$rule = new Rule('!(1 == 2)');
151+
var_dump($rule->isTrue()); // bool(true) - NOT with comparison
152+
```
153+
154+
> [!NOTE]
155+
> For security reasons, PHP's magic methods like __construct and __destruct cannot be
156+
> called from within rules. However, __call will be invoked automatically if available,
157+
> unless the called method is defined.
140158
141159
## Built-in Methods
142160

0 commit comments

Comments
 (0)