Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MessageTrait
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
18
100.00% covered (success)
100.00%
1 / 1
 resolveSize
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
9
 buildAlignedMessageLines
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
 calculatePad
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Console;
16
17/**
18 * Console message sizing/alignment trait, shared by Header and Alert
19 *
20 * @category   Pop
21 * @package    Pop\Console
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27trait MessageTrait
28{
29
30    /**
31     * Resolve an explicit/auto/null $size against the given wrap/width/margin
32     *
33     * @param  string          $text
34     * @param  int|string|null $size
35     * @param  ?int            $wrap
36     * @param  int             $width
37     * @param  ?int            $margin
38     * @param  int             $fallbackPadding
39     * @return int|string
40     */
41    protected function resolveSize(string $text, int|string|null $size, ?int $wrap, int $width, ?int $margin, int $fallbackPadding = 0): int|string
42    {
43        if ($size === null) {
44            if (!empty($wrap) && (strlen($text) > $wrap)) {
45                $size = $wrap;
46            } else if (!empty($width) && (strlen($text) > $width)) {
47                $size = $width - ((int)$margin * 2);
48            } else {
49                $size = strlen($text) + $fallbackPadding;
50            }
51        } else if ($size == 'auto') {
52            if (!empty($wrap)) {
53                $size = $wrap;
54            } else if (!empty($width)) {
55                $size = $width - ((int)$margin * 2);
56            }
57        }
58
59        return $size;
60    }
61
62    /**
63     * Word-wrap and align a message into padded lines of $size width
64     *
65     * @param  string $message
66     * @param  int    $size
67     * @param  string $align
68     * @param  int    $innerPad
69     * @return array
70     */
71    protected function buildAlignedMessageLines(string $message, int $size, string $align, int $innerPad): array
72    {
73        $innerSize    = $size - ($innerPad * 2);
74        $messageLines = [];
75        $lines        = (strlen($message) > $innerSize) ?
76            explode(PHP_EOL, wordwrap($message, $innerSize, PHP_EOL)) : [$message];
77
78        foreach ($lines as $line) {
79            $pad = $this->calculatePad($line, $size, $align);
80            if ($align == 'center') {
81                $messageLines[] = str_repeat(' ', $pad) . $line . str_repeat(' ', ($size - strlen($line) - $pad));
82            } else if ($align == 'left') {
83                $messageLines[] = str_repeat(' ', $innerPad) . $line . str_repeat(' ', ($size - strlen($line) - $pad - $innerPad));
84            } else if ($align == 'right') {
85                $messageLines[] = str_repeat(' ', ($size - strlen($line) - $innerPad)) . $line . str_repeat(' ', $innerPad);
86            }
87        }
88
89        return $messageLines;
90    }
91
92    /**
93     * Calculate string pad
94     *
95     * @param  string $string
96     * @param  int    $size
97     * @param  string $align
98     * @return int
99     */
100    protected function calculatePad(string $string, int $size, string $align = 'center'): int
101    {
102        $pad = 0;
103
104        if ($align == 'center') {
105            $pad = (int)round(($size - strlen($string)) / 2);
106        } else if ($align == 'right') {
107            $pad = $size - strlen($string);
108        }
109
110        return $pad;
111    }
112
113}