├── .github └── workflows │ └── php.yml ├── .gitignore ├── .travis.yml ├── Dockerfile ├── Helpers └── helpers.php ├── LICENSE ├── _config.yml ├── composer.json ├── docker-compose.yml ├── dockerizer.sh ├── phpunit.xml ├── readme.md ├── src ├── Conditional.php └── Exceptions │ └── InvalidConditionOrderException.php └── tests └── ConditionalTest.php /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: PHP Composer 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | permissions: 10 | contents: read 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | 20 | - name: Validate composer.json and composer.lock 21 | run: composer validate --strict 22 | 23 | - name: Cache Composer packages 24 | id: composer-cache 25 | uses: actions/cache@v3 26 | with: 27 | path: vendor 28 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} 29 | restore-keys: | 30 | ${{ runner.os }}-php- 31 | 32 | - name: Install dependencies 33 | run: composer install --prefer-dist --no-progress 34 | 35 | # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" 36 | # Docs: https://getcomposer.org/doc/articles/scripts.md 37 | 38 | - name: Run test suite 39 | run: composer run-script test 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bash_history 2 | /vendor 3 | .phpunit.result.cache 4 | /.idea 5 | .vscode 6 | .composer 7 | composer.lock -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.2 5 | - 7.3 6 | - 7.4 7 | 8 | before_script: 9 | - composer self-update 10 | - composer install --prefer-source --no-interaction --dev 11 | 12 | script: vendor/bin/phpunit -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Set master image 2 | FROM php:7.4-fpm-alpine 3 | 4 | # Copy composer.lock and composer.json 5 | COPY composer.json /var/www/html/ 6 | 7 | # Set working directory 8 | WORKDIR /var/www/html 9 | 10 | # Install Additional dependencies 11 | RUN apk update && apk add --no-cache \ 12 | build-base shadow vim curl \ 13 | php7 \ 14 | php7-fpm \ 15 | php7-common \ 16 | php7-mcrypt \ 17 | php7-mbstring \ 18 | php7-xml \ 19 | php7-openssl \ 20 | php7-json \ 21 | php7-phar \ 22 | php7-zip \ 23 | php7-gd \ 24 | php7-dom \ 25 | php7-session \ 26 | php7-zlib 27 | 28 | # Add and Enable PHP-PDO Extenstions 29 | 30 | # Install PHP Composer 31 | RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer 32 | 33 | # Remove Cache 34 | RUN rm -rf /var/cache/apk/* -------------------------------------------------------------------------------- /Helpers/helpers.php: -------------------------------------------------------------------------------- 1 | then($then); 20 | } 21 | 22 | if (func_num_args() === 3) { 23 | return $conditional->then($then)->else($else); 24 | } 25 | 26 | return $conditional; 27 | } 28 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Oluwatobi Samuel Omisakin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-leap-day -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "omitobisam/conditional", 3 | "description": "A fluent helper for object-oriented style of if-else statements", 4 | "license": "MIT", 5 | "keywords": [ 6 | "Conditionals", 7 | "Helpers", 8 | "Laravel" 9 | ], 10 | "homepage": "https://omitobi.github.io/conditional/", 11 | "support": { 12 | "issues": "https://github.com/omitobi/conditional/issues", 13 | "source": "https://github.com/omitobi/conditional/issues" 14 | }, 15 | "authors": [ 16 | { 17 | "name": "Oluwatobi Samuel Omisakin", 18 | "email": "omisakin.oluwatobi@gmail.com" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=7.4" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "~8.0", 26 | "symfony/var-dumper": "^6.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Conditional\\": "src" 31 | }, 32 | "files": [ 33 | "Helpers/helpers.php" 34 | ] 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Conditional\\Tests\\": "tests/" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "vendor/bin/phpunit" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | services: 4 | conditional: 5 | build: 6 | context: . 7 | dockerfile: Dockerfile 8 | container_name: conditional 9 | restart: unless-stopped 10 | tty: true 11 | working_dir: /var/www/html 12 | environment: 13 | SERVICE_TAGS: dev 14 | SERVICE_NAME: conditional 15 | volumes: 16 | - ./:/var/www/html 17 | networks: 18 | - conditional-network 19 | 20 | #Docker Networks 21 | networks: 22 | conditional-network: 23 | driver: bridge -------------------------------------------------------------------------------- /dockerizer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | no_rebuild=$1 4 | 5 | if [ "$no_rebuild" != '--no_rebuild' ] 6 | then 7 | printf "Rebuilding docker image because of missing --no_rebuild flag\n" 8 | docker-compose build 9 | fi 10 | 11 | docker-compose up -d --remove-orphans 12 | 13 | docker-compose exec conditional composer install -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 |

6 | Build Status 7 | Latest Stable Version 8 | Total Downloads 9 | Latest Unstable Version 10 | Latest Monthly Downloads 11 | License 12 |

13 | 14 | ## About Conditional 15 | 16 | if-else statements in a cleaner and beautiful way. 17 | 18 | ```php 19 | conditional(isset($data)) 20 | ->then(fn() => doThis()) 21 | ->else(fn() => doThat()); 22 | ``` 23 | 24 | ## Installation 25 | 26 | `composer require omitobisam/conditional` 27 | 28 | ## Minimum Requirement 29 | 30 | - PHP >=7.4 31 | 32 | ## Usage 33 | 34 | You can call it simply statically: 35 | 36 | ```php 37 | 38 | use Conditional\Conditional; 39 | $data = null; 40 | 41 | Conditional::if(is_null($data)) 42 | ->then(fn() => doThis()) 43 | ->else(fn() => doThat()); 44 | 45 | ``` 46 | 47 | Conditional also comes with a helper function called `conditional()` and its used like so: 48 | 49 | ```php 50 | conditional(isset($data)) 51 | ->then(fn() => doThis()) 52 | ->else(fn() => doThat()); 53 | ``` 54 | 55 | :tada: Now like a tenary operator. Conditional at version 1.2 `else()` immediately returns the value of the last truthy execution: 56 | 57 | ```php 58 | conditional('1' === 'a', 1, 2); //returns 2 - without calling ->value() 59 | 60 | conditional(false, 1) 61 | 62 | ->else(2); //returns 2 - without calling ->value() 63 | 64 | // Of course the normal one 65 | conditional(false) 66 | 67 | ->then(1) 68 | 69 | ->else(2); //returns 2 70 | ``` 71 | 72 | You can also evaluate a closure call on the conditional `if` method: 73 | 74 | ```php 75 | use Conditional\Conditional; 76 | 77 | Conditional::if(fn() => '1' == 1) // or conditional(fn() => 1 + 1) 78 | ->then(fn() => doThis()) // doThis() is executed 79 | ->else(fn() => doThat()); 80 | ``` 81 | 82 | Conditional also allows returning values passed in the conditions. 83 | You use `value()` method to get the values either the result of the closure passed or the values as they are. 84 | 85 | ```php 86 | use Conditional\Conditional; 87 | 88 | $value = Conditional::if(fn() => 'a' !== 1) // or conditional(fn() => 'a' !== 1) 89 | ->then(1) 90 | ->value(); // returns 2 (because a !== 1) 91 | 92 | //do something with $value 93 | ``` 94 | 95 | Finally, `then()` and `else()` methods accepts invokable class or objects. Lets see: 96 | 97 | ```php 98 | use Conditional\Conditional; 99 | 100 | class Invokable { 101 | 102 | public function __invoke() 103 | { 104 | return 'I was Invoked'; 105 | } 106 | } 107 | 108 | $invokableClass = new Invokable(); 109 | 110 | $value = Conditional::if(fn() => 'a' === 1) // or conditional(fn() => 1 + 1) 111 | ->then(1) 112 | ->else($invokableClass); //Value returns 'I was Invoked' 113 | 114 | // Do something with $value 115 | ``` 116 | 117 | You are also allowed to throw exception based on the condition like so: 118 | 119 | ```php 120 | \conditional('foo' === 'bar') 121 | 122 | ->then('foo === bar') 123 | 124 | ->else(new TestException('Foo is not the same as bar')); //this exception is thrown 125 | ``` 126 | 127 | ### Newly released 128 | 129 | `elseIf()` method of Conditional like so: 130 | 131 | ```php 132 | conditional(isset($data)) 133 | 134 | ->then(fn() => doThis()) 135 | 136 | ->elseIf(is_int(1)) 137 | 138 | ->then(fn() => doThat()) 139 | 140 | ->else(2); 141 | ``` 142 | 143 | `elseIf()` can be called multiple times on an instance: 144 | 145 | ```php 146 | $value = Conditional::if(false) 147 | 148 | ->then('a') 149 | 150 | ->elseIf('b' == 1) //first one 151 | 152 | ->then('b') 153 | 154 | ->elseIf('b' !== 2) //another 155 | 156 | ->then('2') 157 | 158 | ->else(1); 159 | 160 | // $value is '2' 161 | ``` 162 | 163 | ### Coming Soon 164 | 165 | `If()` and `elseIf()` statement accepting a default value when no condition is met and `else()` is not called like so: 166 | 167 | ```php 168 | Conditional::if(is_array('a'), 'ninja') //default value is ninja 169 | 170 | ->then(fn() => doThis()) 171 | 172 | ->elseIf(is_int("")) 173 | 174 | ->then(fn() => doThat()) 175 | 176 | ->value(); // 'ninja' is returned :scream: 177 | ``` 178 | 179 | Multiple conditional check like `a && b && c && d` or `a || b || c ||...` syntax 180 | 181 | - Help wanted for this 182 | 183 | ## Caveats (or Awareness) 184 | 185 | - 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: 186 | 187 | ```php 188 | // This is Wrong! 189 | 190 | Conditional::if(true) 191 | ->then(1) 192 | ->else(2) 193 | ->if('1'); // Don't do it except you intend to start a new and fresh if Conditional 194 | ``` 195 | > See: tests/ConditionalTest::testEveryIfCallCreatesNewFreshInstance test. On the last line of that test, the two conditionals are not the same. 196 | - Conditional relies on closures to return non closure values passed to then. 197 | > In the future release it will be optional for `then` and `else` method 198 | 199 | ## Contributions 200 | 201 | - More tests are needed 202 | - Issues have been opened 203 | - How about those static properties, any idea how to reduce the number of static properties used? 204 | - Performance optimization (?) 205 | 206 | ## Development 207 | 208 | For new feature, checkout with prefix `feat-#issueid` e.g `feature-#100-add-auto-deloy` 209 | 210 | - 211 | - Clone this repository 212 | - run `sh dockerizer.sh` or `bash dockerizer.sh` 213 | - execute into the docker environment with `docker-compose exec conditional sh` (`sh` can be another bash) 214 | - run tests with `vendor/bin/phpunit` 215 | 216 | ## Licence 217 | 218 | MIT (see LICENCE file) 219 | 220 | ## Additional Information 221 | 222 | Other related packages: 223 | 224 | - https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way] 225 | - https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way] 226 | - https://github.com/transprime-research/attempt [A smart PHP try...catch statement] 227 | - https://github.com/omitobi/carbonate [A smart Carbon + Collection package] 228 | - https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response] 229 | -------------------------------------------------------------------------------- /src/Conditional.php: -------------------------------------------------------------------------------- 1 | on($condition); 36 | } 37 | 38 | public function on($condition) 39 | { 40 | $this->setTruthy($condition); 41 | 42 | $this->conditionsExists = true; 43 | 44 | $this->ifCalled = true; 45 | 46 | return $this; 47 | } 48 | 49 | public function setTruthy($condition) 50 | { 51 | if (!$condition instanceof Closure) { 52 | $this->truthy = (bool)$condition; 53 | } else { 54 | $this->truthy = (bool)$condition(); 55 | } 56 | } 57 | 58 | public function else($action) 59 | { 60 | if (!$this->thenCalled) { 61 | throw new InvalidConditionOrderException( 62 | 'then() must be called before calling else()' 63 | ); 64 | } 65 | 66 | $this->toggleTruthy(); 67 | 68 | $this->conditionsExists = true; 69 | 70 | $this->elseCalled = true; 71 | 72 | return $this->then($action)->value(); 73 | } 74 | 75 | public function then($action): self 76 | { 77 | if (!$this->allowThen()) { 78 | throw new InvalidConditionOrderException( 79 | 'A condition must be called before calling then()' 80 | ); 81 | } 82 | 83 | if ($this->truthy) { 84 | if ($this->isExceptionClass($action)) { 85 | throw $action; 86 | } 87 | 88 | if (!$this->canBeCalled($action)) { 89 | $action = function () use ($action) { 90 | return $action; 91 | }; 92 | } 93 | 94 | $this->finalValue = $action(); 95 | 96 | $this->finalValueChanged = true; 97 | } 98 | 99 | $this->thenCalled = true; 100 | 101 | $this->conditionsExists = false; 102 | 103 | return $this; 104 | } 105 | 106 | 107 | public function elseIf($condition): self 108 | { 109 | if (!$this->allowElseIf()) { 110 | throw new InvalidConditionOrderException( 111 | 'At least then() condition must be called before calling elseIf' 112 | ); 113 | } 114 | 115 | $this->conditionsExists = true; 116 | 117 | if ($this->truthy) { 118 | $this->toggleTruthy(); 119 | 120 | return $this; 121 | } 122 | 123 | $this->setTruthy($condition); 124 | 125 | $this->elseIfCalled = true; 126 | 127 | return $this; 128 | } 129 | 130 | private function isExceptionClass($action): bool 131 | { 132 | return is_a($action, \Throwable::class); 133 | } 134 | 135 | public function value() 136 | { 137 | return $this->finalValue; 138 | } 139 | 140 | private function allowElseIf(): bool 141 | { 142 | return $this->thenCalled && 143 | !$this->conditionsExists && 144 | !$this->elseCalled; 145 | } 146 | 147 | private function allowThen(): bool 148 | { 149 | return $this->conditionsExists && ($this->ifCalled || $this->elseIfCalled); 150 | } 151 | 152 | protected function canBeCalled($value): bool 153 | { 154 | return ( 155 | (is_object($value) && method_exists($value, '__invoke')) || 156 | ($value instanceof Closure) 157 | ); 158 | } 159 | 160 | private function toggleTruthy(): void 161 | { 162 | $this->truthy = !$this->truthy; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidConditionOrderException.php: -------------------------------------------------------------------------------- 1 | assertEquals(Conditional::if(true), conditional(true)); 17 | } 18 | 19 | public function testThenCannotBeCalledBeforeIf() 20 | { 21 | $conditional = new Conditional(); 22 | 23 | $this->expectException(InvalidConditionOrderException::class); 24 | 25 | $this->expectExceptionMessage('A condition must be called before calling then()'); 26 | 27 | $conditional->then(1); 28 | } 29 | 30 | public function testElseCannotBeCalledBeforeThen() 31 | { 32 | $conditional = new Conditional(); 33 | 34 | $this->expectException(InvalidConditionOrderException::class); 35 | 36 | $this->expectExceptionMessage('then() must be called before calling else()'); 37 | 38 | $conditional->else(2); 39 | } 40 | 41 | public function testExecutionFollowsConditions() 42 | { 43 | $firstResponse = 1; 44 | $secondResponse = 2; 45 | 46 | Conditional::if($firstResponse === $secondResponse) 47 | ->then(function () use ($firstResponse, $secondResponse) { 48 | $this->assertEquals($firstResponse, $secondResponse); 49 | }) 50 | ->else(function () use ($firstResponse, $secondResponse) { 51 | $this->assertNotEquals($firstResponse, $secondResponse); 52 | }); 53 | } 54 | 55 | public function testThatValueIsReceived() 56 | { 57 | $firstResponse = 1; 58 | $secondResponse = 2; 59 | 60 | $value = rand(1, 2); 61 | 62 | $result = Conditional::if($value === $firstResponse) 63 | ->then($firstResponse) 64 | ->else($secondResponse); 65 | 66 | $this->assertEquals($value, $result); 67 | } 68 | 69 | public function testGetFunctionValue() 70 | { 71 | $result = Conditional::if(function () { 72 | return true; 73 | }) 74 | ->then(function () { 75 | return function () { 76 | return 'fn 1'; 77 | }; 78 | }) 79 | ->else(function () { 80 | return function () { 81 | return 'fn 2'; 82 | }; 83 | }); 84 | 85 | $this->assertInstanceOf(Closure::class, $result); 86 | } 87 | 88 | public function testInvokableClassValue() 89 | { 90 | $invokable = new class { 91 | 92 | public function __invoke($value = '') 93 | { 94 | return $value ?: 'Invocable'; 95 | } 96 | }; 97 | 98 | $result1 = Conditional::if($invokable() === 'Invocable') 99 | ->then($invokable) 100 | ->else($invokable(1)); 101 | 102 | $result2 = Conditional::if(strlen('abcd') === 4) 103 | ->then(new Invokable()) 104 | ->value(); 105 | 106 | $this->assertEquals(true, is_string($result1)); 107 | $this->assertEquals(true, is_int($result2)); 108 | } 109 | 110 | public function testEveryIfCallCreatesNewFreshInstance() 111 | { 112 | $conditional = new Conditional(); 113 | 114 | $instanceOne = $conditional->if(false); 115 | $instanceTwo = $conditional->if(false); 116 | 117 | $this->assertInstanceOf(Conditional::class, $instanceOne); 118 | $this->assertInstanceOf(Conditional::class, $instanceTwo); 119 | 120 | $this->assertNotSame($instanceOne, $instanceTwo); 121 | } 122 | 123 | public function testThenAndElseAcceptsException() 124 | { 125 | $this->expectException(TestException::class); 126 | $this->expectExceptionMessage('This is still wrong'); 127 | 128 | \conditional('foo' === 'bar') 129 | ->then(new TestException('This is wrong')) 130 | ->else(new TestException('This is still wrong')); 131 | } 132 | 133 | public function testElseAfterElseIfConditional() 134 | { 135 | $value = Conditional::if('1' === true) 136 | ->then(1) 137 | ->elseIf('1' === false) 138 | ->then(3) 139 | ->else(4); 140 | 141 | $this->assertEquals(4, $value); 142 | } 143 | 144 | public function testElseIfCannotBeCalledBeforeThen() 145 | { 146 | $condtional = new Conditional(); 147 | 148 | $this->expectException(InvalidConditionOrderException::class); 149 | 150 | $this->expectExceptionMessage('At least then() condition must be called before calling elseIf'); 151 | 152 | $condtional->elseIf(true); 153 | } 154 | 155 | public function testElseIfCannotBeCalledAfterElse() 156 | { 157 | $this->expectException(InvalidConditionOrderException::class); 158 | 159 | $this->expectExceptionMessage('At least then() condition must be called before calling elseIf'); 160 | 161 | Conditional::if('1' === true) 162 | ->elseIf('1' === false) 163 | ->then(3) 164 | ->elseIf(true) 165 | ->then('abc'); 166 | } 167 | 168 | public function testElseIfBuildsUpAnotherConditional() 169 | { 170 | $conditional = Conditional::if(false); 171 | 172 | $conditional2 = $conditional->then(1) 173 | ->elseIf(true); 174 | 175 | $this->assertEquals($conditional, $conditional2); 176 | $this->assertSame($conditional, $conditional2); 177 | 178 | $value = $conditional2->then(3) 179 | ->value(); 180 | 181 | $this->assertEquals(3, $value); 182 | } 183 | 184 | public function testElseIfCannotBeChainedWithElseIf() 185 | { 186 | $this->expectException(InvalidConditionOrderException::class); 187 | 188 | $this->expectExceptionMessage('At least then() condition must be called before calling elseIf'); 189 | 190 | Conditional::if('1' === true) 191 | ->then(1) 192 | ->elseIf('1' === false) 193 | ->elseIf(1 === '1') 194 | ->value(); 195 | } 196 | 197 | public function testElseIfCanBeCalledMultipleTimesInAnInstance() 198 | { 199 | $value = Conditional::if(false) 200 | ->then('a') 201 | ->elseIf('b' == 1) 202 | ->then('b') 203 | ->elseIf('b' !== 2) 204 | ->then('2') 205 | ->else(1); 206 | 207 | $this->assertEquals($value, 2); 208 | } 209 | 210 | public function testConditionalHelperFunctionAcceptsThenAndElseValues() 211 | { 212 | $this->assertSame('3', conditional(1 === '1', '2', '3')); 213 | 214 | $this->assertSame('2', conditional(1 == '1', '2')->value()); 215 | 216 | $this->assertSame( 217 | conditional(1 === 2, '2') 218 | ->elseIf(is_string('a')) 219 | ->then('a') 220 | ->value(), 221 | 'a' 222 | ); 223 | } 224 | 225 | public function testConditionalTwice() 226 | { 227 | $conditionally = function ($condition) { 228 | $conditional = Conditional::if($condition); 229 | 230 | Conditional::if(func_num_args() === 2) 231 | ->then('1') 232 | ->else('2'); 233 | 234 | return $conditional; 235 | }; 236 | 237 | $this->assertEquals( 238 | 'b', 239 | $conditionally(1 === 2) 240 | ->then('a') 241 | ->else('b') 242 | ); 243 | } 244 | 245 | // public function testIfCannotBeCalledAfterElseIf() 246 | // { 247 | // $this->expectException(InvalidConditionOrderException::class); 248 | // 249 | // $this->expectExceptionMessage('At least then() condition must be called before calling elseIf'); 250 | // 251 | // Conditional::if('1' === true) 252 | // ->then(1) 253 | // ->elseIf('1' === false) 254 | // ->if(1 === '1') 255 | // ->value(); 256 | // } 257 | 258 | } 259 | 260 | class Invokable 261 | { 262 | 263 | public function __invoke($value = '') 264 | { 265 | return $value ?: 1; 266 | } 267 | } 268 | 269 | class TestException extends \Exception 270 | { 271 | 272 | } --------------------------------------------------------------------------------