|
1 | | -# macroable |
2 | | -A trait to dynamically add methods to a class. |
| 1 | +<h1 align="center">Macroable Component</h1> |
| 2 | +<p align="center"> |
| 3 | +Macroable Component is a trait that, gives you the ability in effect to add new methods to a class at runtime. |
| 4 | +</p> |
| 5 | +<p align="center"> |
| 6 | +<a href="https://github.com/atomastic/macroable/releases"><img alt="Version" src="https://img.shields.io/github/release/atomastic/macroable.svg?label=version&color=green"></a> <a href="https://github.com/atomastic/macroable"><img src="https://img.shields.io/badge/license-MIT-blue.svg?color=green" alt="License"></a> <a href="https://packagist.org/packages/atomastic/macroable"><img src="https://poser.pugx.org/atomastic/macroable/downloads" alt="Total downloads"></a> <img src="https://github.com/atomastic/macroable/workflows/Static%20Analysis/badge.svg?branch=dev"> <img src="https://github.com/atomastic/macroable/workflows/Tests/badge.svg"> |
| 7 | + <a href="https://app.codacy.com/gh/atomastic/macroable?utm_source=github.com&utm_medium=referral&utm_content=atomastic/macroable&utm_campaign=Badge_Grade_Dashboard"><img src="https://api.codacy.com/project/badge/Grade/72b4dc84c20145e1b77dc0004a3c8e3d"></a> <a href="https://codeclimate.com/github/atomastic/macroable/maintainability"><img src="https://api.codeclimate.com/v1/badges/a4c673a4640a3863a9a4/maintainability" /></a> |
| 8 | +</p> |
| 9 | + |
| 10 | +<br> |
| 11 | + |
| 12 | +* [Installation](#installation) |
| 13 | +* [Usage](#usage) |
| 14 | +* [Methods](#methods) |
| 15 | +* [Tests](#tests) |
| 16 | +* [License](#license) |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +#### With [Composer](https://getcomposer.org) |
| 21 | + |
| 22 | +``` |
| 23 | +composer require atomastic/macroable |
| 24 | +``` |
| 25 | + |
| 26 | +### Usage |
| 27 | + |
| 28 | +```php |
| 29 | +use Atomastic\Macroable\Macroable; |
| 30 | +``` |
| 31 | + |
| 32 | +### Methods |
| 33 | + |
| 34 | +| Method | Description | |
| 35 | +|---|---| |
| 36 | +| <a href="#macroable_macro">`macro()`</a> | Register a custom macro. | |
| 37 | +| <a href="#macroable_mixin">`mixin()`</a> | Mix another object into the class. | |
| 38 | +| <a href="#macroable_hasMacro">`hasMacro()`</a> | Checks if macro is registered. | |
| 39 | + |
| 40 | +#### Methods Details |
| 41 | + |
| 42 | +##### <a name="macroable_macro"></a> Method: `macro()` |
| 43 | + |
| 44 | +```php |
| 45 | +/** |
| 46 | + * Register a custom macro. |
| 47 | + * |
| 48 | + * @param string $name Name. |
| 49 | + * @param object|callable $macro Macro. |
| 50 | + * @return void |
| 51 | + */ |
| 52 | +public static function macro(string $name, $macro): void |
| 53 | +``` |
| 54 | + |
| 55 | +##### Example |
| 56 | + |
| 57 | +```php |
| 58 | +$macroableClass = new class() { |
| 59 | + use Macroable; |
| 60 | +}; |
| 61 | + |
| 62 | +$macroableClass::macro('concatenate', function(... $strings) { |
| 63 | + return implode('-', $strings); |
| 64 | +}); |
| 65 | + |
| 66 | +$macroableClass::macro('message', function($name) { |
| 67 | + return 'Hello ' . $name; |
| 68 | +}); |
| 69 | + |
| 70 | +echo $macroableClass->concatenate('one', 'two', 'three'); |
| 71 | +echo $macroableClass->message('Jack'); |
| 72 | +``` |
| 73 | + |
| 74 | +##### The above example will output: |
| 75 | + |
| 76 | +``` |
| 77 | +one-two-three |
| 78 | +Hello Jack |
| 79 | +``` |
| 80 | + |
| 81 | +##### <a name="macroable_mixin"></a> Method: `mixin()` |
| 82 | + |
| 83 | +```php |
| 84 | +/** |
| 85 | + * Mix another object into the class. |
| 86 | + * |
| 87 | + * @param object $mixin Mixin. |
| 88 | + * @param bool $replace Replace. |
| 89 | + * @return void |
| 90 | + * |
| 91 | + * @throws ReflectionException |
| 92 | + */ |
| 93 | +public static function mixin($mixin, bool $replace = true): void |
| 94 | +``` |
| 95 | + |
| 96 | +##### Example |
| 97 | + |
| 98 | +```php |
| 99 | +$mixinClass = new class() { |
| 100 | + public function mixinMethod() |
| 101 | + { |
| 102 | + return function() { |
| 103 | + return 'mixinMethod'; |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + public function anotherMixinMethod() |
| 108 | + { |
| 109 | + return function() { |
| 110 | + return 'anotherMixinMethod'; |
| 111 | + }; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +$macroableClass->mixin($mixin); |
| 116 | + |
| 117 | +$macroableClass->mixinMethod(); |
| 118 | +$macroableClass->anotherMixinMethod(); |
| 119 | +``` |
| 120 | + |
| 121 | +##### The above example will output: |
| 122 | + |
| 123 | +``` |
| 124 | +mixinMethod |
| 125 | +anotherMixinMethod |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +##### <a name="macroable_hasMacro"></a> Method: `hasMacro()` |
| 130 | + |
| 131 | +```php |
| 132 | +/** |
| 133 | + * Checks if macro is registered. |
| 134 | + * |
| 135 | + * @param string $name Name |
| 136 | + * @return bool |
| 137 | + */ |
| 138 | +public static function hasMacro(string $name): bool |
| 139 | +``` |
| 140 | + |
| 141 | +##### Example |
| 142 | + |
| 143 | +```php |
| 144 | +$macroableClass = new class() { |
| 145 | + use Macroable; |
| 146 | +}; |
| 147 | + |
| 148 | +$macroableClass::macro('message', function($name) { |
| 149 | + return 'Hello ' . $name; |
| 150 | +}); |
| 151 | + |
| 152 | +if ($macroableClass::hasMacro('message')) { |
| 153 | + // do something... |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### Tests |
| 158 | + |
| 159 | +Run tests |
| 160 | + |
| 161 | +``` |
| 162 | +./vendor/bin/pest |
| 163 | +``` |
| 164 | + |
| 165 | +### License |
| 166 | +[The MIT License (MIT)](https://github.com/atomastic/macroable/blob/master/LICENSE) |
| 167 | +Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum) |
0 commit comments