1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Model; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SparQL\Exception; |
7
|
|
|
|
8
|
|
|
abstract class Annotations |
9
|
|
|
{ |
10
|
|
|
protected $config = "object"; |
11
|
|
|
protected $annotations; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
public function getAnnotations($key = null, $default = null) |
15
|
|
|
{ |
16
|
|
|
if ($key == null) { |
17
|
|
|
return $this->annotations; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (!isset($this->annotations["$this->config:$key"])) { |
21
|
|
|
return $default; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (is_bool($default)) { |
25
|
|
|
return array_key_exists($key, $this->annotations); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $this->annotations["$this->config:$key"]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function replaceVars($name, $text) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
# Host |
34
|
|
|
$port = isset($_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : 80; |
35
|
|
|
$httpHost = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'localhost'; |
36
|
|
|
$host = ($port == 443 ? "https://" : "http://").$httpHost; |
37
|
|
|
# Replace Part One |
38
|
|
|
$text = preg_replace(array("/\{[hH][oO][sS][tT]\}/", "/\{[cC][lL][aA][sS][sS]\}/"), array($host, $name), $text); |
39
|
|
|
if (preg_match('/(\{(\S+)\})/', $text, $matches)) { |
40
|
|
|
$class = new ReflectionClass(get_class($this->_model)); |
|
|
|
|
41
|
|
|
$method = str_replace("()", "", $matches[2]); |
42
|
|
|
$value = spl_object_hash($this->_model); |
43
|
|
|
if ($class->hasMethod($method)) { |
44
|
|
|
try { |
45
|
|
|
$value = $this->_model->$method(); |
46
|
|
|
} catch (Exception $ex) { |
47
|
|
|
$value = "***$value***"; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$text = preg_replace('/(\{(\S+)\})/', $value, $text); |
51
|
|
|
} |
52
|
|
|
return $text; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function adjustParams($arr) |
56
|
|
|
{ |
57
|
|
|
$count = count($arr[0]); |
58
|
|
|
$result = array(); |
59
|
|
|
|
60
|
|
|
for ($i = 0; $i < $count; $i++) { |
61
|
|
|
$key = strtolower($arr["param"][$i]); |
62
|
|
|
$value = $arr["value"][$i]; |
63
|
|
|
|
64
|
|
|
if (!array_key_exists($key, $result)) { |
65
|
|
|
$result[$key] = $value; |
66
|
|
|
} elseif (is_array($result[$key])) { |
67
|
|
|
$result[$key][] = $value; |
68
|
|
|
} else { |
69
|
|
|
$result[$key] = array($result[$key], $value); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: