1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Aware; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TagAwareTrait |
19
|
|
|
* |
20
|
|
|
* One implementation of TagAwareInterface |
21
|
|
|
* |
22
|
|
|
* @package Phossa2\Shared |
23
|
|
|
* @author Hong Zhang <[email protected]> |
24
|
|
|
* @see TagAwareInterface |
25
|
|
|
* @version 2.0.0 |
26
|
|
|
* @since 2.0.0 added |
27
|
|
|
*/ |
28
|
|
|
trait TagAwareTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* associate array (for speed consideration) of tags |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
* @access private |
35
|
|
|
*/ |
36
|
|
|
private $tags = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a single tag |
40
|
|
|
* |
41
|
|
|
* @param string $tag |
42
|
|
|
* @return $this |
43
|
|
|
* @access public |
44
|
|
|
* @api |
45
|
|
|
*/ |
46
|
|
|
public function addTag(/*# string */ $tag) |
47
|
|
|
{ |
48
|
|
|
$this->tags[(string) $tag] = true; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Has this tag ? |
54
|
|
|
* |
55
|
|
|
* @param string $tag |
56
|
|
|
* @return bool |
57
|
|
|
* @access public |
58
|
|
|
* @api |
59
|
|
|
*/ |
60
|
|
|
public function hasTag(/*# string */ $tag)/*# : bool */ |
61
|
|
|
{ |
62
|
|
|
return isset($this->tags[(string) $tag]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove one tag |
67
|
|
|
* |
68
|
|
|
* @param string $tag |
69
|
|
|
* @return $this |
70
|
|
|
* @access public |
71
|
|
|
* @api |
72
|
|
|
*/ |
73
|
|
|
public function removeTag(/*# string */ $tag) |
74
|
|
|
{ |
75
|
|
|
unset($this->tags[(string) $tag]); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set/replace all tags, empty array to clear all tags |
81
|
|
|
* |
82
|
|
|
* @param array $tags like ['apple', 'orange'] |
83
|
|
|
* @return $this |
84
|
|
|
* @access public |
85
|
|
|
* @api |
86
|
|
|
*/ |
87
|
|
|
public function setTags(array $tags) |
88
|
|
|
{ |
89
|
|
|
$this->tags = array_flip($tags); |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get all tags in array |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
* @access public |
98
|
|
|
* @api |
99
|
|
|
*/ |
100
|
|
|
public function getTags()/*# : array */ |
101
|
|
|
{ |
102
|
|
|
return array_keys($this->tags); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return array of tags existed, return empty [] if no match found |
107
|
|
|
* |
108
|
|
|
* @param array $tags tags to match against |
109
|
|
|
* @return array |
110
|
|
|
* @access public |
111
|
|
|
* @api |
112
|
|
|
*/ |
113
|
|
|
public function hasTags(array $tags)/*# : array */ |
114
|
|
|
{ |
115
|
|
|
$x = []; |
116
|
|
|
foreach ($tags as $tag) { |
117
|
|
|
if ($this->hasTag($tag)) { |
118
|
|
|
$x[] = $tag; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
return $x; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|