Completed
Pull Request — master (#55)
by Greg
02:19
created

WordWrapper::minimumWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations;
3
4
use Consolidation\OutputFormatters\Transformations\Wrap\ColumnWidths;
5
use Consolidation\OutputFormatters\Transformations\Wrap\DataCellWidths;
6
use Symfony\Component\Console\Helper\TableStyle;
7
8
class WordWrapper
9
{
10
    protected $width;
11
    protected $minimumWidths;
12
13
    // For now, hardcode these to match what the Symfony Table helper does.
14
    // Note that these might actually need to be adjusted depending on the
15
    // table style.
16
    protected $extraPaddingAtBeginningOfLine = 0;
17
    protected $extraPaddingAtEndOfLine = 0;
18
    protected $paddingInEachCell = 3;
19
20
    public function __construct($width)
21
    {
22
        $this->width = $width;
23
        $this->minimumWidths = new DataCellWidths();
24
    }
25
26
    /**
27
     * Calculate our padding widths from the specified table style.
28
     * @param TableStyle $style
29
     */
30
    public function setPaddingFromStyle(TableStyle $style)
31
    {
32
        $verticalBorderLen = strlen(sprintf($style->getBorderFormat(), $style->getVerticalBorderChar()));
33
        $paddingLen = strlen($style->getPaddingChar());
34
35
        $this->extraPaddingAtBeginningOfLine = 0;
36
        $this->extraPaddingAtEndOfLine = $verticalBorderLen;
37
        $this->paddingInEachCell = $verticalBorderLen + $paddingLen + 1;
38
    }
39
40
    /**
41
     * If columns have minimum widths, then set them here.
42
     * @param array $minimumWidths
43
     */
44
    public function setMinimumWidths($minimumWidths)
45
    {
46
        $this->minimumWidths = new DataCellWidths($minimumWidths);
47
    }
48
49
    /**
50
     * Set the minimum width of just one column
51
     */
52
    public function minimumWidth($colkey, $width)
53
    {
54
        $this->minimumWidths->setWidth($colkey, $width);
55
    }
56
57
    /**
58
     * Wrap the cells in each part of the provided data table
59
     * @param array $rows
60
     * @return array
61
     */
62
    public function wrap($rows, $widths = [])
0 ignored issues
show
Unused Code introduced by
The parameter $widths is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        // If the width was not set, then disable wordwrap.
65
        if (!$this->width) {
66
            return $rows;
67
        }
68
69
        $dataCellWidths = new DataCellWidths();
70
        $dataCellWidths->calculateLongestCell($rows);
71
72
        $availableWidth = $this->width - $dataCellWidths->paddingSpace($this->paddingInEachCell, $this->extraPaddingAtEndOfLine, $this->extraPaddingAtBeginningOfLine);
73
74
        $this->minimumWidths->adjustMinimumWidths($availableWidth, $dataCellWidths);
75
76
        $columnWidths = new ColumnWidths();
77
        $auto_widths = $columnWidths->calculate($availableWidth, $dataCellWidths, $this->minimumWidths);
78
79
        // Do wordwrap on all cells.
80
        $newrows = array();
81
        foreach ($rows as $rowkey => $row) {
82
            foreach ($row as $colkey => $cell) {
83
                $newrows[$rowkey][$colkey] = $this->wrapCell($cell, $auto_widths->width($colkey));
84
            }
85
        }
86
87
        return $newrows;
88
    }
89
90
    /**
91
     * Wrap one cell.  Guard against modifying non-strings and
92
     * then call through to wordwrap().
93
     *
94
     * @param mixed $cell
95
     * @param string $cellWidth
96
     * @return mixed
97
     */
98
    protected function wrapCell($cell, $cellWidth)
99
    {
100
        if (!is_string($cell)) {
101
            return $cell;
102
        }
103
        return wordwrap($cell, $cellWidth, "\n", true);
104
    }
105
}
106