Element   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 78
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toString() 0 12 2
A __toString() 0 3 1
A makeAttributes() 0 12 3
B makeChildren() 0 14 5
1
<?php
2
namespace rtens\domin\delivery\web;
3
4
class Element {
5
6
    private static $EMPTY_ELEMENTS = [
7
        'link',
8
        'track',
9
        'param',
10
        'area',
11
        'command',
12
        'col',
13
        'base',
14
        'meta',
15
        'hr',
16
        'source',
17
        'img',
18
        'keygen',
19
        'br',
20
        'wbr',
21
        'colgroup',
22
        'input',
23
    ];
24
25
    private $name;
26
27
    private $attributes;
28
29
    private $children;
30
31
    public function __construct($name, $attributes = [], $children = []) {
32
        $this->name = $name;
33
        $this->attributes = $attributes;
34
        $this->children = $children;
35
    }
36
37
    private function toString() {
38
        $attributes = $this->makeAttributes();
39
40
        if (in_array($this->name, self::$EMPTY_ELEMENTS)) {
41
            return "<{$this->name}$attributes/>";
42
        }
43
44
        return
45
            "<{$this->name}$attributes>" .
46
            $this->makeChildren() .
47
            "</{$this->name}>";
48
    }
49
50
    public function __toString() {
51
        return $this->toString();
52
    }
53
54
    private function makeAttributes() {
55
        $attributes = [];
56
        foreach ($this->attributes as $key => $value) {
57
            $attributes[] = $key . '="' . htmlentities($value) . '"';
58
        }
59
60
        if (empty($attributes)) {
61
            return '';
62
        }
63
64
        return ' ' . implode(' ', $attributes);
65
    }
66
67
    private function makeChildren() {
68
        $children = [];
69
        foreach ($this->children as $child) {
70
            $children[] = (string)$child;
71
        }
72
73
        if (empty($children)) {
74
            return '';
75
        } else if (count($children) == 1 && strpos($children[0], "\n") === false) {
76
            return $children[0];
77
        } else {
78
            return "\n" . implode("\n", $children) . "\n";
79
        }
80
    }
81
}