@@ -73,14 +73,16 @@ discard block |
||
73 | 73 | $scopeName = $m[1]; |
74 | 74 | $lookup = substr($this->var, 0, strlen($this->var) - strlen($m[0])); |
75 | 75 | $hsv = new H\Text(trim($lookup), H\Text::TOKEN_CONTROL); |
76 | - } else |
|
77 | - $hsv = new H\Text($this->var, H\Text::TOKEN_CONTROL); |
|
76 | + } else { |
|
77 | + $hsv = new H\Text($this->var, H\Text::TOKEN_CONTROL); |
|
78 | + } |
|
78 | 79 | switch ($this->type) { |
79 | 80 | case "each": |
80 | - if ($this->var) |
|
81 | - $out .= "foreach(" . $hsv->toPHP() . " as {$this->o}) { \n"; |
|
82 | - else |
|
83 | - $out .= "foreach(Hamle\\Scope::get() as {$this->o}) { \n"; |
|
81 | + if ($this->var) { |
|
82 | + $out .= "foreach(" . $hsv->toPHP() . " as {$this->o}) { \n"; |
|
83 | + } else { |
|
84 | + $out .= "foreach(Hamle\\Scope::get() as {$this->o}) { \n"; |
|
85 | + } |
|
84 | 86 | $out .= "Hamle\\Scope::add({$this->o}); "; |
85 | 87 | break; |
86 | 88 | case "if": |
@@ -88,9 +90,9 @@ discard block |
||
88 | 90 | $out .= "if(" . $hsvcomp->toPHP() . ") {"; |
89 | 91 | break; |
90 | 92 | case "with": |
91 | - if ($scopeName) |
|
92 | - $out .= "Hamle\\Scope::add(" . $hsv->toPHP() . ", \"$scopeName\");\n;"; |
|
93 | - else { |
|
93 | + if ($scopeName) { |
|
94 | + $out .= "Hamle\\Scope::add(" . $hsv->toPHP() . ", \"$scopeName\");\n;"; |
|
95 | + } else { |
|
94 | 96 | $out .= "if(({$this->o} = " . $hsv->toPHP() . ") && " . |
95 | 97 | "{$this->o}->valid()) {\n"; |
96 | 98 | $out .= "Hamle\\Scope::add({$this->o});\n;"; |
@@ -101,10 +103,11 @@ discard block |
||
101 | 103 | break; |
102 | 104 | case "include": |
103 | 105 | $file = $hsv->toHTML(); |
104 | - if($file[0] == "#") |
|
105 | - $out .= "echo Hamle\\Run::includeFragment(".$hsv->toPHP().");"; |
|
106 | - else |
|
107 | - $out .= "echo Hamle\\Run::includeFile(" . $hsv->toPHP() . ");"; |
|
106 | + if($file[0] == "#") { |
|
107 | + $out .= "echo Hamle\\Run::includeFragment(".$hsv->toPHP().");"; |
|
108 | + } else { |
|
109 | + $out .= "echo Hamle\\Run::includeFile(" . $hsv->toPHP() . ");"; |
|
110 | + } |
|
108 | 111 | break; |
109 | 112 | } |
110 | 113 | return $out . ' ?>'; |
@@ -123,8 +126,9 @@ discard block |
||
123 | 126 | case "each"; |
124 | 127 | $out .= 'Hamle\\Scope::done(); '; |
125 | 128 | $out .= '}'; |
126 | - if (!$this->var) |
|
127 | - $out .= "Hamle\\Scope::get()->rewind();\n"; |
|
129 | + if (!$this->var) { |
|
130 | + $out .= "Hamle\\Scope::get()->rewind();\n"; |
|
131 | + } |
|
128 | 132 | break; |
129 | 133 | case "if": |
130 | 134 | case "else": |
@@ -140,7 +144,9 @@ discard block |
||
140 | 144 | return ""; |
141 | 145 | break; |
142 | 146 | } |
143 | - if ($this->else) $out .= "else{"; |
|
147 | + if ($this->else) { |
|
148 | + $out .= "else{"; |
|
149 | + } |
|
144 | 150 | return $out . ' ?>'; |
145 | 151 | } |
146 | 152 |
@@ -18,12 +18,14 @@ discard block |
||
18 | 18 | static $namedScopes = array(); |
19 | 19 | |
20 | 20 | static function add($model, $name = null) { |
21 | - if (!$model instanceOf Model) |
|
22 | - throw new Unsupported("Unsupported Model (".get_class($model)."), Needs to implement hamleModel Interface"); |
|
23 | - if ($name) |
|
24 | - self::$namedScopes[$name] = $model; |
|
25 | - else |
|
26 | - self::$scopes[] = $model; |
|
21 | + if (!$model instanceOf Model) { |
|
22 | + throw new Unsupported("Unsupported Model (".get_class($model)."), Needs to implement hamleModel Interface"); |
|
23 | + } |
|
24 | + if ($name) { |
|
25 | + self::$namedScopes[$name] = $model; |
|
26 | + } else { |
|
27 | + self::$scopes[] = $model; |
|
28 | + } |
|
27 | 29 | } |
28 | 30 | |
29 | 31 | static function done() { |
@@ -41,15 +43,21 @@ discard block |
||
41 | 43 | */ |
42 | 44 | static function get($id = 0) { |
43 | 45 | if (0 == $id) { |
44 | - if ($scope = end(self::$scopes)) |
|
45 | - return $scope; |
|
46 | + if ($scope = end(self::$scopes)) { |
|
47 | + return $scope; |
|
48 | + } |
|
46 | 49 | throw new OutOfScope("Unable to find Scope ($id)"); |
47 | 50 | } |
48 | 51 | $key = $id - 1; |
49 | - if ($id < 0) $key = count(self::$scopes) + $id - 1; |
|
50 | - if ($id == 0) $key = count(self::$scopes) - 1; |
|
51 | - if (!isset(self::$scopes[$key])) |
|
52 | - throw new OutOfScope("Unable to find Scope ($id) or $key"); |
|
52 | + if ($id < 0) { |
|
53 | + $key = count(self::$scopes) + $id - 1; |
|
54 | + } |
|
55 | + if ($id == 0) { |
|
56 | + $key = count(self::$scopes) - 1; |
|
57 | + } |
|
58 | + if (!isset(self::$scopes[$key])) { |
|
59 | + throw new OutOfScope("Unable to find Scope ($id) or $key"); |
|
60 | + } |
|
53 | 61 | return self::$scopes[$key]; |
54 | 62 | } |
55 | 63 | |
@@ -67,8 +75,9 @@ discard block |
||
67 | 75 | self::$namedScopes[$name]->rewind(); |
68 | 76 | return self::$namedScopes[$name]; |
69 | 77 | } else |
70 | - if(self::$returnZeroOnNoScope) |
|
71 | - return new Model\Zero(); |
|
78 | + if(self::$returnZeroOnNoScope) { |
|
79 | + return new Model\Zero(); |
|
80 | + } |
|
72 | 81 | throw new RunTime("Unable to find scope ($name)"); |
73 | 82 | } |
74 | 83 |
@@ -109,22 +109,25 @@ discard block |
||
109 | 109 | $revSnip = array(); |
110 | 110 | /** @var Tag[] $roots */ |
111 | 111 | $roots = array(); |
112 | - foreach ($this->root as $snip) |
|
113 | - if ($snip instanceOf Tag\Snippet) { |
|
112 | + foreach ($this->root as $snip) { |
|
113 | + if ($snip instanceOf Tag\Snippet) { |
|
114 | 114 | if ($snip->getType() == "append") { |
115 | - array_unshift($revSnip, $snip); |
|
115 | + array_unshift($revSnip, $snip); |
|
116 | + } |
|
116 | 117 | } else { |
117 | 118 | $fwdSnip[] = $snip; |
118 | 119 | } |
119 | 120 | } else { |
120 | 121 | $roots[] = $snip; |
121 | 122 | } |
122 | - foreach ($fwdSnip as $snip) |
|
123 | - foreach ($roots as $root) |
|
124 | - $snip->apply($root); |
|
125 | - foreach ($revSnip as $snip) |
|
126 | - foreach ($roots as $root) |
|
127 | - $snip->apply($root); |
|
123 | + foreach ($fwdSnip as $snip) { |
|
124 | + foreach ($roots as $root) |
|
125 | + $snip->apply($root); |
|
126 | + } |
|
127 | + foreach ($revSnip as $snip) { |
|
128 | + foreach ($roots as $root) |
|
129 | + $snip->apply($root); |
|
130 | + } |
|
128 | 131 | $this->root = $roots; |
129 | 132 | } |
130 | 133 | |
@@ -144,9 +147,11 @@ discard block |
||
144 | 147 | $heir = array(); |
145 | 148 | while ($this->lineNo < $this->lineCount) { |
146 | 149 | $line = $this->lines[$this->lineNo]; |
147 | - if (trim($line)) if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
150 | + if (trim($line)) { |
|
151 | + if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
148 | 152 | if (FALSE !== strpos($m[1], "\t")) |
149 | - throw new ParseError("Tabs are not supported in templates at this time"); |
|
153 | + throw new ParseError("Tabs are not supported in templates at this time"); |
|
154 | + } |
|
150 | 155 | $indent = strlen($m[1]); |
151 | 156 | $tag = isset($m[2]) ? $tag = $m[2] : ""; |
152 | 157 | $classid = isset($m[3]) ? $m[3] : ""; |
@@ -158,13 +163,13 @@ discard block |
||
158 | 163 | unset($m[0]); |
159 | 164 | switch (strlen($code) ? $code[0] : ($textcode ? $textcode : "")) { |
160 | 165 | case "|": //Control Tag |
161 | - if ($code == "|snippet") |
|
162 | - $hTag = new Tag\Snippet($text); |
|
163 | - elseif ($code == "|form") |
|
164 | - $hTag = new Tag\Form($text); |
|
165 | - elseif ($code == "|formhint") |
|
166 | - $hTag = new Tag\FormHint($text); |
|
167 | - elseif ($code == "|else") { |
|
166 | + if ($code == "|snippet") { |
|
167 | + $hTag = new Tag\Snippet($text); |
|
168 | + } elseif ($code == "|form") { |
|
169 | + $hTag = new Tag\Form($text); |
|
170 | + } elseif ($code == "|formhint") { |
|
171 | + $hTag = new Tag\FormHint($text); |
|
172 | + } elseif ($code == "|else") { |
|
168 | 173 | $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
169 | 174 | $hTag->setVar($text); |
170 | 175 | } else { |
@@ -175,8 +180,9 @@ discard block |
||
175 | 180 | case ":": //Filter Tag |
176 | 181 | $hTag = new Tag\Filter(substr($code, 1)); |
177 | 182 | $hTag->addContent($text, Text::TOKEN_CODE); |
178 | - foreach ($this->consumeBlock($indent) as $l) |
|
179 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
183 | + foreach ($this->consumeBlock($indent) as $l) { |
|
184 | + $hTag->addContent($l, Text::TOKEN_CODE); |
|
185 | + } |
|
180 | 186 | break; |
181 | 187 | case "_": //String Tag |
182 | 188 | case "__": //Unescape String Tag |
@@ -187,8 +193,9 @@ discard block |
||
187 | 193 | case "//": // Non Printed Comment |
188 | 194 | $hTag = new Tag\Comment($textcode); |
189 | 195 | $hTag->addContent($text); |
190 | - foreach ($this->consumeBlock($indent) as $l) |
|
191 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
196 | + foreach ($this->consumeBlock($indent) as $l) { |
|
197 | + $hTag->addContent($l, Text::TOKEN_CODE); |
|
198 | + } |
|
192 | 199 | break; |
193 | 200 | default: |
194 | 201 | $attr = array(); |
@@ -201,25 +208,34 @@ discard block |
||
201 | 208 | } |
202 | 209 | $class = array(); $id = ""; $ref = ""; |
203 | 210 | preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
204 | - if (isset($cid[0])) foreach ($cid[0] as $s) { |
|
205 | - if ($s[0] == "#") $id = substr($s, 1); |
|
206 | - if ($s[0] == ".") $class[] = substr($s, 1); |
|
207 | - if ($s[0] == "!") $ref = substr($s, 1); |
|
211 | + if (isset($cid[0])) { |
|
212 | + foreach ($cid[0] as $s) { |
|
213 | + if ($s[0] == "#") $id = substr($s, 1); |
|
214 | + } |
|
215 | + if ($s[0] == ".") { |
|
216 | + $class[] = substr($s, 1); |
|
217 | + } |
|
218 | + if ($s[0] == "!") { |
|
219 | + $ref = substr($s, 1); |
|
220 | + } |
|
208 | 221 | } |
209 | - if($ref) |
|
210 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
211 | - else |
|
212 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
222 | + if($ref) { |
|
223 | + $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
224 | + } else { |
|
225 | + $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
226 | + } |
|
213 | 227 | $hTag->addContent($text); |
214 | 228 | break; |
215 | 229 | } |
216 | 230 | $heir[$i] = $hTag; |
217 | - if ($i > 0) |
|
218 | - $heir[$i - 1]->addChild($hTag); |
|
219 | - else |
|
220 | - $this->root[] = $hTag; |
|
221 | - } else |
|
222 | - throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/" . preg_last_error()); |
|
231 | + if ($i > 0) { |
|
232 | + $heir[$i - 1]->addChild($hTag); |
|
233 | + } else { |
|
234 | + $this->root[] = $hTag; |
|
235 | + } |
|
236 | + } else { |
|
237 | + throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/" . preg_last_error()); |
|
238 | + } |
|
223 | 239 | $this->lineNo++; |
224 | 240 | } |
225 | 241 | } |
@@ -235,8 +251,9 @@ discard block |
||
235 | 251 | |
236 | 252 | function output() { |
237 | 253 | $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
238 | - foreach ($this->root as $tag) |
|
239 | - $out .= $tag->render(); |
|
254 | + foreach ($this->root as $tag) { |
|
255 | + $out .= $tag->render(); |
|
256 | + } |
|
240 | 257 | return $out; |
241 | 258 | |
242 | 259 | } |
@@ -248,15 +265,18 @@ discard block |
||
248 | 265 | (!trim($this->lines[$this->lineNo + 1]) || |
249 | 266 | preg_match('/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
250 | 267 | $this->lines[$this->lineNo + 1], $m))) { |
251 | - if (trim($this->lines[$this->lineNo + 1])) |
|
252 | - $out[] = $m[2]; |
|
268 | + if (trim($this->lines[$this->lineNo + 1])) { |
|
269 | + $out[] = $m[2]; |
|
270 | + } |
|
253 | 271 | $this->lineNo++; |
254 | 272 | } |
255 | 273 | return $out; |
256 | 274 | } |
257 | 275 | |
258 | 276 | function indentLevel($indent) { |
259 | - if (!isset($this->indents)) $this->indents = array(); |
|
277 | + if (!isset($this->indents)) { |
|
278 | + $this->indents = array(); |
|
279 | + } |
|
260 | 280 | if (!count($this->indents)) { |
261 | 281 | $this->indents = array(0 => $indent); |
262 | 282 | // Key = indent level, Value = Depth in spaces |
@@ -44,8 +44,9 @@ discard block |
||
44 | 44 | } |
45 | 45 | |
46 | 46 | function toHTML($escape = false) { |
47 | - if($escape) |
|
48 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
47 | + if($escape) { |
|
48 | + return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
49 | + } |
|
49 | 50 | return "<?=" . $this->toPHP() . "?>"; |
50 | 51 | } |
51 | 52 | |
@@ -67,8 +68,9 @@ discard block |
||
67 | 68 | */ |
68 | 69 | function setValue($value) { |
69 | 70 | $model = $this->getOrCreateModel(); |
70 | - if(!$model instanceof WriteModel) |
|
71 | - throw new \RuntimeException('Can only write to model that implements WriteModel'); |
|
71 | + if(!$model instanceof WriteModel) { |
|
72 | + throw new \RuntimeException('Can only write to model that implements WriteModel'); |
|
73 | + } |
|
72 | 74 | $model->hamleSet($this->var, $value); |
73 | 75 | return $model; |
74 | 76 | } |
@@ -54,10 +54,11 @@ discard block |
||
54 | 54 | */ |
55 | 55 | static function popInstance() { |
56 | 56 | array_pop(self::$hamleList); |
57 | - if (self::$hamleList) |
|
58 | - self::$hamle = self::$hamleList[count(self::$hamleList) - 1]; |
|
59 | - else |
|
60 | - self::$hamle = NULL; |
|
57 | + if (self::$hamleList) { |
|
58 | + self::$hamle = self::$hamleList[count(self::$hamleList) - 1]; |
|
59 | + } else { |
|
60 | + self::$hamle = NULL; |
|
61 | + } |
|
61 | 62 | } |
62 | 63 | |
63 | 64 | /** |
@@ -113,7 +114,9 @@ discard block |
||
113 | 114 | */ |
114 | 115 | static function modelId($id, $sort = [], $limit = 0, $offset = 0) { |
115 | 116 | $o = self::$hamle->setup->getModelDefault($id, $sort, $limit, $offset); |
116 | - if (!$o instanceOf Model) throw new RunTime("Application must return instance of hamleModel"); |
|
117 | + if (!$o instanceOf Model) { |
|
118 | + throw new RunTime("Application must return instance of hamleModel"); |
|
119 | + } |
|
117 | 120 | return $o; |
118 | 121 | } |
119 | 122 |
@@ -44,20 +44,24 @@ discard block |
||
44 | 44 | } |
45 | 45 | $s = preg_split("/-[>!]/", $s); |
46 | 46 | // if(count($s) == 1) $s = explode("-!",$s[0]); |
47 | - if (!$s[0]) throw new ParseError("Unable to parse Complex Expression"); |
|
48 | - if ($s[0][1] == "(") |
|
49 | - $this->func = new Text\Func($s[0]); |
|
50 | - elseif ($s[0][1] == "[") |
|
51 | - $this->func = new Text\Scope($s[0]); |
|
52 | - else |
|
53 | - $this->func = new SimpleVar($s[0]); |
|
47 | + if (!$s[0]) { |
|
48 | + throw new ParseError("Unable to parse Complex Expression"); |
|
49 | + } |
|
50 | + if ($s[0][1] == "(") { |
|
51 | + $this->func = new Text\Func($s[0]); |
|
52 | + } elseif ($s[0][1] == "[") { |
|
53 | + $this->func = new Text\Scope($s[0]); |
|
54 | + } else { |
|
55 | + $this->func = new SimpleVar($s[0]); |
|
56 | + } |
|
54 | 57 | array_shift($s); |
55 | 58 | $this->sel = $s; |
56 | 59 | } |
57 | 60 | |
58 | 61 | function toHTML($escape = false) { |
59 | - if($escape) |
|
60 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
62 | + if($escape) { |
|
63 | + return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
64 | + } |
|
61 | 65 | return "<?=" . $this->toPHP() . "?>"; |
62 | 66 | } |
63 | 67 | function toPHP() { |
@@ -66,18 +70,22 @@ discard block |
||
66 | 70 | function toPHPVar() { |
67 | 71 | if ($this->sel) { |
68 | 72 | $sel = array(); |
69 | - foreach ($this->sel as $s) |
|
70 | - $sel[] = "hamleGet('$s')"; |
|
73 | + foreach ($this->sel as $s) { |
|
74 | + $sel[] = "hamleGet('$s')"; |
|
75 | + } |
|
71 | 76 | return $this->func->toPHP() . "->" . implode('->', $sel); |
72 | - } else |
|
73 | - return $this->func->toPHP(); |
|
77 | + } else { |
|
78 | + return $this->func->toPHP(); |
|
79 | + } |
|
74 | 80 | } |
75 | 81 | |
76 | 82 | function getOrCreateModel(Model $parent = null) { |
77 | - if($this->func instanceof Text\Scope) |
|
78 | - return $this->func->getOrCreateModel($parent); |
|
79 | - if($this->func instanceof Text\Func) |
|
80 | - return $this->func->getOrCreateModel($parent); |
|
83 | + if($this->func instanceof Text\Scope) { |
|
84 | + return $this->func->getOrCreateModel($parent); |
|
85 | + } |
|
86 | + if($this->func instanceof Text\Func) { |
|
87 | + return $this->func->getOrCreateModel($parent); |
|
88 | + } |
|
81 | 89 | } |
82 | 90 | |
83 | 91 | /** |
@@ -85,11 +93,13 @@ discard block |
||
85 | 93 | * @return WriteModel |
86 | 94 | */ |
87 | 95 | function setValue($value) { |
88 | - if(!$this->sel || count($this->sel) != 1) |
|
89 | - throw new \RuntimeException('Can only set values, when one var name is present'); |
|
96 | + if(!$this->sel || count($this->sel) != 1) { |
|
97 | + throw new \RuntimeException('Can only set values, when one var name is present'); |
|
98 | + } |
|
90 | 99 | $model = $this->getOrCreateModel(); |
91 | - if(!$model instanceof WriteModel) |
|
92 | - throw new \RuntimeException('Can only set values on WriteModel, got '.get_class($model)); |
|
100 | + if(!$model instanceof WriteModel) { |
|
101 | + throw new \RuntimeException('Can only set values on WriteModel, got '.get_class($model)); |
|
102 | + } |
|
93 | 103 | $model->hamleSet($this->sel[0], $value); |
94 | 104 | return $model; |
95 | 105 | } |
@@ -39,17 +39,25 @@ discard block |
||
39 | 39 | */ |
40 | 40 | public function __construct($s) { |
41 | 41 | $m = array(); |
42 | - if (!preg_match('/^ +([><]) +('.self::REGEX_FUNCSEL . '+)(.*)$/', $s, $m)) |
|
43 | - throw new ParseError("Unable to read \$ sub func in '$s'"); |
|
44 | - if ($m[1] == "<") $this->dir = Hamle\Hamle::REL_PARENT; |
|
45 | - elseif ($m[1] == ">") $this->dir = Hamle\Hamle::REL_CHILD; |
|
46 | - else $this->dir = Hamle\Hamle::REL_ANY; |
|
42 | + if (!preg_match('/^ +([><]) +('.self::REGEX_FUNCSEL . '+)(.*)$/', $s, $m)) { |
|
43 | + throw new ParseError("Unable to read \$ sub func in '$s'"); |
|
44 | + } |
|
45 | + if ($m[1] == "<") { |
|
46 | + $this->dir = Hamle\Hamle::REL_PARENT; |
|
47 | + } elseif ($m[1] == ">") { |
|
48 | + $this->dir = Hamle\Hamle::REL_CHILD; |
|
49 | + } else { |
|
50 | + $this->dir = Hamle\Hamle::REL_ANY; |
|
51 | + } |
|
47 | 52 | $this->sortlimit = $this->attSortLimit($m[2]); |
48 | 53 | $this->filt = $this->attIdTag($m[2]); |
49 | 54 | $this->grouptype = $this->attGroupType($m[2]); |
50 | - if ($this->filt['id']) throw new ParseError("Unable to select by id"); |
|
51 | - if (trim($m[3])) |
|
52 | - $this->sub = new FuncSub($m[3]); |
|
55 | + if ($this->filt['id']) { |
|
56 | + throw new ParseError("Unable to select by id"); |
|
57 | + } |
|
58 | + if (trim($m[3])) { |
|
59 | + $this->sub = new FuncSub($m[3]); |
|
60 | + } |
|
53 | 61 | } |
54 | 62 | |
55 | 63 | /** |
@@ -69,13 +77,15 @@ discard block |
||
69 | 77 | $model = $parent->hamleRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'], |
70 | 78 | $this->sortlimit['limit'], $this->sortlimit['offset']); |
71 | 79 | if(!$model->valid()) { |
72 | - if(!$parent instanceof Hamle\WriteModel) |
|
73 | - throw new \Exception('Cant create model, ' . get_class($parent) . ' must implement Hamle\\WriteModel.'); |
|
80 | + if(!$parent instanceof Hamle\WriteModel) { |
|
81 | + throw new \Exception('Cant create model, ' . get_class($parent) . ' must implement Hamle\\WriteModel.'); |
|
82 | + } |
|
74 | 83 | $model = $parent->current()->hamleCreateRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'], |
75 | 84 | $this->sortlimit['limit'], $this->sortlimit['offset']); |
76 | 85 | } |
77 | - if($this->sub) |
|
78 | - return $this->sub->getOrCreateModel($model)->current(); |
|
86 | + if($this->sub) { |
|
87 | + return $this->sub->getOrCreateModel($model)->current(); |
|
88 | + } |
|
79 | 89 | return $model->current(); |
80 | 90 | } |
81 | 91 |
@@ -50,10 +50,12 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function __construct($s) { |
52 | 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]); |
|
53 | + if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m)) { |
|
54 | + throw new ParseError("Unable to read \$ func in '$s'"); |
|
55 | + } |
|
56 | + if (trim($m[2])) { |
|
57 | + $this->sub = new FuncSub($m[2]); |
|
58 | + } |
|
57 | 59 | if (!trim($m[1])) { |
58 | 60 | $this->scope = true; |
59 | 61 | return; |
@@ -70,16 +72,23 @@ discard block |
||
70 | 72 | $m = array(); |
71 | 73 | $att = array('id' => array(), 'tag' => array()); |
72 | 74 | 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) |
|
75 | + if (preg_match('/^[a-zA-Z0-9\\_]+/', $str, $m)) { |
|
76 | + $type = $m[0]; |
|
77 | + } else { |
|
78 | + $type = "*"; |
|
79 | + } |
|
80 | + if (preg_match('/#([a-zA-Z0-9\_\\${}]+)/', $str, $m)) { |
|
81 | + $att['id'][$type][] = $m[1]; |
|
82 | + } elseif (preg_match_all('/\\.([a-zA-Z0-9\_\-\\${}]+)/', $str, $m)) { |
|
83 | + foreach ($m[1] as $tag) |
|
78 | 84 | $att['tag'][$type][] = new Text($tag, Text::TOKEN_CODE); |
79 | - else $att['tag'][$type] = array(); |
|
85 | + } else { |
|
86 | + $att['tag'][$type] = array(); |
|
87 | + } |
|
88 | + } |
|
89 | + if (!(count($att['id']) xor count($att['tag']))) { |
|
90 | + throw new ParseError("Only tag, type or id can be combined"); |
|
80 | 91 | } |
81 | - if (!(count($att['id']) xor count($att['tag']))) |
|
82 | - throw new ParseError("Only tag, type or id can be combined"); |
|
83 | 92 | return $att; |
84 | 93 | } |
85 | 94 | |
@@ -92,14 +101,18 @@ discard block |
||
92 | 101 | } |
93 | 102 | $rand = false; |
94 | 103 | if (preg_match_all('/\\^(-?)([a-zA-Z0-9\_]*)/', $s, $m)) { |
95 | - foreach($m[0] as $k=>$mv) |
|
96 | - if ($m[2][$k]) { |
|
104 | + foreach($m[0] as $k=>$mv) { |
|
105 | + if ($m[2][$k]) { |
|
97 | 106 | $dir = $m[1][$k] == "-"?Hamle\Hamle::SORT_DESCENDING:Hamle\Hamle::SORT_ASCENDING; |
107 | + } |
|
98 | 108 | $att['sort'][$m[2][$k]] = $dir; |
99 | - } else $rand = true; |
|
109 | + } else { |
|
110 | + $rand = true; |
|
111 | + } |
|
112 | + } |
|
113 | + if($rand) { |
|
114 | + $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM]; |
|
100 | 115 | } |
101 | - if($rand) |
|
102 | - $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM]; |
|
103 | 116 | return $att; |
104 | 117 | } |
105 | 118 | |
@@ -124,17 +137,19 @@ discard block |
||
124 | 137 | } |
125 | 138 | $limit = Text::varToCode($this->sortlimit['sort']) . "," . |
126 | 139 | $this->sortlimit['limit'] . "," . $this->sortlimit['offset']; |
127 | - if (count($this->filt['tag'])) |
|
128 | - return "Hamle\\Run::modelTypeTags(" . |
|
140 | + if (count($this->filt['tag'])) { |
|
141 | + return "Hamle\\Run::modelTypeTags(" . |
|
129 | 142 | Text::varToCode($this->filt['tag']) . ",$limit)$sub"; |
130 | - if (count($this->filt['id'])) |
|
131 | - if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1) |
|
143 | + } |
|
144 | + if (count($this->filt['id'])) { |
|
145 | + if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1) |
|
132 | 146 | return "Hamle\\Run::modelId(" . |
133 | 147 | Text::varToCode(current($this->filt['id']['*'])) . |
134 | 148 | ",$limit)$sub"; |
135 | - else |
|
136 | - return "Hamle\\Run::modelTypeId(" . |
|
149 | + } else { |
|
150 | + return "Hamle\\Run::modelTypeId(" . |
|
137 | 151 | Text::varToCode($this->filt['id']) . ",$limit)$sub"; |
152 | + } |
|
138 | 153 | return ""; |
139 | 154 | } |
140 | 155 | |
@@ -145,34 +160,39 @@ discard block |
||
145 | 160 | public function getOrCreateModel(Model $parent = null) { |
146 | 161 | if($this->scope instanceof Scope) { |
147 | 162 | $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( |
|
163 | + } elseif ($this->scope === true) { |
|
164 | + $parent = \Seufert\Hamle\Scope::get(0); |
|
165 | + } |
|
166 | + if ($this->filt && count($this->filt['tag'])) { |
|
167 | + $parent = \Seufert\Hamle\Run::modelTypeTags( |
|
152 | 168 | $this->filt['tag'], |
153 | 169 | $this->sortlimit['sort'], |
154 | 170 | $this->sortlimit['limit'], |
155 | 171 | $this->sortlimit['offset'] |
156 | 172 | ); |
157 | - if ($this->filt && count($this->filt['id'])) |
|
158 | - if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1) |
|
173 | + } |
|
174 | + if ($this->filt && count($this->filt['id'])) { |
|
175 | + if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1) |
|
159 | 176 | $parent = \Seufert\Hamle\Run::modelId( |
160 | 177 | current($this->filt['id']['*']), |
161 | 178 | $this->sortlimit['sort'], |
162 | 179 | $this->sortlimit['limit'], |
163 | 180 | $this->sortlimit['offset'] |
164 | 181 | ); |
165 | - else |
|
166 | - $parent = \Seufert\Hamle\Run::modelTypeId( |
|
182 | + } else { |
|
183 | + $parent = \Seufert\Hamle\Run::modelTypeId( |
|
167 | 184 | $this->filt['id'], |
168 | 185 | $this->sortlimit['sort'], |
169 | 186 | $this->sortlimit['limit'], |
170 | 187 | $this->sortlimit['offset'] |
171 | 188 | ); |
172 | - if($this->sub) |
|
173 | - return $this->sub->getOrCreateModel($parent)->current(); |
|
174 | - if(!$parent) |
|
175 | - throw new \RuntimeException('Unable to create model with no relation'); |
|
189 | + } |
|
190 | + if($this->sub) { |
|
191 | + return $this->sub->getOrCreateModel($parent)->current(); |
|
192 | + } |
|
193 | + if(!$parent) { |
|
194 | + throw new \RuntimeException('Unable to create model with no relation'); |
|
195 | + } |
|
176 | 196 | return $parent->current(); |
177 | 197 | } |
178 | 198 |
@@ -44,8 +44,9 @@ discard block |
||
44 | 44 | if(preg_match("/^([a-z]+)(?:\\((?P<vars>.*)\\))?(?:\\|(?P<chained>.+?))?$/", $s, $m)) { |
45 | 45 | $this->filter = $m[1]; |
46 | 46 | $this->vars = isset($m['vars']) && strlen($m['vars']) ? explode(',', $m['vars']) : []; |
47 | - foreach($this->vars as $k=>$v) |
|
48 | - $this->vars[$k] = str_replace(",",',',$v); |
|
47 | + foreach($this->vars as $k=>$v) { |
|
48 | + $this->vars[$k] = str_replace(",",',',$v); |
|
49 | + } |
|
49 | 50 | if(isset($m['chained']) && strlen($m['chained'])) { |
50 | 51 | $this->chained = new Filter($m['chained'],$what); |
51 | 52 | } |
@@ -60,31 +61,36 @@ discard block |
||
60 | 61 | $this->filter = "Seufert\\Hamle\\Text\\Filter::{$this->filter}"; |
61 | 62 | } |
62 | 63 | $mapFilter = ['json'=>'json_encode']; |
63 | - if(isset($mapFilter[$this->filter])) |
|
64 | - $this->filter = $mapFilter[$this->filter]; |
|
64 | + if(isset($mapFilter[$this->filter])) { |
|
65 | + $this->filter = $mapFilter[$this->filter]; |
|
66 | + } |
|
65 | 67 | $this->what = $what; |
66 | 68 | } |
67 | 69 | |
68 | 70 | function toHTML($escape = false) { |
69 | - if($escape) |
|
70 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
71 | + if($escape) { |
|
72 | + return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
73 | + } |
|
71 | 74 | return "<?=" . $this->toPHP() . "?>"; |
72 | 75 | } |
73 | 76 | |
74 | 77 | function toPHPpre() { |
75 | 78 | $pre = ''; |
76 | - if($this->chained) |
|
77 | - $pre = $this->chained->toPHPpre(); |
|
79 | + if($this->chained) { |
|
80 | + $pre = $this->chained->toPHPpre(); |
|
81 | + } |
|
78 | 82 | return "$pre{$this->filter}("; |
79 | 83 | } |
80 | 84 | |
81 | 85 | function toPHPpost() { |
82 | 86 | $post = ''; |
83 | - if($this->chained) |
|
84 | - $post = $this->chained->toPHPpost(); |
|
87 | + if($this->chained) { |
|
88 | + $post = $this->chained->toPHPpost(); |
|
89 | + } |
|
85 | 90 | $o = ''; |
86 | - foreach($this->vars as $v) |
|
87 | - $o .= ','.$this->varToCode($v); |
|
91 | + foreach($this->vars as $v) { |
|
92 | + $o .= ','.$this->varToCode($v); |
|
93 | + } |
|
88 | 94 | return "$o)$post"; |
89 | 95 | } |
90 | 96 | |
@@ -99,8 +105,9 @@ discard block |
||
99 | 105 | static function itersplit($v, $sep = ",") { |
100 | 106 | $o = []; |
101 | 107 | foreach(explode($sep, $v) as $k=>$i) { |
102 | - if($i) |
|
103 | - $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k,'key'=>$k]; |
|
108 | + if($i) { |
|
109 | + $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k,'key'=>$k]; |
|
110 | + } |
|
104 | 111 | } |
105 | 112 | return new WrapArray($o); |
106 | 113 | } |