summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/Table.php
blob: 28a67387a15b61e20fda857e22ee93912ab77d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

namespace ipl\Html;

use RuntimeException;
use stdClass;

class Table extends BaseHtmlElement
{
    protected $contentSeparator = "\n";

    /** @var string */
    protected $tag = 'table';

    /** @var HtmlElement */
    private $caption;

    /** @var HtmlElement */
    private $header;

    /** @var HtmlElement */
    private $body;

    /** @var HtmlElement */
    private $footer;

    public function addHtml(ValidHtml ...$content)
    {
        foreach ($content as $html) {
            if ($html instanceof BaseHtmlElement) {
                switch ($html->getTag()) {
                    case 'tr':
                        $this->getBody()->addHtml($html);

                        break;
                    case 'thead':
                        parent::addHtml($html);
                        $this->header = $html;

                        break;
                    case 'tbody':
                        parent::addHtml($html);
                        $this->body = $html;

                        break;
                    case 'tfoot':
                        parent::addHtml($html);
                        $this->footer = $html;

                        break;
                    case 'caption':
                        if ($this->caption === null) {
                            $this->prependHtml($html);
                            $this->caption = $html;
                        } else {
                            throw new RuntimeException(
                                'Tables allow only one <caption> tag'
                            );
                        }

                        break;
                    default:
                        $this->getBody()->addHtml(static::row([$html]));
                }
            } else {
                $this->getBody()->addHtml(static::row([$html]));
            }
        }

        return $this;
    }

    /**
     * @param mixed $content
     * @return $this
     */
    public function add($content)
    {
        if ($content instanceof stdClass) {
            $this->getBody()->addHtml(static::row((array) $content));
        } elseif (is_iterable($content)) {
            $this->getBody()->addHtml(static::row($content));
        } elseif ($content instanceof ValidHtml) {
            $this->addHtml($content);
        } else {
            $this->getBody()->addHtml(static::row([$content]));
        }

        return $this;
    }

    /**
     * Set the table title
     *
     * Will be rendered as a "caption" HTML element
     *
     * @param mixed $caption
     * @return $this
     */
    public function setCaption($caption)
    {
        if ($caption instanceof BaseHtmlElement && $caption->getTag() === 'caption') {
            $this->caption = $caption;
            $this->prependHtml($caption);
        } elseif ($this->caption === null) {
            $this->caption = new HtmlElement('caption', null, ...Html::wantHtmlList($caption));
            $this->prependHtml($this->caption);
        } else {
            $this->caption->setContent($caption);
        }

        return $this;
    }

    /**
     * Static helper creating a tr element
     *
     * @param Attributes|array $attributes
     * @param Html|array|string $content
     * @return HtmlElement
     */
    public static function tr($content = null, $attributes = null)
    {
        return Html::tag('tr', $attributes, $content);
    }

    /**
     * Static helper creating a th element
     *
     * @param Attributes|array $attributes
     * @param Html|array|string $content
     * @return HtmlElement
     */
    public static function th($content = null, $attributes = null)
    {
        return Html::tag('th', $attributes, $content);
    }

    /**
     * Static helper creating a td element
     *
     * @param Attributes|array $attributes
     * @param Html|array|string $content
     * @return HtmlElement
     */
    public static function td($content = null, $attributes = null)
    {
        return Html::tag('td', $attributes, $content);
    }

    /**
     * @param $row
     * @param null $attributes
     * @param string $tag
     * @return HtmlElement
     */
    public static function row($row, $attributes = null, $tag = 'td')
    {
        $tr = static::tr();
        foreach ((array) $row as $value) {
            $tr->addHtml(Html::tag($tag, null, $value));
        }

        if ($attributes !== null) {
            $tr->setAttributes($attributes);
        }

        return $tr;
    }

    /**
     * @return HtmlElement
     */
    public function getBody()
    {
        if ($this->body === null) {
            $this->addHtml(Html::tag('tbody')->setSeparator("\n"));
        }

        return $this->body;
    }

    /**
     * @return HtmlElement
     */
    public function getHeader()
    {
        if ($this->header === null) {
            $this->addHtml(Html::tag('thead')->setSeparator("\n"));
        }

        return $this->header;
    }

    /**
     * @return HtmlElement
     */
    public function getFooter()
    {
        if ($this->footer === null) {
            $this->addHtml(Html::tag('tfoot')->setSeparator("\n"));
        }

        return $this->footer;
    }

    /**
     * @return HtmlElement
     */
    public function nextBody()
    {
        $this->body = null;

        return $this->getBody();
    }

    /**
     * @return HtmlElement
     */
    public function nextHeader()
    {
        $this->header = null;

        return $this->getHeader();
    }
}