{"id":41361111,"url":"https://github.com/omitobi/conditional","last_synced_at":"2026-01-23T07:48:42.384Z","repository":{"id":57031263,"uuid":"255391375","full_name":"omitobi/conditional","owner":"omitobi","description":"PHP if-else re-imagined","archived":false,"fork":false,"pushed_at":"2024-04-30T21:11:41.000Z","size":61,"stargazers_count":43,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-12-09T01:10:09.645Z","etag":null,"topics":["fluent-helper","if-statements","oop","php"],"latest_commit_sha":null,"homepage":"https://omitobi.github.io/conditional/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omitobi.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-13T17:07:49.000Z","updated_at":"2024-06-09T10:50:34.000Z","dependencies_parsed_at":"2022-08-23T18:50:47.324Z","dependency_job_id":null,"html_url":"https://github.com/omitobi/conditional","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/omitobi/conditional","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omitobi%2Fconditional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omitobi%2Fconditional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omitobi%2Fconditional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omitobi%2Fconditional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omitobi","download_url":"https://codeload.github.com/omitobi/conditional/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omitobi%2Fconditional/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28683996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T05:48:07.525Z","status":"ssl_error","status_checked_at":"2026-01-23T05:48:07.129Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fluent-helper","if-statements","oop","php"],"created_at":"2026-01-23T07:48:41.923Z","updated_at":"2026-01-23T07:48:42.379Z","avatar_url":"https://github.com/omitobi.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/omitobi/assets/blob/master/conditional/twitter_header_photo_2.png\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.com/omitobi/conditional\"\u003e \u003cimg src=\"https://travis-ci.com/omitobi/conditional.svg?branch=master\" alt=\"Build Status\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omitobisam/conditional\"\u003e \u003cimg src=\"https://poser.pugx.org/omitobisam/conditional/v/stable\" alt=\"Latest Stable Version\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omitobisam/conditional\"\u003e \u003cimg src=\"https://poser.pugx.org/omitobisam/conditional/downloads\" alt=\"Total Downloads\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omitobisam/conditional\"\u003e \u003cimg src=\"https://poser.pugx.org/omitobisam/conditional/v/unstable\" alt=\"Latest Unstable Version\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omitobisam/conditional\"\u003e \u003cimg src=\"https://poser.pugx.org/omitobisam/conditional/d/monthly\" alt=\"Latest Monthly Downloads\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/omitobisam/conditional\"\u003e \u003cimg src=\"https://poser.pugx.org/omitobisam/conditional/license\" alt=\"License\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## About Conditional\n\nif-else statements in a cleaner and beautiful way.\n\n```php\nconditional(isset($data))\n    -\u003ethen(fn() =\u003e doThis())\n    -\u003eelse(fn() =\u003e doThat());\n```\n\n## Installation\n\n`composer require omitobisam/conditional`\n\n## Minimum Requirement\n\n- PHP \u003e=7.4\n\n## Usage\n\nYou can call it simply statically:\n\n```php\n\nuse Conditional\\Conditional;\n$data = null;\n\nConditional::if(is_null($data))\n    -\u003ethen(fn() =\u003e doThis())\n    -\u003eelse(fn() =\u003e doThat());\n\n```\n\nConditional also comes with a helper function called `conditional()` and its used like so:\n\n```php\nconditional(isset($data))\n    -\u003ethen(fn() =\u003e doThis())\n    -\u003eelse(fn() =\u003e doThat());\n```\n\n:tada: Now like a tenary operator. Conditional at version 1.2 `else()` immediately returns the value of the last truthy execution:\n\n```php\nconditional('1' === 'a', 1, 2); //returns 2 - without calling -\u003evalue()\n\nconditional(false, 1)\n\n  -\u003eelse(2); //returns 2 - without calling -\u003evalue()\n\n// Of course the normal one\nconditional(false)\n\n  -\u003ethen(1)\n\n  -\u003eelse(2); //returns 2\n```\n\nYou can also evaluate a closure call on the conditional `if` method:\n\n```php\nuse Conditional\\Conditional;\n\nConditional::if(fn() =\u003e  '1' == 1) // or conditional(fn() =\u003e 1 + 1)\n    -\u003ethen(fn() =\u003e doThis()) // doThis() is executed\n    -\u003eelse(fn() =\u003e doThat());\n```\n\nConditional also allows returning values passed in the conditions.\nYou use `value()` method to get the values either the result of the closure passed or the values as they are. \n\n```php\nuse Conditional\\Conditional;\n\n$value = Conditional::if(fn() =\u003e 'a' !== 1) // or conditional(fn() =\u003e 'a' !== 1)\n    -\u003ethen(1)\n    -\u003evalue(); // returns 2 (because a !== 1)\n\n//do something with $value\n```\n\nFinally, `then()`  and `else()` methods accepts invokable class or objects. Lets see:\n\n```php\nuse Conditional\\Conditional;\n\nclass Invokable {\n\n    public function __invoke()\n    {\n        return 'I was Invoked';\n    }\n}\n\n$invokableClass = new Invokable();\n\n$value = Conditional::if(fn() =\u003e 'a' === 1) // or conditional(fn() =\u003e 1 + 1)\n    -\u003ethen(1)\n    -\u003eelse($invokableClass); //Value returns 'I was Invoked'\n\n// Do something with $value\n```\n\nYou are also allowed to throw exception based on the condition like so:\n\n```php\n \\conditional('foo' === 'bar')\n\n    -\u003ethen('foo === bar')\n\n    -\u003eelse(new TestException('Foo is not the same as bar'));  //this exception is thrown\n```\n\n### Newly released\n\n`elseIf()` method of Conditional like so:\n\n```php\nconditional(isset($data))\n\n    -\u003ethen(fn() =\u003e doThis())\n\n    -\u003eelseIf(is_int(1))\n\n    -\u003ethen(fn() =\u003e doThat())\n\n    -\u003eelse(2);\n```\n\n`elseIf()` can be called multiple times on an instance:\n\n```php\n$value = Conditional::if(false)\n\n    -\u003ethen('a')\n\n    -\u003eelseIf('b' == 1) //first one\n\n    -\u003ethen('b')\n\n    -\u003eelseIf('b' !== 2) //another\n\n    -\u003ethen('2')\n\n    -\u003eelse(1);\n\n// $value is '2'\n```\n\n### Coming Soon\n\n`If()` and `elseIf()` statement accepting a default value when no condition is met and `else()` is not called like so:\n\n```php\nConditional::if(is_array('a'), 'ninja') //default value is ninja\n\n    -\u003ethen(fn() =\u003e doThis())\n\n    -\u003eelseIf(is_int(\"\"))\n\n    -\u003ethen(fn() =\u003e doThat())\n    \n    -\u003evalue(); // 'ninja' is returned :scream:\n```\n\nMultiple conditional check like `a \u0026\u0026 b \u0026\u0026 c \u0026\u0026 d` or `a || b || c ||...` syntax\n\n- Help wanted for this\n\n## Caveats (or Awareness)\n\n- 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:\n\n```php\n// This is Wrong!\n\nConditional::if(true)\n    -\u003ethen(1)\n    -\u003eelse(2)\n    -\u003eif('1'); // Don't do it except you intend to start a new and fresh if Conditional\n```\n\u003e See: tests/ConditionalTest::testEveryIfCallCreatesNewFreshInstance test. On the last line of that test, the two conditionals are not the same.\n- Conditional relies on closures to return non closure values passed to then.\n\u003e In the future release it will be optional for `then` and `else` method\n\n## Contributions\n\n- More tests are needed\n- Issues have been opened\n- How about those static properties, any idea how to reduce the number of static properties used?\n- Performance optimization (?)\n\n## Development\n\nFor new feature, checkout with prefix `feat-#issueid` e.g `feature-#100-add-auto-deloy`\n\n- \n- Clone this repository\n- run `sh dockerizer.sh` or `bash dockerizer.sh`\n- execute into the docker environment with `docker-compose exec conditional sh` (`sh` can be another bash)\n- run tests with `vendor/bin/phpunit`\n\n## Licence\n\nMIT (see LICENCE file)\n\n## Additional Information\n\nOther related packages:\n\n- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way]\n- https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way]\n- https://github.com/transprime-research/attempt [A smart PHP try...catch statement]\n- https://github.com/omitobi/carbonate [A smart Carbon + Collection package]\n- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response]\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomitobi%2Fconditional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomitobi%2Fconditional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomitobi%2Fconditional/lists"}