@@ -61,8 +61,7 @@ discard block |
||
61 | 61 | if($mode === self::TOKEN_CONTROL) { |
62 | 62 | if(preg_match('/^"(.*)"$/', trim($s), $m)) { |
63 | 63 | $this->nodes[] = new Text($m[1]); |
64 | - } |
|
65 | - else { |
|
64 | + } else { |
|
66 | 65 | $this->nodes[] = new Text\Complex(trim($s)); |
67 | 66 | } |
68 | 67 | return; |
@@ -76,8 +75,7 @@ discard block |
||
76 | 75 | } |
77 | 76 | $this->nodes[] = new Text\Complex(substr($match[2][0], 1, -1)); |
78 | 77 | $pos = $match[2][1] + strlen($match[2][0]); |
79 | - } |
|
80 | - else if($mode & self::FIND_DOLLARVAR) { |
|
78 | + } else if($mode & self::FIND_DOLLARVAR) { |
|
81 | 79 | if($match[1][1] > 0 && $s[$match[1][1] - 1] === '\\') { |
82 | 80 | continue; |
83 | 81 | } |
@@ -54,12 +54,12 @@ discard block |
||
54 | 54 | $pos = 0; |
55 | 55 | $this->nodes = []; |
56 | 56 | $rFlag = PREG_OFFSET_CAPTURE + PREG_SET_ORDER; |
57 | - if(trim($s) === '') { |
|
57 | + if (trim($s) === '') { |
|
58 | 58 | $this->nodes[] = new Text\Plain($s, $mode); |
59 | 59 | return; |
60 | 60 | } |
61 | - if($mode === self::TOKEN_CONTROL) { |
|
62 | - if(preg_match('/^"(.*)"$/', trim($s), $m)) { |
|
61 | + if ($mode === self::TOKEN_CONTROL) { |
|
62 | + if (preg_match('/^"(.*)"$/', trim($s), $m)) { |
|
63 | 63 | $this->nodes[] = new Text($m[1]); |
64 | 64 | } |
65 | 65 | else { |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | return; |
69 | 69 | } |
70 | 70 | preg_match_all(self::REGEX_HTML, $s, $m, $rFlag); |
71 | - foreach($m as $match) { |
|
72 | - if($mode & self::FIND_BARDOLLAR && isset($match[2])) { |
|
73 | - if($match[2][1] != $pos) { |
|
71 | + foreach ($m as $match) { |
|
72 | + if ($mode & self::FIND_BARDOLLAR && isset($match[2])) { |
|
73 | + if ($match[2][1] != $pos) { |
|
74 | 74 | $this->nodes[] = new Text\Plain( |
75 | 75 | substr($s, $pos, $match[2][1] - $pos), $mode); |
76 | 76 | } |
77 | 77 | $this->nodes[] = new Text\Complex(substr($match[2][0], 1, -1)); |
78 | 78 | $pos = $match[2][1] + strlen($match[2][0]); |
79 | 79 | } |
80 | - else if($mode & self::FIND_DOLLARVAR) { |
|
81 | - if($match[1][1] > 0 && $s[$match[1][1] - 1] === '\\') { |
|
80 | + else if ($mode & self::FIND_DOLLARVAR) { |
|
81 | + if ($match[1][1] > 0 && $s[$match[1][1] - 1] === '\\') { |
|
82 | 82 | continue; |
83 | 83 | } |
84 | - if($match[1][1] != $pos) { |
|
84 | + if ($match[1][1] != $pos) { |
|
85 | 85 | $this->nodes[] = new Text\Plain( |
86 | 86 | substr($s, $pos, $match[1][1] - $pos), $mode); |
87 | 87 | } |
@@ -89,14 +89,14 @@ discard block |
||
89 | 89 | $pos = $match[1][1] + strlen($match[1][0]); |
90 | 90 | } |
91 | 91 | } |
92 | - if($pos != strlen($s)) { |
|
92 | + if ($pos != strlen($s)) { |
|
93 | 93 | $this->nodes[] = new Text\Plain(substr($s, $pos), $mode); |
94 | 94 | } |
95 | 95 | } |
96 | 96 | |
97 | 97 | function toHTML($escape = false) { |
98 | 98 | $out = []; |
99 | - foreach($this->nodes as $string) { |
|
99 | + foreach ($this->nodes as $string) { |
|
100 | 100 | $out[] = $string->toHTML($escape); |
101 | 101 | } |
102 | 102 | return implode('', $out); |
@@ -108,34 +108,34 @@ discard block |
||
108 | 108 | |
109 | 109 | function toPHP() { |
110 | 110 | $out = []; |
111 | - foreach($this->nodes as $string) { |
|
111 | + foreach ($this->nodes as $string) { |
|
112 | 112 | $out[] = $string->toPHP(); |
113 | 113 | } |
114 | 114 | return implode(".", $out); |
115 | 115 | } |
116 | 116 | |
117 | 117 | function doEval() { |
118 | - return eval('use Seufert\Hamle; return ' . $this->toPHP() . ';'); |
|
118 | + return eval('use Seufert\Hamle; return '.$this->toPHP().';'); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | static function varToCode($var) { |
122 | - if(is_array($var)) { |
|
122 | + if (is_array($var)) { |
|
123 | 123 | $code = []; |
124 | - foreach($var as $key => $value) { |
|
125 | - $code[] = self::varToCode($key) . '=>' . self::varToCode($value); |
|
124 | + foreach ($var as $key => $value) { |
|
125 | + $code[] = self::varToCode($key).'=>'.self::varToCode($value); |
|
126 | 126 | } |
127 | - return 'array(' . implode(',', $code) . ')'; //remove unnecessary coma |
|
127 | + return 'array('.implode(',', $code).')'; //remove unnecessary coma |
|
128 | 128 | } |
129 | - if(is_bool($var)) { |
|
129 | + if (is_bool($var)) { |
|
130 | 130 | return ($var ? 'TRUE' : 'FALSE'); |
131 | 131 | } |
132 | - if(is_int($var) || is_float($var) || is_numeric($var)) { |
|
132 | + if (is_int($var) || is_float($var) || is_numeric($var)) { |
|
133 | 133 | return $var; |
134 | 134 | } |
135 | - if($var instanceof Text) { |
|
135 | + if ($var instanceof Text) { |
|
136 | 136 | return $var->toPHP(); |
137 | 137 | } |
138 | - return "'" . str_replace(['$', "'"], ['$', "\\'"], $var) . "'"; |
|
138 | + return "'".str_replace(['$', "'"], ['$', "\\'"], $var)."'"; |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -45,7 +45,7 @@ |
||
45 | 45 | |
46 | 46 | static function decodeClassId($s) { |
47 | 47 | $out = $m = array(); |
48 | - if(preg_match('/^[a-zA-Z0-9\_]+/', $s, $m)) |
|
48 | + if (preg_match('/^[a-zA-Z0-9\_]+/', $s, $m)) |
|
49 | 49 | $out['type'] = $m[0]; |
50 | 50 | preg_match_all('/[#\.][a-zA-Z0-9\-\_]+/m', $s, $m); |
51 | 51 | if (isset($m[0])) foreach ($m[0] as $ss) { |
@@ -32,25 +32,33 @@ discard block |
||
32 | 32 | |
33 | 33 | function __construct($params) { |
34 | 34 | parent::__construct(); |
35 | - if (!preg_match('/^(append|content|prepend|replace)(?: (.*))?$/', $params, $m)) |
|
36 | - throw new Hamle\Exception\ParseError("Unable to parse Snippet($params)"); |
|
35 | + if (!preg_match('/^(append|content|prepend|replace)(?: (.*))?$/', $params, $m)) { |
|
36 | + throw new Hamle\Exception\ParseError("Unable to parse Snippet($params)"); |
|
37 | + } |
|
37 | 38 | $this->type = $m[1]; |
38 | - if (isset($m[2])) |
|
39 | - $this->path = explode(" ", $m[2]); |
|
40 | - else |
|
41 | - $this->path = array(); |
|
42 | - foreach ($this->path as $k => $v) |
|
43 | - $this->path[$k] = self::decodeClassId($v); |
|
39 | + if (isset($m[2])) { |
|
40 | + $this->path = explode(" ", $m[2]); |
|
41 | + } else { |
|
42 | + $this->path = array(); |
|
43 | + } |
|
44 | + foreach ($this->path as $k => $v) { |
|
45 | + $this->path[$k] = self::decodeClassId($v); |
|
46 | + } |
|
44 | 47 | } |
45 | 48 | |
46 | 49 | static function decodeClassId($s) { |
47 | 50 | $out = $m = array(); |
48 | - if(preg_match('/^[a-zA-Z0-9\_]+/', $s, $m)) |
|
49 | - $out['type'] = $m[0]; |
|
51 | + if(preg_match('/^[a-zA-Z0-9\_]+/', $s, $m)) { |
|
52 | + $out['type'] = $m[0]; |
|
53 | + } |
|
50 | 54 | preg_match_all('/[#\.][a-zA-Z0-9\-\_]+/m', $s, $m); |
51 | - if (isset($m[0])) foreach ($m[0] as $ss) { |
|
55 | + if (isset($m[0])) { |
|
56 | + foreach ($m[0] as $ss) { |
|
52 | 57 | if ($s[0] == "#") $out['id'] = substr($ss, 1); |
53 | - if ($s[0] == ".") $out['class'][] = substr($ss, 1); |
|
58 | + } |
|
59 | + if ($s[0] == ".") { |
|
60 | + $out['class'][] = substr($ss, 1); |
|
61 | + } |
|
54 | 62 | } |
55 | 63 | return $out; |
56 | 64 | } |
@@ -62,21 +70,24 @@ discard block |
||
62 | 70 | function addSnipContent($contentTag, &$tagArray = array(), $key = 0) { |
63 | 71 | if ($this->type == "content") { |
64 | 72 | $tagArray[$key] = $contentTag; |
65 | - } else |
|
66 | - parent::addSnipContent($contentTag, $tagArray, $key); |
|
73 | + } else { |
|
74 | + parent::addSnipContent($contentTag, $tagArray, $key); |
|
75 | + } |
|
67 | 76 | } |
68 | 77 | |
69 | 78 | function apply(Hamle\Tag $rootTag) { |
70 | 79 | if ($this->type == "append" or $this->type == "prepend") { |
71 | 80 | $matchTags = $rootTag->find($this->path); |
72 | - foreach ($matchTags as $tag) |
|
73 | - foreach ($this->tags as $t) { |
|
81 | + foreach ($matchTags as $tag) { |
|
82 | + foreach ($this->tags as $t) { |
|
74 | 83 | $tag->addChild($t, $this->type); |
84 | + } |
|
75 | 85 | } |
76 | 86 | } elseif ($this->type == "replace") { |
77 | 87 | $rootTag->replace($this->path, $this); |
78 | - } else |
|
79 | - throw new Hamle\Exception\ParseError("Cant Apply snippet to document '{$this->type}'"); |
|
88 | + } else { |
|
89 | + throw new Hamle\Exception\ParseError("Cant Apply snippet to document '{$this->type}'"); |
|
90 | + } |
|
80 | 91 | } |
81 | 92 | |
82 | 93 | } |
83 | 94 | \ No newline at end of file |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function __construct($s) { |
52 | 52 | $m = array(); |
53 | - if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m)) |
|
53 | + if (!preg_match('/^\$\(('.self::REGEX_FUNCSEL.'*)(.*)\)$/', $s, $m)) |
|
54 | 54 | throw new ParseError("Unable to read \$ func in '$s'"); |
55 | 55 | if (trim($m[2])) |
56 | 56 | $this->sub = new FuncSub($m[2]); |
@@ -92,13 +92,13 @@ discard block |
||
92 | 92 | } |
93 | 93 | $rand = false; |
94 | 94 | if (preg_match_all('/\\^(-?)([a-zA-Z0-9_]*)/', $s, $m)) { |
95 | - foreach($m[0] as $k=>$mv) |
|
95 | + foreach ($m[0] as $k=>$mv) |
|
96 | 96 | if ($m[2][$k]) { |
97 | - $dir = $m[1][$k] == "-"?Hamle\Hamle::SORT_DESCENDING:Hamle\Hamle::SORT_ASCENDING; |
|
97 | + $dir = $m[1][$k] == "-" ?Hamle\Hamle::SORT_DESCENDING : Hamle\Hamle::SORT_ASCENDING; |
|
98 | 98 | $att['sort'][$m[2][$k]] = $dir; |
99 | 99 | } else $rand = true; |
100 | 100 | } |
101 | - if($rand) |
|
101 | + if ($rand) |
|
102 | 102 | $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM]; |
103 | 103 | return $att; |
104 | 104 | } |
@@ -116,25 +116,25 @@ discard block |
||
116 | 116 | * @return string PHP Code |
117 | 117 | */ |
118 | 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) { |
|
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 | 123 | return "Hamle\\Scope::get(0)$sub"; |
124 | 124 | } |
125 | - $limit = Text::varToCode($this->sortlimit['sort']) . "," . |
|
126 | - $this->sortlimit['limit'] . "," . $this->sortlimit['offset']; |
|
125 | + $limit = Text::varToCode($this->sortlimit['sort']).",". |
|
126 | + $this->sortlimit['limit'].",".$this->sortlimit['offset']; |
|
127 | 127 | if (count($this->filt['tag'])) |
128 | - return "Hamle\\Run::modelTypeTags(" . |
|
129 | - Text::varToCode($this->filt['tag']) . ",$limit)$sub"; |
|
128 | + return "Hamle\\Run::modelTypeTags(". |
|
129 | + Text::varToCode($this->filt['tag']).",$limit)$sub"; |
|
130 | 130 | if (count($this->filt['id'])) |
131 | 131 | if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1) |
132 | - return "Hamle\\Run::modelId(" . |
|
133 | - Text::varToCode(current($this->filt['id']['*'])) . |
|
132 | + return "Hamle\\Run::modelId(". |
|
133 | + Text::varToCode(current($this->filt['id']['*'])). |
|
134 | 134 | ",$limit)$sub"; |
135 | 135 | else |
136 | - return "Hamle\\Run::modelTypeId(" . |
|
137 | - Text::varToCode($this->filt['id']) . ",$limit)$sub"; |
|
136 | + return "Hamle\\Run::modelTypeId(". |
|
137 | + Text::varToCode($this->filt['id']).",$limit)$sub"; |
|
138 | 138 | return ""; |
139 | 139 | } |
140 | 140 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * @return Model |
144 | 144 | */ |
145 | 145 | public function getOrCreateModel(Model $parent = null) { |
146 | - if($this->scope instanceof Scope) { |
|
146 | + if ($this->scope instanceof Scope) { |
|
147 | 147 | $parent = $this->scope->getOrCreateModel(); |
148 | 148 | } elseif ($this->scope === true) |
149 | 149 | $parent = \Seufert\Hamle\Scope::get(0); |
@@ -169,9 +169,9 @@ discard block |
||
169 | 169 | $this->sortlimit['limit'], |
170 | 170 | $this->sortlimit['offset'] |
171 | 171 | ); |
172 | - if($this->sub) |
|
172 | + if ($this->sub) |
|
173 | 173 | return $this->sub->getOrCreateModel($parent)->current(); |
174 | - if(!$parent) |
|
174 | + if (!$parent) |
|
175 | 175 | throw new \RuntimeException('Unable to create model with no relation'); |
176 | 176 | return $parent->current(); |
177 | 177 | } |
@@ -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 |
@@ -37,9 +37,9 @@ discard block |
||
37 | 37 | protected $filter; |
38 | 38 | |
39 | 39 | function __construct($s) { |
40 | - if(FALSE !== $pos = strpos($s,'|')) { |
|
41 | - $this->filter = new Filter(substr($s, $pos+1), $this); |
|
42 | - $s = substr($s,0,$pos); |
|
40 | + if (FALSE !== $pos = strpos($s, '|')) { |
|
41 | + $this->filter = new Filter(substr($s, $pos + 1), $this); |
|
42 | + $s = substr($s, 0, $pos); |
|
43 | 43 | } |
44 | 44 | $s = preg_split("/-[>!]/", $s); |
45 | 45 | // if(count($s) == 1) $s = explode("-!",$s[0]); |
@@ -55,27 +55,27 @@ discard block |
||
55 | 55 | } |
56 | 56 | |
57 | 57 | function toHTML($escape = false) { |
58 | - if($escape) |
|
59 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
60 | - return "<?=" . $this->toPHP() . "?>"; |
|
58 | + if ($escape) |
|
59 | + return "<?=htmlspecialchars(".$this->toPHP().")?>"; |
|
60 | + return "<?=".$this->toPHP()."?>"; |
|
61 | 61 | } |
62 | 62 | function toPHP() { |
63 | - return $this->filter?$this->filter->toPHP():$this->toPHPVar(); |
|
63 | + return $this->filter ? $this->filter->toPHP() : $this->toPHPVar(); |
|
64 | 64 | } |
65 | 65 | function toPHPVar() { |
66 | 66 | if ($this->sel) { |
67 | 67 | $sel = array(); |
68 | 68 | foreach ($this->sel as $s) |
69 | 69 | $sel[] = "hamleGet('$s')"; |
70 | - return $this->func->toPHP() . "->" . implode('->', $sel); |
|
70 | + return $this->func->toPHP()."->".implode('->', $sel); |
|
71 | 71 | } else |
72 | 72 | return $this->func->toPHP(); |
73 | 73 | } |
74 | 74 | |
75 | 75 | function getOrCreateModel(Model $parent = null) { |
76 | - if($this->func instanceof Text\Scope) |
|
76 | + if ($this->func instanceof Text\Scope) |
|
77 | 77 | return $this->func->getOrCreateModel($parent); |
78 | - if($this->func instanceof Text\Func) |
|
78 | + if ($this->func instanceof Text\Func) |
|
79 | 79 | return $this->func->getOrCreateModel($parent); |
80 | 80 | throw new RuntimeException('Unsupported func type encountered:'.get_class($this->func)); |
81 | 81 | } |
@@ -85,10 +85,10 @@ discard block |
||
85 | 85 | * @return WriteModel |
86 | 86 | */ |
87 | 87 | function setValue($value) { |
88 | - if(!$this->sel || count($this->sel) != 1) |
|
88 | + if (!$this->sel || count($this->sel) != 1) |
|
89 | 89 | throw new RuntimeException('Can only set values, when one var name is present'); |
90 | 90 | $model = $this->getOrCreateModel(); |
91 | - if(!$model instanceof WriteModel) |
|
91 | + if (!$model instanceof WriteModel) |
|
92 | 92 | throw new RuntimeException('Can only set values on WriteModel, got '.get_class($model)); |
93 | 93 | $model->hamleSet($this->sel[0], $value); |
94 | 94 | return $model; |
@@ -43,20 +43,24 @@ discard block |
||
43 | 43 | } |
44 | 44 | $s = preg_split("/-[>!]/", $s); |
45 | 45 | // if(count($s) == 1) $s = explode("-!",$s[0]); |
46 | - if (!$s[0]) throw new ParseError("Unable to parse Complex Expression"); |
|
47 | - if ($s[0][1] === '(') |
|
48 | - $this->func = new Text\Func($s[0]); |
|
49 | - elseif ($s[0][1] === '[') |
|
50 | - $this->func = new Text\Scope($s[0]); |
|
51 | - else |
|
52 | - $this->func = new SimpleVar($s[0]); |
|
46 | + if (!$s[0]) { |
|
47 | + throw new ParseError("Unable to parse Complex Expression"); |
|
48 | + } |
|
49 | + if ($s[0][1] === '(') { |
|
50 | + $this->func = new Text\Func($s[0]); |
|
51 | + } elseif ($s[0][1] === '[') { |
|
52 | + $this->func = new Text\Scope($s[0]); |
|
53 | + } else { |
|
54 | + $this->func = new SimpleVar($s[0]); |
|
55 | + } |
|
53 | 56 | array_shift($s); |
54 | 57 | $this->sel = $s; |
55 | 58 | } |
56 | 59 | |
57 | 60 | function toHTML($escape = false) { |
58 | - if($escape) |
|
59 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
61 | + if($escape) { |
|
62 | + return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
63 | + } |
|
60 | 64 | return "<?=" . $this->toPHP() . "?>"; |
61 | 65 | } |
62 | 66 | function toPHP() { |
@@ -65,18 +69,22 @@ discard block |
||
65 | 69 | function toPHPVar() { |
66 | 70 | if ($this->sel) { |
67 | 71 | $sel = array(); |
68 | - foreach ($this->sel as $s) |
|
69 | - $sel[] = "hamleGet('$s')"; |
|
72 | + foreach ($this->sel as $s) { |
|
73 | + $sel[] = "hamleGet('$s')"; |
|
74 | + } |
|
70 | 75 | return $this->func->toPHP() . "->" . implode('->', $sel); |
71 | - } else |
|
72 | - return $this->func->toPHP(); |
|
76 | + } else { |
|
77 | + return $this->func->toPHP(); |
|
78 | + } |
|
73 | 79 | } |
74 | 80 | |
75 | 81 | function getOrCreateModel(Model $parent = null) { |
76 | - if($this->func instanceof Text\Scope) |
|
77 | - return $this->func->getOrCreateModel($parent); |
|
78 | - if($this->func instanceof Text\Func) |
|
79 | - return $this->func->getOrCreateModel($parent); |
|
82 | + if($this->func instanceof Text\Scope) { |
|
83 | + return $this->func->getOrCreateModel($parent); |
|
84 | + } |
|
85 | + if($this->func instanceof Text\Func) { |
|
86 | + return $this->func->getOrCreateModel($parent); |
|
87 | + } |
|
80 | 88 | throw new RuntimeException('Unsupported func type encountered:'.get_class($this->func)); |
81 | 89 | } |
82 | 90 | |
@@ -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 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | $i = self::indentLevel($indent); |
157 | 157 | unset($m[0]); |
158 | 158 | switch (strlen($code) ? $code[0] : ($textcode ? $textcode : "")) { |
159 | - case "|": //Control Tag |
|
159 | + case "|" : //Control Tag |
|
160 | 160 | if ($code == "|snippet") |
161 | 161 | $hTag = new Tag\Snippet($text); |
162 | 162 | elseif ($code == "|form") |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | break; |
192 | 192 | default: |
193 | 193 | $attr = array(); |
194 | - if(isset($params[0]) && $params[0] == "[") { |
|
194 | + if (isset($params[0]) && $params[0] == "[") { |
|
195 | 195 | $param = substr($params, 1, -1); |
196 | 196 | $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
197 | 197 | // parse_str($param, $attr); |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | if ($s[0] == ".") $class[] = substr($s, 1); |
205 | 205 | if ($s[0] == "!") $ref = substr($s, 1); |
206 | 206 | } |
207 | - if($ref) |
|
207 | + if ($ref) |
|
208 | 208 | $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
209 | 209 | else |
210 | 210 | $hTag = new Tag\Html($tag, $class, $attr, $id); |
@@ -217,16 +217,16 @@ discard block |
||
217 | 217 | else |
218 | 218 | $this->root[] = $hTag; |
219 | 219 | } else |
220 | - throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/" . preg_last_error()); |
|
220 | + throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/".preg_last_error()); |
|
221 | 221 | $this->lineNo++; |
222 | 222 | } |
223 | 223 | } |
224 | 224 | |
225 | 225 | function parseQueryString($qs) { |
226 | 226 | $out = []; |
227 | - foreach(explode('&',$qs) as $s) { |
|
228 | - $kv = explode('=',$s,2); |
|
229 | - $out[urldecode($kv[0])] = isset($kv[1])?urldecode($kv[1]):null; |
|
227 | + foreach (explode('&', $qs) as $s) { |
|
228 | + $kv = explode('=', $s, 2); |
|
229 | + $out[urldecode($kv[0])] = isset($kv[1]) ?urldecode($kv[1]) : null; |
|
230 | 230 | } |
231 | 231 | return $out; |
232 | 232 | } |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | $m = array(); |
245 | 245 | while ($this->lineNo + 1 < $this->lineCount && |
246 | 246 | (!trim($this->lines[$this->lineNo + 1]) || |
247 | - preg_match('/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
247 | + preg_match('/^(\s){'.$indent.'}((\s)+[^\s].*)$/', |
|
248 | 248 | $this->lines[$this->lineNo + 1], $m))) { |
249 | 249 | if (trim($this->lines[$this->lineNo + 1])) |
250 | 250 | $out[] = $m[2]; |
@@ -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 | |
@@ -143,9 +146,11 @@ discard block |
||
143 | 146 | $heir = array(); |
144 | 147 | while ($this->lineNo < $this->lineCount) { |
145 | 148 | $line = $this->lines[$this->lineNo]; |
146 | - if (trim($line)) if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
149 | + if (trim($line)) { |
|
150 | + if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
147 | 151 | if (FALSE !== strpos($m[1], "\t")) |
148 | - throw new ParseError("Tabs are not supported in templates at this time"); |
|
152 | + throw new ParseError("Tabs are not supported in templates at this time"); |
|
153 | + } |
|
149 | 154 | $indent = strlen($m[1]); |
150 | 155 | $tag = isset($m[2]) ? $tag = $m[2] : ""; |
151 | 156 | $classid = isset($m[3]) ? $m[3] : ""; |
@@ -157,13 +162,13 @@ discard block |
||
157 | 162 | unset($m[0]); |
158 | 163 | switch (strlen($code) ? $code[0] : ($textcode ? $textcode : "")) { |
159 | 164 | case "|": //Control Tag |
160 | - if ($code == "|snippet") |
|
161 | - $hTag = new Tag\Snippet($text); |
|
162 | - elseif ($code == "|form") |
|
163 | - $hTag = new Tag\Form($text); |
|
164 | - elseif ($code == "|formhint") |
|
165 | - $hTag = new Tag\FormHint(); |
|
166 | - elseif ($code == "|else") { |
|
165 | + if ($code == "|snippet") { |
|
166 | + $hTag = new Tag\Snippet($text); |
|
167 | + } elseif ($code == "|form") { |
|
168 | + $hTag = new Tag\Form($text); |
|
169 | + } elseif ($code == "|formhint") { |
|
170 | + $hTag = new Tag\FormHint(); |
|
171 | + } elseif ($code == "|else") { |
|
167 | 172 | $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
168 | 173 | $hTag->setVar($text); |
169 | 174 | } else { |
@@ -174,8 +179,9 @@ discard block |
||
174 | 179 | case ":": //Filter Tag |
175 | 180 | $hTag = new Tag\Filter(substr($code, 1)); |
176 | 181 | $hTag->addContent($text, Text::TOKEN_CODE); |
177 | - foreach ($this->consumeBlock($indent) as $l) |
|
178 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
182 | + foreach ($this->consumeBlock($indent) as $l) { |
|
183 | + $hTag->addContent($l, Text::TOKEN_CODE); |
|
184 | + } |
|
179 | 185 | break; |
180 | 186 | case "_": //String Tag |
181 | 187 | case "__": //Unescape String Tag |
@@ -186,8 +192,9 @@ discard block |
||
186 | 192 | case "//": // Non Printed Comment |
187 | 193 | $hTag = new Tag\Comment($textcode); |
188 | 194 | $hTag->addContent($text); |
189 | - foreach ($this->consumeBlock($indent) as $l) |
|
190 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
195 | + foreach ($this->consumeBlock($indent) as $l) { |
|
196 | + $hTag->addContent($l, Text::TOKEN_CODE); |
|
197 | + } |
|
191 | 198 | break; |
192 | 199 | default: |
193 | 200 | $attr = array(); |
@@ -199,25 +206,34 @@ discard block |
||
199 | 206 | } |
200 | 207 | $class = array(); $id = ""; $ref = ""; |
201 | 208 | preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
202 | - if (isset($cid[0])) foreach ($cid[0] as $s) { |
|
203 | - if ($s[0] == "#") $id = substr($s, 1); |
|
204 | - if ($s[0] == ".") $class[] = substr($s, 1); |
|
205 | - if ($s[0] == "!") $ref = substr($s, 1); |
|
209 | + if (isset($cid[0])) { |
|
210 | + foreach ($cid[0] as $s) { |
|
211 | + if ($s[0] == "#") $id = substr($s, 1); |
|
212 | + } |
|
213 | + if ($s[0] == ".") { |
|
214 | + $class[] = substr($s, 1); |
|
215 | + } |
|
216 | + if ($s[0] == "!") { |
|
217 | + $ref = substr($s, 1); |
|
218 | + } |
|
206 | 219 | } |
207 | - if($ref) |
|
208 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
209 | - else |
|
210 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
220 | + if($ref) { |
|
221 | + $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
222 | + } else { |
|
223 | + $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
224 | + } |
|
211 | 225 | $hTag->addContent($text); |
212 | 226 | break; |
213 | 227 | } |
214 | 228 | $heir[$i] = $hTag; |
215 | - if ($i > 0) |
|
216 | - $heir[$i - 1]->addChild($hTag); |
|
217 | - else |
|
218 | - $this->root[] = $hTag; |
|
219 | - } else |
|
220 | - throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/" . preg_last_error()); |
|
229 | + if ($i > 0) { |
|
230 | + $heir[$i - 1]->addChild($hTag); |
|
231 | + } else { |
|
232 | + $this->root[] = $hTag; |
|
233 | + } |
|
234 | + } else { |
|
235 | + throw new ParseError("Unable to parse line {$this->lineNo}\n\"$line\"/" . preg_last_error()); |
|
236 | + } |
|
221 | 237 | $this->lineNo++; |
222 | 238 | } |
223 | 239 | } |
@@ -233,8 +249,9 @@ discard block |
||
233 | 249 | |
234 | 250 | function output($minify = false) { |
235 | 251 | $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
236 | - foreach ($this->root as $tag) |
|
237 | - $out .= $tag->render(0, $minify); |
|
252 | + foreach ($this->root as $tag) { |
|
253 | + $out .= $tag->render(0, $minify); |
|
254 | + } |
|
238 | 255 | return $out; |
239 | 256 | |
240 | 257 | } |
@@ -246,15 +263,18 @@ discard block |
||
246 | 263 | (!trim($this->lines[$this->lineNo + 1]) || |
247 | 264 | preg_match('/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
248 | 265 | $this->lines[$this->lineNo + 1], $m))) { |
249 | - if (trim($this->lines[$this->lineNo + 1])) |
|
250 | - $out[] = $m[2]; |
|
266 | + if (trim($this->lines[$this->lineNo + 1])) { |
|
267 | + $out[] = $m[2]; |
|
268 | + } |
|
251 | 269 | $this->lineNo++; |
252 | 270 | } |
253 | 271 | return $out; |
254 | 272 | } |
255 | 273 | |
256 | 274 | function indentLevel($indent) { |
257 | - if (!isset($this->indents)) $this->indents = array(); |
|
275 | + if (!isset($this->indents)) { |
|
276 | + $this->indents = array(); |
|
277 | + } |
|
258 | 278 | if (!count($this->indents)) { |
259 | 279 | $this->indents = array(0 => $indent); |
260 | 280 | // Key = indent level, Value = Depth in spaces |