1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\View; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Model View ViewModel (MVVM) |
17
|
|
|
* @see http://en.wikipedia.org/wiki/Model_View_ViewModel |
18
|
|
|
*/ |
19
|
|
|
abstract class ViewModel |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Option variable exposed to the templates. |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $options = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Config variable exposed to the templates. |
30
|
|
|
* @var array|null |
31
|
|
|
*/ |
32
|
|
|
public $config = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Deals with the request params/filters definitions. |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getOptions() |
40
|
|
|
{ |
41
|
|
|
return $this->options; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assigns a property. |
46
|
|
|
* |
47
|
|
|
* // This value can be accessed as {{foo}} within the template |
48
|
|
|
* $view->set('foo', 'my value'); |
49
|
|
|
* |
50
|
|
|
* You can also use an array to set several values at once: |
51
|
|
|
* |
52
|
|
|
* // Create the values {{food}} and {{beverage}} in the template |
53
|
|
|
* $view->set(array('food' => 'bread', 'beverage' => 'water')); |
54
|
|
|
* |
55
|
|
|
* @param string|array $blob A string key or an associative array to set. |
56
|
|
|
* @param mixed $value The value to set if the blob is a string. |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function set($blob, $value = null) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (is_array($blob)) { |
62
|
|
|
foreach ($blob as $key => $value) { |
63
|
|
|
$this->{$key} = $value; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
$this->{$blob} = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function get($key, $htmlize=true) |
73
|
|
|
{ |
74
|
|
|
$v = isset($this->{$key}) ? (array) $this->{$key} : array(); |
75
|
|
|
|
76
|
|
|
if (isset($this->options[$key])) { |
77
|
|
|
array_push($v, |
78
|
|
|
// is_array($this->options[$key]) |
|
|
|
|
79
|
|
|
// ? $this->options[$key][0] |
|
|
|
|
80
|
|
|
// : |
81
|
|
|
$this->options[$key] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($htmlize) { |
86
|
|
|
array_walk_recursive($v, function (&$v) { |
87
|
|
|
$v = ViewModel::htmlizer($v); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $v; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public static function htmlizer($string) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$pattern = array( |
97
|
|
|
'/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d\.]*)?)/', # URL |
98
|
|
|
'/([\w\-\d]+\@[\w\-\d]+\.[\w\-\d]+)/', # email |
99
|
|
|
// '/\S{2}/', # line break |
100
|
|
|
); |
101
|
|
|
$replace = array( |
102
|
|
|
'<a href="$1">$1</a>', |
103
|
|
|
'<a href="mailto:$1">$1</a>', |
104
|
|
|
// '- $1' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return preg_replace($pattern, $replace, $string); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Assigns a value by reference. The benefit of binding is that values can |
112
|
|
|
* be altered without re-setting them. It is also possible to bind variables |
113
|
|
|
* before they have values. Assigned values will be available as a |
114
|
|
|
* variable within the template file: |
115
|
|
|
* |
116
|
|
|
* // This reference can be accessed as {{ref}} within the template |
117
|
|
|
* $view->bind('ref', $bar); |
118
|
|
|
* |
119
|
|
|
* @param string variable name |
120
|
|
|
* @param mixed referenced variable |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function bind($key, & $value) |
124
|
|
|
{ |
125
|
|
|
$this->{$key} =& $value; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/* ---- generic helpers --- */ |
131
|
|
|
|
132
|
|
View Code Duplication |
public function hasMany($mix) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if (is_string($mix) && isset($this->{$mix})) { |
135
|
|
|
return count($this->{$mix})>1; |
136
|
|
|
} elseif (is_array($mix)) { |
137
|
|
|
return count($mix)>1; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns this view model layout. |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getLayout() |
149
|
|
|
{ |
150
|
|
|
return $this->_layout; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.