Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Header
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
6 / 6
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 line
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 header
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
 headerLeft
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 headerCenter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 headerRight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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 header class
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 */
27class Header
28{
29
30    use MessageTrait;
31
32    /**
33     * Instantiate the header object
34     *
35     * @param string $indent
36     * @param ?int   $wrap
37     * @param int    $width
38     * @param ?int   $margin
39     */
40    public function __construct(
41        protected string $indent = '', protected ?int $wrap = null, protected int $width = 0, protected ?int $margin = null
42    ) { }
43
44    /**
45     * Build a horizontal line rule
46     *
47     * @param  string $char
48     * @param  ?int   $size
49     * @param  bool   $newline
50     * @return string
51     */
52    public function line(string $char = '-', ?int $size = null, bool $newline = true): string
53    {
54        $line = '';
55
56        if ($size === null) {
57            if (!empty($this->wrap)) {
58                $size = $this->wrap;
59            } else if (!empty($this->width)) {
60                $size = $this->width - ((int)$this->margin * 2);
61            }
62        }
63
64        $line .= $this->indent . str_repeat($char, $size);
65
66        if ($newline) {
67            $line .= PHP_EOL;
68        }
69
70        return $line;
71    }
72
73    /**
74     * Build a header
75     *
76     * @param  string          $string
77     * @param  string          $char
78     * @param  int|string|null $size
79     * @param  string          $align
80     * @param  bool            $newline
81     * @return string
82     */
83    public function header(
84        string $string, string $char = '-', int|string|null $size = null, string $align = 'left', bool $newline = true
85    ): string
86    {
87        $header = '';
88        $size   = $this->resolveSize($string, $size, $this->wrap, $this->width, $this->margin);
89
90        if (strlen($string) > $size) {
91            $lines = explode(PHP_EOL, wordwrap($string, $size, PHP_EOL));
92            foreach ($lines as $line) {
93                if (($align != 'left') && (strlen($line) < $size)) {
94                    $line = str_repeat(' ', $this->calculatePad($line, $size, $align)) . $line;
95                }
96                $header .= $this->indent . $line . PHP_EOL;
97            }
98        } else {
99            if (($align != 'left') && (strlen($string) < $size)) {
100                $string = str_repeat(' ', $this->calculatePad($string, $size, $align)) . $string;
101            }
102            $header = $this->indent . $string . PHP_EOL;
103        }
104
105        $header .= $this->line($char, $size, $newline);
106
107        return $header;
108    }
109
110    /**
111     * Build a left-aligned header
112     *
113     * @param  string          $string
114     * @param  string          $char
115     * @param  int|string|null $size
116     * @param  bool            $newline
117     * @return string
118     */
119    public function headerLeft(string $string, string $char = '-', int|string|null $size = 'auto', bool $newline = true): string
120    {
121        return $this->header($string, $char, $size, 'left', $newline);
122    }
123
124    /**
125     * Build a center-aligned header
126     *
127     * @param  string          $string
128     * @param  string          $char
129     * @param  int|string|null $size
130     * @param  bool            $newline
131     * @return string
132     */
133    public function headerCenter(string $string, string $char = '-', int|string|null $size = 'auto', bool $newline = true): string
134    {
135        return $this->header($string, $char, $size, 'center', $newline);
136    }
137
138    /**
139     * Build a right-aligned header
140     *
141     * @param  string          $string
142     * @param  string          $char
143     * @param  int|string|null $size
144     * @param  bool            $newline
145     * @return string
146     */
147    public function headerRight(string $string, string $char = '-', int|string|null $size = 'auto', bool $newline = true): string
148    {
149        return $this->header($string, $char, $size, 'right', $newline);
150    }
151
152}