-
Notifications
You must be signed in to change notification settings - Fork 1
/
bfox_ref_linker.php
74 lines (55 loc) · 1.94 KB
/
bfox_ref_linker.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
<?php
class BfoxRefLinker extends BfoxObject {
var $attributeValues = array();
var $attributeCallbacks = array();
function __construct() {
$this->attributeCallbacks = array(
'href' => $this->functionWithName('urlForRefStr'),
'data-ref' => $this->functionWithName('refStrCB'),
'text' => $this->functionWithName('refStrCB'),
);
}
function addAttributeValue($attribute, $value) {
if (!isset($this->attributeValues[$attribute])) $this->attributeValues[$attribute] = array();
if (!in_array($value, (array) $this->attributeValues[$attribute])) {
$this->attributeValues[$attribute] []= $value;
}
}
function removeAttributeValue($attribute, $value) {
if (!isset($this->attributeValues[$attribute])) return;
$key = array_search($value, $this->attributeValues[$attribute]);
if (false !== $key) {
array_splice($this->attributeValues[$attribute], $key, 1);
}
}
function addClass($class) {
$this->addAttributeValue('class', $class);
}
function removeClass($class) {
$this->removeAttributeValue('class', $class);
}
function urlForRefStr($refStr) {
return urlencode(strtolower($refStr));
}
function refStrCB($refStr) {
return $refStr;
}
function linkForRefStr($refStr, $attributes = array()) {
if (empty($refStr)) return false;
$attributes = BfoxArray::unsetEmptyElements($attributes);
$attributes = BfoxArray::populateArrayUsingCallbacks($attributes, $this->attributeCallbacks, array($refStr, &$this));
$attributes = BfoxArray::populateArrayUsingArray($attributes, $this->attributeValues);
$text = $attributes['text'];
unset($attributes['text']);
$attributeStr = BfoxHTML::attributeStr($attributes);
return "<a $attributeStr>$text</a>";
}
function linkForTextAndRef($text, BfoxRef $ref, $attributes = array()) {
$attributes['text'] = $text;
return $this->linkForRefStr($ref->get_string(), $attributes);
}
function replaceCallback() {
return $this->functionWithName('linkForTextAndRef');
}
}
?>