定制 rain1/condition-builder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rain1/condition-builder

Composer 安装命令:

composer require rain1/condition-builder

包简介

Condition Builder for logical expressions

README 文档

README

Build Status

This package allows you to build query conditions for PDO-like interfaces.

This is NOT an ORM: it's a condition builder. Let see the example:

Usage

<?php

require_once "vendor/autoload.php";

use rain1\ConditionBuilder\ConditionBuilder;
use rain1\ConditionBuilder\Operator\IsEqual;
use rain1\ConditionBuilder\Operator\IsLess;

$condition = new ConditionBuilder(ConditionBuilder::MODE_AND);

$condition->append(
	new IsEqual("id", 3), // id = ? AND
	new IsLess("priority", 100), // priority < ? AND
	new IsEqual("myCol", [1,2,3]), // myCol IN (?,?,?)
	new IsEqual("anotherCol", null), // it will be ignored because null value
	(new ConditionBuilder(ConditionBuilder::MODE_OR))
		->append(
			new IsEqual("col1", 3), // col1 = ? OR
			new IsEqual("someflag", 0) // someflag = ?
		)
);

$query =  "SELECT * FROM myTable WHERE $condition";

echo $query."\n";
print_r($condition()); // shortcut of $condition->values() it returns an array with all values

//$res = $pdo->prepare($query)->execute($condition->values());

This will produce the following output:


SELECT * FROM myTable WHERE (id = ? AND priority < ? AND myCol IN (?,?,?) AND (col1 = ? OR someflag = ?))
Array
(
    [0] => 3
    [1] => 100
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 3
    [6] => 0
)

Where is the magic?

Well, let suppose we want to write a function that filter rows in a MySQL table. Generally, if we don't want use some magic framework or ORM, we have to do a lot of if conditions and we have to concatenate the query and push params values.

That's the ConditionBuilder internal job.

<?php
function filterUser(array $filters = []) : array
{
    $defaults = [
        'id'                     => null,
        'banned'                 => null,
        'last_login_range_start' => null,
        'last_login_range_end'   => null,
        'email'                  => null,
    ];
    $filters  = $filters + $defaults;

    $condition = new ConditionBuilder(ConditionBuilder::MODE_AND);

    $condition->append(
        new IsEqual('id', $filters['id']),
        new IsEqual('email', $filters['email']),
        new IsEqual('banned', $filters['banned']),
        new IsBetween("last_login", $filters["last_login_range_start"], $filters["last_login_range_end"])
    );
    
    return YourMysqlLibrary::query("SELECT * FROM user WHERE $condition", $condition());
}

$rows = filterUser([
    'id' => [1,2,3]
]); // users with id 1,2 or 3 

$rows = filterUser([
    'last_login_range_start' => date("Y-m-d 00:00:00", "yesterday"),
    'banned' => 0
]); // users not banned and logged yesterday or today

$rows = filterUser([
    'last_login_range_start' => "2021-01-01",
    'last_login_range_end' => "2021-01-31",
    'banned' => 1
]); // users actually banned and last seen in Jan 2021

$rows = filterUser([
    'email' => "user@example.org"
]); // user with specified email
// and so on

So you can easily build a lot of searches with a minimal function.

Of course, it is a simple condition helper, so your query can be more complex. You can use more than one ConditionBuilder and you can append them to another.

Beaware

If the ConditionBuilder is empty (it means all filters are null), the result will be:

  • (TRUE), if the mode is ConditionBuilder::MODE_AND
  • (FALSE), if the mode is ConditionBuilder::MODE_OR

So be carefull especially if you use it with DELETE statements.

统计信息

  • 总下载量: 2.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固