Passed
Push — master ( e8ba28...a594cd )
by Sebastian
03:48
created

Traits_Classable::getClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@see AppUtils\Traits_Classable} trait,
4
 * and the matching interface.
5
 *
6
 * @package Application Utils
7
 * @subpackage Traits
8
 * @see Traits_Classable
9
 * @see Interface_Classable
10
 */
11
12
namespace AppUtils;
13
14
/**
15
 * Trait for handling HTML classes.
16
 *
17
 * NOTE: To add this to a class, it must use the trait,
18
 * but also implement the interface.
19
 *
20
 * @package Application Utils
21
 * @subpackage Traits
22
 * @author Sebastian Mordziol <[email protected]>
23
 *
24
 * @see Interface_Classable
25
 */
26
trait Traits_Classable
27
{
28
   /**
29
    * @var string[]
30
    */
31
    protected $classes = array();
32
    
33
    public function addClass(string $name)
34
    {
35
        if(!in_array($name, $this->classes)) {
36
            $this->classes[] = $name;
37
        }
38
        
39
        return $this;
40
    }
41
    
42
    public function addClasses(array $names)
43
    {
44
        foreach($names as $name) {
45
            $this->addClass($name);
46
        }
47
        
48
        return $this;
49
    }
50
    
51
    public function hasClass(string $name) : bool
52
    {
53
        return in_array($name, $this->classes);
54
    }
55
    
56
    public function removeClass(string $name)
57
    {
58
        $idx = array_search($name, $this->classes);
59
        
60
        if($idx !== false) {
61
            unset($this->classes[$idx]);
62
            sort($this->classes);
63
        }
64
        
65
        return $this;
66
    }
67
    
68
   /**
69
    * Retrieves a list of all classes, if any.
70
    * 
71
    * @return string[]
72
    */
73
    public function getClasses() : array
74
    {
75
        return $this->classes;
76
    }
77
    
78
   /**
79
    * Renders the class names list as space-separated string for use in an HTML tag.
80
    * 
81
    * @return string
82
    */
83
    public function classesToString() : string
84
    {
85
        return implode(' ', $this->classes);
86
    }
87
    
88
   /**
89
    * Renders the "class" attribute string for inserting into an HTML tag.
90
    * @return string
91
    */
92
    public function classesToAttribute() : string
93
    {
94
        if(!empty($this->classes))
95
        {
96
            return sprintf(
97
                ' class="%s" ',
98
                $this->classesToString()
99
            );
100
        }
101
        
102
        return '';
103
    }
104
}
105
106
/**
107
 * Interface for classes that use the classable trait.
108
 * The trait itself fulfills most of the interface, but
109
 * it is used to guarantee internal type checks will work,
110
 * as well as ensure the abstract methods are implemented.
111
 *
112
 * @package Application Utils
113
 * @subpackage Traits
114
 * @author Sebastian Mordziol <[email protected]>
115
 *
116
 * @see Traits_Classable
117
 */
118
interface Interface_Classable
119
{
120
   /**
121
    * @param string $name
122
    * @return $this
123
    */
124
    public function addClass(string $name);
125
126
   /**
127
    * @param array $names
128
    * @return $this
129
    */
130
    public function addClasses(array $names);
131
    
132
   /**
133
    * @param string $name
134
    * @return bool
135
    */
136
    public function hasClass(string $name) : bool;
137
    
138
   /**
139
    * @param string $name
140
    * @return $this
141
    */
142
    public function removeClass(string $name);
143
    
144
   /**
145
    * @return array
146
    */
147
    public function getClasses() : array;
148
    
149
   /**
150
    * @return string
151
    */
152
    public function classesToString() : string;
153
    
154
   /**
155
    * @return string
156
    */
157
    public function classesToAttribute() : string;
158
}
159