定制 omitobisam/conditional 二次开发

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

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

omitobisam/conditional

Composer 安装命令:

composer require omitobisam/conditional

包简介

A fluent helper for object-oriented style of if-else statements

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version Latest Monthly Downloads License

About Conditional

if-else statements in a cleaner and beautiful way.

conditional(isset($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());

Installation

composer require omitobisam/conditional

Minimum Requirement

  • PHP >=7.4

Usage

You can call it simply statically:

use Conditional\Conditional;
$data = null;

Conditional::if(is_null($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());

Conditional also comes with a helper function called conditional() and its used like so:

conditional(isset($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());

🎉 Now like a tenary operator. Conditional at version 1.2 else() immediately returns the value of the last truthy execution:

conditional('1' === 'a', 1, 2); //returns 2 - without calling ->value()

conditional(false, 1)

  ->else(2); //returns 2 - without calling ->value()

// Of course the normal one
conditional(false)

  ->then(1)

  ->else(2); //returns 2

You can also evaluate a closure call on the conditional if method:

use Conditional\Conditional;

Conditional::if(fn() =>  '1' == 1) // or conditional(fn() => 1 + 1)
    ->then(fn() => doThis()) // doThis() is executed
    ->else(fn() => doThat());

Conditional also allows returning values passed in the conditions. You use value() method to get the values either the result of the closure passed or the values as they are.

use Conditional\Conditional;

$value = Conditional::if(fn() => 'a' !== 1) // or conditional(fn() => 'a' !== 1)
    ->then(1)
    ->value(); // returns 2 (because a !== 1)

//do something with $value

Finally, then() and else() methods accepts invokable class or objects. Lets see:

use Conditional\Conditional;

class Invokable {

    public function __invoke()
    {
        return 'I was Invoked';
    }
}

$invokableClass = new Invokable();

$value = Conditional::if(fn() => 'a' === 1) // or conditional(fn() => 1 + 1)
    ->then(1)
    ->else($invokableClass); //Value returns 'I was Invoked'

// Do something with $value

You are also allowed to throw exception based on the condition like so:

 \conditional('foo' === 'bar')

    ->then('foo === bar')

    ->else(new TestException('Foo is not the same as bar'));  //this exception is thrown

Newly released

elseIf() method of Conditional like so:

conditional(isset($data))

    ->then(fn() => doThis())

    ->elseIf(is_int(1))

    ->then(fn() => doThat())

    ->else(2);

elseIf() can be called multiple times on an instance:

$value = Conditional::if(false)

    ->then('a')

    ->elseIf('b' == 1) //first one

    ->then('b')

    ->elseIf('b' !== 2) //another

    ->then('2')

    ->else(1);

// $value is '2'

Coming Soon

If() and elseIf() statement accepting a default value when no condition is met and else() is not called like so:

Conditional::if(is_array('a'), 'ninja') //default value is ninja

    ->then(fn() => doThis())

    ->elseIf(is_int(""))

    ->then(fn() => doThat())
    
    ->value(); // 'ninja' is returned :scream:

Multiple conditional check like a && b && c && d or a || b || c ||... syntax

  • Help wanted for this

Caveats (or Awareness)

  • As at version 1.x, Calling if() method returns an instance of Condtional, so do not call it twice on the same instance for example:
// This is Wrong!

Conditional::if(true)
    ->then(1)
    ->else(2)
    ->if('1'); // Don't do it except you intend to start a new and fresh if Conditional

See: tests/ConditionalTest::testEveryIfCallCreatesNewFreshInstance test. On the last line of that test, the two conditionals are not the same.

  • Conditional relies on closures to return non closure values passed to then.

In the future release it will be optional for then and else method

Contributions

  • More tests are needed
  • Issues have been opened
  • How about those static properties, any idea how to reduce the number of static properties used?
  • Performance optimization (?)

Development

For new feature, checkout with prefix feat-#issueid e.g feature-#100-add-auto-deloy

  • Clone this repository
  • run sh dockerizer.sh or bash dockerizer.sh
  • execute into the docker environment with docker-compose exec conditional sh (sh can be another bash)
  • run tests with vendor/bin/phpunit

Licence

MIT (see LICENCE file)

Additional Information

Other related packages:

omitobisam/conditional 适用场景与选型建议

omitobisam/conditional 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 175 次下载、GitHub Stars 达 43, 最近一次更新时间为 2020 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helpers」 「laravel」 「conditionals」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 omitobisam/conditional 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 omitobisam/conditional 我们能提供哪些服务?
定制开发 / 二次开发

基于 omitobisam/conditional 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-16