o2system /
kernel
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Kernel\Cli\Writers; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Kernel\Cli\Writers\Interfaces\ContextualClassInterface; |
||
| 19 | use O2System\Kernel\Cli\Writers\Traits\ContextualColorClassSetterTrait; |
||
| 20 | use O2System\Kernel\Cli\Writers\Traits\IndentSetterTrait; |
||
| 21 | use O2System\Kernel\Cli\Writers\Traits\NewLinesSetterTrait; |
||
| 22 | use O2System\Kernel\Cli\Writers\Traits\QuoteSetterTrait; |
||
| 23 | use O2System\Kernel\Cli\Writers\Traits\SpaceSetterTrait; |
||
| 24 | use O2System\Kernel\Cli\Writers\Traits\StringSetterTrait; |
||
| 25 | use O2System\Spl\Traits\OptionsSetterTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Class Format |
||
| 29 | * |
||
| 30 | * @package O2System\Kernel\Cli\Writers |
||
| 31 | */ |
||
| 32 | class Format implements ContextualClassInterface |
||
| 33 | { |
||
| 34 | use OptionsSetterTrait; |
||
| 35 | use ContextualColorClassSetterTrait; |
||
| 36 | use IndentSetterTrait; |
||
| 37 | use SpaceSetterTrait; |
||
| 38 | use NewLinesSetterTrait; |
||
| 39 | use QuoteSetterTrait; |
||
| 40 | use StringSetterTrait; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Format::$contextualClassColorMap |
||
| 44 | * |
||
| 45 | * Contextual class color mapping. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $contextualClassColorMap = [ |
||
| 50 | 'default' => 'white', |
||
| 51 | 'primary' => 'blue', |
||
| 52 | 'success' => 'green', |
||
| 53 | 'info' => 'cyan', |
||
| 54 | 'warning' => 'yellow', |
||
| 55 | 'danger' => 'red', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | // ------------------------------------------------------------------------ |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Format::__construct |
||
| 62 | * |
||
| 63 | * @param array $options |
||
| 64 | */ |
||
| 65 | public function __construct(array $options = []) |
||
| 66 | { |
||
| 67 | $this->setOptions($options); |
||
| 68 | } |
||
| 69 | |||
| 70 | // ------------------------------------------------------------------------ |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Format::__toString |
||
| 74 | * |
||
| 75 | * Implementation __toString magic method so that when the class is converted to a string |
||
| 76 | * automatically performs the rendering process. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function __toString() |
||
| 81 | { |
||
| 82 | return $this->apply(); |
||
| 83 | } |
||
| 84 | |||
| 85 | // ------------------------------------------------------------------------ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Format::apply |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function apply() |
||
| 93 | { |
||
| 94 | if (empty($this->string)) { |
||
| 95 | return ''; |
||
| 96 | } |
||
| 97 | |||
| 98 | $lines = explode("\n", $this->string); |
||
| 99 | |||
| 100 | foreach ($lines as &$line) { |
||
| 101 | $line = ((string)$this->quote) |
||
| 102 | . $line; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($this->color instanceof Color) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 106 | $output = str_repeat(' ', $this->indent) . $this->color->paint(implode("\n", |
||
| 107 | $lines)) . str_repeat(' ', $this->space); |
||
| 108 | } else { |
||
| 109 | $output = str_repeat(' ', $this->indent) . implode("\n", $lines) . str_repeat(' ', $this->space); |
||
| 110 | } |
||
| 111 | |||
| 112 | return str_repeat(PHP_EOL, $this->newLinesBefore) . $output . str_repeat(PHP_EOL, $this->newLinesAfter); |
||
| 113 | } |
||
| 114 | } |