forked from berkes/tagadelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagadelicTag.php
181 lines (159 loc) · 4.45 KB
/
TagadelicTag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* class TagadelicTag
* TagadelicTag contains the tag itself.
*/
class TagadelicTag {
private $id = 0; # Identifier of this tag
private $name = ""; # A human readable name for this tag.
private $description = ""; # A human readable piece of HTML-formatted text.
private $link = ""; # Where this tag will point to. If left empty, tag will not be linked. Can be a full url too.
private $count = 0.0000001;# Absolute count for the weight. Weight, i.e. tag-size will be extracted from this.
private $dirty = true;
private $weight = 0.0;
private $drupal = NULL; # Contains the DrupalWrapper, mostly for testablity
/**
* Initalize this tag
* @param id Integer the identifier of this tag
* @param name String a human readable name describing this tag
*/
function __construct($id, $name, $count) {
$this->id = $id;
$this->name = $name;
if($count != 0) {
$this->count = $count;
}
}
/**
* Magic method to render the Tag.
* turns the tag into an HTML link to its source.
*/
public function __ToString() {
$this->clean();
$attributes = $options = array();
if (!empty($this->description)) $attributes["title"] = $this->description;
if ($this->weight > 0) $attributes["class"][] = "level{$this->weight}";
if (!empty($attributes)) $options["attributes"] = $attributes;
return $this->drupal()->l($this->name, $this->link, $options);
}
/**
* Getter for the ID
* @ingroup getters
* return Integer Identifier
**/
public function get_id() {
return $this->id;
}
/**
* Getter for the name
* @ingroup getters
* return String the human readable name
**/
public function get_name() {
$this->clean();
return $this->name;
}
/**
* Getter for the description
* @ingroup getters
* return String the human readable description
**/
public function get_description() {
$this->clean();
return $this->description;
}
/**
* Returns the weight, getter only.
* Will call recalculate to calculate the weight.
* @ingroup getters
* return Float the weight of this tag.
**/
public function get_weight() {
return $this->weight;
}
/**
* Returns the count, getter only.
* @ingroup getters
* return Int the count as provided when Initializing the Object.
**/
public function get_count() {
return $this->count;
}
/**
* Sets the optional description.
* A tag may have a description
* @param $description String a description
*/
public function set_description($description) {
$this->description = $description;
}
/**
* Link to a resource.
* @param link String Optional a link to a resource that represents
* the tag. e.g. a listing with all things tagged with Tag, or
* the article that represents the tag.
*/
public function set_link($link) {
$this->link = $link;
}
/**
* setter for weight
* Operates on $this
* Returns $this
*/
public function set_weight($weight) {
$this->weight = $weight;
return $this;
}
/**
* setter for drupal(Wrapper)
* Operates on $this
* Returns $this
*/
public function set_drupal($drupal) {
$this->drupal = $drupal;
return $this;
}
/**
* Getter for drupal, if not found, will instantiate a default TagaDelicDrupalWrapper
* @return type value in $this::$drupal.
*/
public function drupal() {
if (empty($this->drupal)) {
$this->drupal = new TagaDelicDrupalWrapper();
}
return $this->drupal;
}
/**
* Flag $name and $description as dirty; none-cleaned.
* BEWARE! This will probably lead to double escaping, unless you know what you are doing.
*/
public function force_dirty() {
$this->dirty = true;
}
/**
* Flag $name and $description as safe.
* XSS-escaping and sanitizing is left to implementer.
* BEWARE! Only enforce when you know what you are doing. Seriously!
*/
public function force_clean() {
$this->dirty = false;
}
/**
* Calculates a more evenly distributed value.
*/
public function distributed() {
return log($this->count);
}
/**
* Utility, to enforce XSS filtering on strings before they are
* printed or returned.
**/
private function clean() {
if ($this->dirty) {
$this->name = $this->drupal()->check_plain($this->name);
$this->description = $this->drupal()->check_plain($this->description);
$this->force_clean();
}
}
}