├── CHANGELOG.md ├── .editorconfig ├── src └── OrElse.php ├── composer.json ├── LICENSE.md └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `or-else` will be documented in this file 4 | 5 | ## 1.0.0 - 2016-06-17 6 | 7 | ### Added 8 | - Everything, initial release 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /src/OrElse.php: -------------------------------------------------------------------------------- 1 | 0; 12 | 13 | if ($isOrElse) { 14 | $orElse = array_pop($args); 15 | $methodName = substr($name, 0, -6); 16 | 17 | $result = call_user_func_array([$this, $methodName], $args); 18 | 19 | if (is_null($result) || $result === false) { 20 | return is_callable($orElse) ? $orElse() : $orElse; 21 | } 22 | 23 | return $result; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/or-else", 3 | "description": "A trait to add orElse functions to a class", 4 | "keywords": [ 5 | "spatie", 6 | "or-else", 7 | "optional" 8 | ], 9 | "homepage": "https://github.com/spatie/or-else", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": ":Freek Van der Herten", 14 | "email": "freek@spatie.be", 15 | "homepage": "https://spatie.be", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php" : ">=5.4.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit" : "^4.8 || ^5.7 || ^6.5", 24 | "scrutinizer/ocular": "~1.1" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Spatie\\OrElse\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "Spatie\\OrElse\\Test\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "phpunit" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015 :Freek Van der Herten 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://supportukrainenow.org) 3 | 4 | # or-else 5 | 6 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/or-else.svg?style=flat-square)](https://packagist.org/packages/spatie/or-else) 7 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 8 | [![Build Status](https://img.shields.io/travis/spatie/or-else/master.svg?style=flat-square)](https://travis-ci.org/spatie/or-else) 9 | [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/or-else.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/or-else) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/or-else.svg?style=flat-square)](https://packagist.org/packages/spatie/or-else) 11 | 12 | This package adds an `orElse`-trait to your project. 13 | 14 | Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 15 | 16 | ## Support us 17 | 18 | [](https://spatie.be/github-ad-click/or-else) 19 | 20 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 21 | 22 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 23 | 24 | ## Install 25 | 26 | You can install the package via composer: 27 | ``` bash 28 | $ composer require spatie/or-else 29 | ``` 30 | 31 | ## Postcardware 32 | 33 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. 34 | 35 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 36 | 37 | The best postcards will get published on the open source page on our website. 38 | 39 | ## Usage 40 | 41 | When implementing the `OrElse`-trait to a class, all methods of the class will have a `OrElse`-variant. 42 | That variant has an extra parameter that will be returned if the original function returns `null` or `false`. 43 | 44 | Consider this simple class that implements the `orElse`-trait. 45 | 46 | ```php 47 | use Spatie\OrElse\OrElse; 48 | 49 | class TestClass { 50 | 51 | use OrElse; 52 | 53 | /** 54 | * This function will return the given argument. 55 | * 56 | * @return string 57 | */ 58 | public function willReturn($value) 59 | { 60 | return $value; 61 | } 62 | 63 | } 64 | ``` 65 | 66 | The trait dynamically adds a `willReturnOrElse`-method. 67 | 68 | ```php 69 | $testClass = new TestClass; 70 | $testClass->willReturn('value'); // returns 'value'; 71 | $testClass->willReturnOrElse('value', 'otherValue'); // returns 'value'; 72 | $testClass->willReturnOrElse(null, 'otherValue'); // returns 'otherValue'; 73 | $testClass->willReturnOrElse(false, 'otherValue'); // returns 'otherValue'; 74 | $testClass->willReturnOrElse(false, function() { return 'closureValue'; }); // returns 'closureValue'; 75 | ``` 76 | 77 | ## Change log 78 | 79 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 80 | 81 | ## Testing 82 | 83 | ``` bash 84 | $ composer test 85 | ``` 86 | 87 | ## Contributing 88 | 89 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 90 | 91 | ## Security 92 | 93 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 94 | 95 | ## Credits 96 | 97 | - [Freek Van der Herten](https://murze.be) 98 | - [Edd Mann](https://twitter.com/edd_mann) 99 | - [All Contributors](../../contributors) 100 | 101 | ## About Spatie 102 | Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 103 | 104 | ## License 105 | 106 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 107 | --------------------------------------------------------------------------------