PHP code example of changhorizon / sql-condition

1. Go to this page and download the library: Download changhorizon/sql-condition library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

changhorizon / sql-condition example snippets


use ChangHorizon\SqlCondition\Expression;

// Simple conditions
$eq  = Expression::EQ('name', 'John');
$gt  = Expression::GT('age', '18');
$in  = Expression::IN('status', ['active', 'pending']);
$like = Expression::LIKE('title', 'hello');

$eq->getString();  // "name = :value_name"
$eq->getParams();  // [':value_name' => 'John']

use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Expression;

$cond = new Condition([
    Expression::EQ('name', 'John'),
    Expression::GT('age', '18'),
    Expression::IS_NULL('deleted_at'),
]);

$cond->getString();  // "name = :value_name AND age > :value_age AND deleted_at IS NULL"
$cond->getParams();  // [':value_name' => 'John', ':value_age' => '18']

use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Enums\Logic;

$cond = new Condition([
    Expression::EQ('role', 'admin'),
    Expression::EQ('role', 'moderator'),
], Logic::OR);

$cond->getString();  // "role = :value_role OR role = :value_role"

$adminOrMod = new Condition([
    Expression::EQ('role', 'admin'),
    Expression::EQ('role', 'moderator'),
], Logic::OR);

$cond = new Condition([
    $adminOrMod,
    Expression::IS_TRUE('is_active'),
]);

$cond->getString();  // "(role = :value_role OR role = :value_role) AND is_active IS TRUE"

use ChangHorizon\SqlCondition\Enums\Anchor;
use ChangHorizon\SqlCondition\Expressions\Like;

// Default (both sides): %value%
new Like('title', 'hello')->getParams();       // [':value_title' => '%:hello%']

// Left anchor only: value%
new Like('title', 'hello', Anchor::LEFT)->getParams();   // [':value_title' => ':hello%']

// Right anchor only: %value
new Like('title', 'hello', Anchor::RIGHT)->getParams();  // [':value_title' => '%:hello']

$cond = new Condition([
    Expression::EQ('email', '[email protected]'),
    Expression::IS_NOT_NULL('verified_at'),
]);

$sql  = 'SELECT * FROM users WHERE ' . $cond->getString();
$stmt = $pdo->prepare($sql);
$stmt->execute($cond->getParams());
txt
src/
├── Condition.php               # Expression combiner with AND/OR logic
├── Expression.php              # Static factory (EQ, NEQ, GT, ...)
├── Enums/
│   ├── Anchor.php              # LIKE anchor direction
│   ├── Logic.php               # AND / OR
│   └── Operator.php            # 16 SQL operators
├── Expressions/
│   ├── Between.php / NotBetween.php
│   ├── Equal.php (+ NEQ, GT, GTE, LT, LTE via inheritance)
│   ├── In.php / NotIn.php
│   ├── IsNull.php (+ IsNotNull, IsTrue, IsFalse via inheritance)
│   └── Like.php / NotLike.php
└── Interfaces/
    └── ExpressionInterface.php