ObjectTable::setHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\web\renderers\tables\types;
3
4
use rtens\domin\delivery\web\renderers\tables\Table;
5
use rtens\domin\reflection\types\TypeFactory;
6
use watoki\reflect\Property;
7
use watoki\reflect\PropertyReader;
8
9
class ObjectTable implements Table {
10
11
    /** @var object[] */
12
    private $objects;
13
14
    /** @var Property[] */
15
    private $properties;
16
17
    /** @var string[] */
18
    private $headers = [];
19
20
    /** @var callable[] */
21
    private $filters = [];
22
23
    public function __construct(array $objects, TypeFactory $types) {
24
        $this->objects = $objects;
25
        $this->properties = [];
26
27
        if ($objects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $objects of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
28
            $reader = new PropertyReader($types, get_class($objects[0]));
29
            $this->properties = $reader->readInterface($objects[0])->filter(function (Property $property) {
30
                return $property->canGet();
31
            });
32
        }
33
    }
34
35
    /**
36
     * @return string[] Header captions
37
     */
38
    public function getHeaders() {
39
        $headers = [];
40
        foreach ($this->properties as $property) {
41
            if (array_key_exists($property->name(), $this->headers)) {
42
                $headers[] = $this->headers[$property->name()];
43
            } else {
44
                $headers[] = ucfirst($property->name());
45
            }
46
        }
47
        return $headers;
48
    }
49
50
    /**
51
     * @return object[]
52
     */
53
    public function getItems() {
54
        return $this->objects;
55
    }
56
57
    /**
58
     * @param object $object
59
     * @return mixed[]
60
     */
61
    public function getCells($object) {
62
        $row = [];
63
        foreach ($this->properties as $property) {
64
            $value = $property->get($object);
65
            if (array_key_exists($property->name(), $this->filters)) {
66
                $value = call_user_func($this->filters[$property->name()], $value);
67
            }
68
            $row[] = $value;
69
        }
70
        return $row;
71
    }
72
73
    public function selectProperties($names) {
74
        $selected = [];
75
        foreach ($names as $name) {
76
            if (isset($this->properties[$name])) {
77
                $selected[$name] = $this->properties[$name];
78
            }
79
        }
80
        $this->properties = $selected;
81
        return $this;
82
    }
83
84
    public function setHeader($propertyName, $header) {
85
        $this->headers[$propertyName] = $header;
86
        return $this;
87
    }
88
89
    public function setFilter($propertyName, callable $filter) {
90
        $this->filters[$propertyName] = $filter;
91
        return $this;
92
    }
93
}