1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
This project is Licenced under The MIT License (MIT) |
4
|
|
|
|
5
|
|
|
Copyright (c) 2014 Christopher Seufert |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in |
15
|
|
|
all copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
THE SOFTWARE. |
24
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
namespace Seufert\Hamle\Text; |
27
|
|
|
|
28
|
|
|
use Seufert\Hamle; |
29
|
|
|
use Seufert\Hamle\Model; |
30
|
|
|
use Seufert\Hamle\Text; |
31
|
|
|
use Seufert\Hamle\Exception\ParseError; |
32
|
|
|
|
33
|
|
|
class Func extends SimpleVar { |
34
|
|
|
|
35
|
|
|
const REGEX_FUNCSEL = '[a-zA-Z0-9\*\.,#_:\\^\\-@\\${}[\]]'; |
36
|
|
|
|
37
|
|
|
protected $sub = null; |
38
|
|
|
|
39
|
|
|
/** @var bool|Scope */ |
40
|
|
|
protected $scope = false; |
41
|
|
|
|
42
|
|
|
protected $filt; |
43
|
|
|
|
44
|
|
|
protected $sortlimit; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Func constructor. |
49
|
|
|
* @param string $s |
50
|
|
|
*/ |
51
|
|
|
public function __construct($s) { |
52
|
|
|
$m = array(); |
53
|
|
|
if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m)) |
54
|
|
|
throw new ParseError("Unable to read \$ func in '$s'"); |
55
|
|
|
if (trim($m[2])) |
56
|
|
|
$this->sub = new FuncSub($m[2]); |
57
|
|
|
if (!trim($m[1])) { |
58
|
|
|
$this->scope = true; |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
if ($m[1][0] == '$' && $m[1][1] == '[') { |
62
|
|
|
$this->scope = new Scope($m[1]); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$this->sortlimit = $this->attSortLimit($m[1]); |
66
|
|
|
$this->filt = $this->attIdTag($m[1]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function attIdTag(&$s) { |
70
|
|
|
$m = array(); |
71
|
|
|
$att = array('id' => array(), 'tag' => array()); |
72
|
|
|
foreach (explode(",", $s) as $str) { |
73
|
|
|
if (preg_match('/^[a-zA-Z0-9\\_]+/', $str, $m)) $type = $m[0]; |
74
|
|
|
else $type = "*"; |
75
|
|
|
if (preg_match('/#([a-zA-Z0-9\_\\${}]+)/', $str, $m)) $att['id'][$type][] = $m[1]; |
76
|
|
|
elseif (preg_match_all('/\\.([a-zA-Z0-9\_\-\\${}]+)/', $str, $m)) |
77
|
|
|
foreach ($m[1] as $tag) |
78
|
|
|
$att['tag'][$type][] = new Text($tag, Text::TOKEN_CODE); |
79
|
|
|
else $att['tag'][$type] = array(); |
80
|
|
|
} |
81
|
|
|
if (!(count($att['id']) xor count($att['tag']))) |
82
|
|
|
throw new ParseError("Only tag, type or id can be combined"); |
83
|
|
|
return $att; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function attSortLimit(&$s) { |
87
|
|
|
$att = array('limit' => 0, 'offset' => 0, 'sort'=> []); |
88
|
|
|
$m = array(); |
89
|
|
|
if (preg_match('/:(?:([0-9]+)\-)?([0-9]+)/', $s, $m)) { |
90
|
|
|
$att['limit'] = $m[2]; |
91
|
|
|
$att['offset'] = $m[1] ? $m[1] : 0; |
92
|
|
|
} |
93
|
|
|
$rand = false; |
94
|
|
|
if (preg_match_all('/\\^(-?)([a-zA-Z0-9\_]*)/', $s, $m)) { |
95
|
|
|
foreach($m[0] as $k=>$mv) |
96
|
|
|
if ($m[2][$k]) { |
97
|
|
|
$dir = $m[1][$k] == "-"?Hamle\Hamle::SORT_DESCENDING:Hamle\Hamle::SORT_ASCENDING; |
98
|
|
|
$att['sort'][$m[2][$k]] = $dir; |
99
|
|
|
} else $rand = true; |
100
|
|
|
} |
101
|
|
|
if($rand) |
102
|
|
|
$att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM]; |
103
|
|
|
return $att; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function attGroupType(&$s) { |
107
|
|
|
$att = array('grouptype' => 0); |
108
|
|
|
$m = array(); |
109
|
|
|
if (preg_match('/@([0-9]+)/', $s, $m)) { |
110
|
|
|
$att['grouptype'] = $m[1]; |
111
|
|
|
} |
112
|
|
|
return $att; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string PHP Code |
117
|
|
|
*/ |
118
|
|
|
public function toPHP() { |
119
|
|
|
$sub = $this->sub ? "->" . $this->sub->toPHP() : ""; |
120
|
|
|
if($this->scope instanceof Scope) { |
121
|
|
|
return $this->scope->toPHP() . $sub; |
122
|
|
|
} elseif($this->scope === true) { |
123
|
|
|
return "Hamle\\Scope::get(0)$sub"; |
124
|
|
|
} |
125
|
|
|
$limit = Text::varToCode($this->sortlimit['sort']) . "," . |
126
|
|
|
$this->sortlimit['limit'] . "," . $this->sortlimit['offset']; |
127
|
|
|
if (count($this->filt['tag'])) |
128
|
|
|
return "Hamle\\Run::modelTypeTags(" . |
129
|
|
|
Text::varToCode($this->filt['tag']) . ",$limit)$sub"; |
130
|
|
|
if (count($this->filt['id'])) |
131
|
|
|
if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1) |
132
|
|
|
return "Hamle\\Run::modelId(" . |
133
|
|
|
Text::varToCode(current($this->filt['id']['*'])) . |
134
|
|
|
",$limit)$sub"; |
135
|
|
|
else |
136
|
|
|
return "Hamle\\Run::modelTypeId(" . |
137
|
|
|
Text::varToCode($this->filt['id']) . ",$limit)$sub"; |
138
|
|
|
return ""; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Model|null $parent |
143
|
|
|
* @return Model |
144
|
|
|
*/ |
145
|
|
|
public function getOrCreateModel(Model $parent = null) { |
146
|
|
|
if($this->scope instanceof Scope) { |
147
|
|
|
$parent = $this->scope->getOrCreateModel(); |
148
|
|
|
} elseif ($this->scope === true) |
149
|
|
|
$parent = \Seufert\Hamle\Scope::get(0); |
150
|
|
|
if ($this->filt && count($this->filt['tag'])) |
151
|
|
|
$parent = \Seufert\Hamle\Run::modelTypeTags( |
152
|
|
|
$this->filt['tag'], |
153
|
|
|
$this->sortlimit['sort'], |
154
|
|
|
$this->sortlimit['limit'], |
155
|
|
|
$this->sortlimit['offset'] |
156
|
|
|
); |
157
|
|
|
if ($this->filt && count($this->filt['id'])) |
158
|
|
|
if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1) |
159
|
|
|
$parent = \Seufert\Hamle\Run::modelId( |
160
|
|
|
current($this->filt['id']['*']), |
161
|
|
|
$this->sortlimit['sort'], |
162
|
|
|
$this->sortlimit['limit'], |
163
|
|
|
$this->sortlimit['offset'] |
164
|
|
|
); |
165
|
|
|
else |
166
|
|
|
$parent = \Seufert\Hamle\Run::modelTypeId( |
167
|
|
|
$this->filt['id'], |
168
|
|
|
$this->sortlimit['sort'], |
169
|
|
|
$this->sortlimit['limit'], |
170
|
|
|
$this->sortlimit['offset'] |
171
|
|
|
); |
172
|
|
|
if($this->sub) |
173
|
|
|
return $this->sub->getOrCreateModel($parent); |
174
|
|
|
if(!$parent) |
175
|
|
|
throw new \RuntimeException('Unable to create model with no relation'); |
176
|
|
|
return $parent; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function toHTML($escape = false) { |
180
|
|
|
throw new ParseError("Unable to use Scope operator in HTML Code"); |
181
|
|
|
} |
182
|
|
|
} |