Passed
Push — master ( 2d43fd...f4c9de )
by Sebastian
02:54
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
    protected $classes = array();
29
    
30
    public function addClass(string $name)
31
    {
32
        if(!in_array($name, $this->classes)) {
33
            $this->classes[] = $name;
34
        }
35
        
36
        return $this;
37
    }
38
    
39
    public function addClasses(array $names)
40
    {
41
        foreach($names as $name) {
42
            $this->addClass($name);
43
        }
44
        
45
        return $this;
46
    }
47
    
48
    public function hasClass(string $name) : bool
49
    {
50
        return in_array($name, $this->classes);
51
    }
52
    
53
    public function removeClass(string $name)
54
    {
55
        $idx = array_search($name, $this->classes);
56
        
57
        if($idx !== false) {
58
            unset($this->classes[$idx]);
59
            sort($this->classes);
60
        }
61
        
62
        return $this;
63
    }
64
    
65
    public function getClasses() : array
66
    {
67
        return $this->classes;
68
    }
69
    
70
    public function classesToString() : string
71
    {
72
        return implode(' ', $this->classes);
73
    }
74
}
75
76
/**
77
 * Interface for classes that use the classable trait.
78
 * The trait itself fulfills most of the interface, but
79
 * it is used to guarantee internal type checks will work,
80
 * as well as ensure the abstract methods are implemented.
81
 *
82
 * @package Application Utils
83
 * @subpackage Traits
84
 * @author Sebastian Mordziol <[email protected]>
85
 *
86
 * @see Traits_Classable
87
 */
88
interface Interface_Classable
89
{
90
   /**
91
    * @param string $name
92
    * @return Interface_Classable
93
    */
94
    public function addClass(string $name);
95
96
   /**
97
    * @param array $names
98
    * @return Interface_Classable
99
    */
100
    public function addClasses(array $names);
101
    
102
   /**
103
    * @param string $name
104
    * @return bool
105
    */
106
    public function hasClass(string $name) : bool;
107
    
108
   /**
109
    * @param string $name
110
    * @return Interface_Classable
111
    */
112
    public function removeClass(string $name);
113
    
114
   /**
115
    * @return array
116
    */
117
    public function getClasses() : array;
118
    
119
   /**
120
    * @return string
121
    */
122
    public function classesToString() : string;
123
}
124