summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/web/src/Table/NameValueTable.php
blob: 7227de743e04e4e6563c63bade3d9a67821cca1d (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
<?php

namespace gipfl\Web\Table;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Table;

class NameValueTable extends Table
{
    protected $defaultAttributes = ['class' => 'gipfl-name-value-table'];

    public static function create($pairs = [])
    {
        $self = new static;
        $self->addNameValuePairs($pairs);

        return $self;
    }

    public function createNameValueRow($name, $value)
    {
        return $this::tr([$this::th($name), $this::wantTd($value)]);
    }

    public function addNameValueRow($name, $value)
    {
        return $this->add($this->createNameValueRow($name, $value));
    }

    public function addNameValuePairs($pairs)
    {
        foreach ($pairs as $name => $value) {
            $this->addNameValueRow($name, $value);
        }

        return $this;
    }

    protected function wantTd($value)
    {
        if ($value instanceof BaseHtmlElement && $value->getTag() === 'td') {
            return $value;
        } else {
            return $this::td($value);
        }
    }
}