forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubstitutionTemplate.class.php
108 lines (96 loc) · 2.58 KB
/
SubstitutionTemplate.class.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
<?php
/**
* Stores the definition for class SubstitutionTemplate
*/
/**
* Manages a string substitution set
* @author etessore
* @version 1.0.0
* @package classes
*/
class SubstitutionTemplate{
/**
* @var array Stores some static html
*/
public $static_markup;
/**
* @var string the substitution template
*/
public $tpl;
/**
* Initializes the current object with default parameters
*/
public function __construct($tpl=''){
$this->set_tpl($tpl);
}
public function minify_template(){
$this->tpl = str_replace(array("\n", "\r"), '', $this->tpl);
$this->tpl = preg_replace('@ {2,}@', ' ', $this->tpl);
return $this;
}
/**
* Sets the substitutions template
* @param string $tpl the template
*/
public function set_tpl($tpl){
$this->tpl = $tpl;
return $this;
}
/**
* Retrieves the markup for the given key
* It returns false if the key isn't defined
* @param string $key key to be searched
*/
public function get_markup($key){
if(isset($this->static_markup[$key]))
return $this->static_markup[$key];
return false;
}
/**
* Set the static markup; ie: prev\next\loading divs
* @param string|array $key the searches to be substituted
* @param string|array $markup html markups
* @return SubstitutionTemplate $this for chainability
* @throws Exception if $key and $markup have different number of elements
*/
public function set_markup($key, $markup){
$key = (array) $key;
$markup = (array) $markup;
if(count($markup) == 1 && count($key) > 1){
foreach($key as $k => $v){
$this->static_markup[$v] = $markup[0];
}
} elseif(count($markup) != count($key)){
throw new Exception('$key and $markup have different number of elements');
} else {
foreach($key as $k => $v){
$this->static_markup[$v] = $markup[$k];
}
}
//$this->static_markup[$key] = $markup;
return $this;
}
/**
* Bulk set the key:markup pairs
* @param array $ass_array an associative array of keys and markups
*/
public function set_multi_markup($ass_array){
$ass_array = (array) $ass_array;
$this->set_markup(array_keys($ass_array), array_values($ass_array));
return $this;
}
/**
* Replaces the markup in $this->tpl %tag%s with the one
* in the corresponding value of $this->static_markup[tag].
*/
public function replace_markup(){
return str_replace(
array_map(
create_function('$k', 'return "%".$k."%";'),
array_keys($this->static_markup)
),
array_values($this->static_markup),
$this->tpl
);
}
}