@@ -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 | |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | protected $filter; |
| 39 | 39 | |
| 40 | 40 | function __construct($s) { |
| 41 | - if(FALSE !== $pos = strpos($s,'|')) { |
|
| 42 | - $this->filter = new Filter(substr($s, $pos+1), $this); |
|
| 43 | - $s = substr($s,0,$pos); |
|
| 41 | + if (FALSE !== $pos = strpos($s, '|')) { |
|
| 42 | + $this->filter = new Filter(substr($s, $pos + 1), $this); |
|
| 43 | + $s = substr($s, 0, $pos); |
|
| 44 | 44 | } |
| 45 | 45 | $s = preg_split("/-[>!]/", $s); |
| 46 | 46 | // if(count($s) == 1) $s = explode("-!",$s[0]); |
@@ -56,27 +56,27 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | function toHTML($escape = false) { |
| 59 | - if($escape) |
|
| 60 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
| 61 | - return "<?=" . $this->toPHP() . "?>"; |
|
| 59 | + if ($escape) |
|
| 60 | + return "<?=htmlspecialchars(".$this->toPHP().")?>"; |
|
| 61 | + return "<?=".$this->toPHP()."?>"; |
|
| 62 | 62 | } |
| 63 | 63 | function toPHP() { |
| 64 | - return $this->filter?$this->filter->toPHP():$this->toPHPVar(); |
|
| 64 | + return $this->filter ? $this->filter->toPHP() : $this->toPHPVar(); |
|
| 65 | 65 | } |
| 66 | 66 | function toPHPVar() { |
| 67 | 67 | if ($this->sel) { |
| 68 | 68 | $sel = array(); |
| 69 | 69 | foreach ($this->sel as $s) |
| 70 | 70 | $sel[] = "hamleGet('$s')"; |
| 71 | - return $this->func->toPHP() . "->" . implode('->', $sel); |
|
| 71 | + return $this->func->toPHP()."->".implode('->', $sel); |
|
| 72 | 72 | } else |
| 73 | 73 | return $this->func->toPHP(); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | function getOrCreateModel(Model $parent = null) { |
| 77 | - if($this->func instanceof Text\Scope) |
|
| 77 | + if ($this->func instanceof Text\Scope) |
|
| 78 | 78 | return $this->func->getOrCreateModel($parent); |
| 79 | - if($this->func instanceof Text\Func) |
|
| 79 | + if ($this->func instanceof Text\Func) |
|
| 80 | 80 | return $this->func->getOrCreateModel($parent); |
| 81 | 81 | } |
| 82 | 82 | |
@@ -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; |
@@ -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,7 +39,7 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function __construct($s) { |
| 41 | 41 | $m = array(); |
| 42 | - if (!preg_match('/^ +([><]) +('.self::REGEX_FUNCSEL . '+)(.*)$/', $s, $m)) |
|
| 42 | + if (!preg_match('/^ +([><]) +('.self::REGEX_FUNCSEL.'+)(.*)$/', $s, $m)) |
|
| 43 | 43 | throw new ParseError("Unable to read \$ sub func in '$s'"); |
| 44 | 44 | if ($m[1] == "<") $this->dir = Hamle\Hamle::REL_PARENT; |
| 45 | 45 | elseif ($m[1] == ">") $this->dir = Hamle\Hamle::REL_CHILD; |
@@ -57,24 +57,24 @@ discard block |
||
| 57 | 57 | * @return string |
| 58 | 58 | */ |
| 59 | 59 | public function toPHP() { |
| 60 | - $limit = Hamle\Text::varToCode($this->sortlimit['sort']) . "," . |
|
| 61 | - $this->sortlimit['limit'] . "," . $this->sortlimit['offset'] . "," . |
|
| 60 | + $limit = Hamle\Text::varToCode($this->sortlimit['sort']).",". |
|
| 61 | + $this->sortlimit['limit'].",".$this->sortlimit['offset'].",". |
|
| 62 | 62 | $this->grouptype['grouptype']; |
| 63 | - $sub = $this->sub ? "->" . $this->sub->toPHP() : ""; |
|
| 64 | - return "hamleRel(" . $this->dir . "," . |
|
| 65 | - Hamle\Text::varToCode($this->filt['tag']) . ",$limit)$sub"; |
|
| 63 | + $sub = $this->sub ? "->".$this->sub->toPHP() : ""; |
|
| 64 | + return "hamleRel(".$this->dir.",". |
|
| 65 | + Hamle\Text::varToCode($this->filt['tag']).",$limit)$sub"; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public function getOrCreateModel(Model $parent = null) { |
| 69 | 69 | $model = $parent->hamleRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'], |
| 70 | 70 | $this->sortlimit['limit'], $this->sortlimit['offset']); |
| 71 | - if(!$model->valid()) { |
|
| 72 | - if(!$parent instanceof Hamle\WriteModel) |
|
| 73 | - throw new \Exception('Cant create model, ' . get_class($parent) . ' must implement Hamle\\WriteModel.'); |
|
| 71 | + if (!$model->valid()) { |
|
| 72 | + if (!$parent instanceof Hamle\WriteModel) |
|
| 73 | + throw new \Exception('Cant create model, '.get_class($parent).' must implement Hamle\\WriteModel.'); |
|
| 74 | 74 | $model = $parent->current()->hamleCreateRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'], |
| 75 | 75 | $this->sortlimit['limit'], $this->sortlimit['offset']); |
| 76 | 76 | } |
| 77 | - if($this->sub) |
|
| 77 | + if ($this->sub) |
|
| 78 | 78 | return $this->sub->getOrCreateModel($model)->current(); |
| 79 | 79 | return $model->current(); |
| 80 | 80 | } |
@@ -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,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 | |
@@ -41,47 +41,47 @@ discard block |
||
| 41 | 41 | protected $chained; |
| 42 | 42 | |
| 43 | 43 | function __construct($s, Text $what) { |
| 44 | - if(preg_match("/^([a-z]+)(?:\\((?P<vars>.*)\\))?(?:\\|(?P<chained>.+?))?$/", $s, $m)) { |
|
| 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); |
|
| 49 | - if(isset($m['chained']) && strlen($m['chained'])) { |
|
| 50 | - $this->chained = new Filter($m['chained'],$what); |
|
| 47 | + foreach ($this->vars as $k=>$v) |
|
| 48 | + $this->vars[$k] = str_replace(",", ',', $v); |
|
| 49 | + if (isset($m['chained']) && strlen($m['chained'])) { |
|
| 50 | + $this->chained = new Filter($m['chained'], $what); |
|
| 51 | 51 | } |
| 52 | 52 | } else { |
| 53 | 53 | throw new ParseError("Unable to parse filter expression \"$s\""); |
| 54 | 54 | } |
| 55 | - if(method_exists(Filter::class, $this->filter)) { |
|
| 55 | + if (method_exists(Filter::class, $this->filter)) { |
|
| 56 | 56 | $this->filter = "Seufert\\Hamle\\Text\\Filter::{$this->filter}"; |
| 57 | - } elseif(!in_array($this->filter, ['round', 'strtoupper', 'strtolower', 'ucfirst', 'json'])) { |
|
| 57 | + } elseif (!in_array($this->filter, ['round', 'strtoupper', 'strtolower', 'ucfirst', 'json'])) { |
|
| 58 | 58 | throw new ParseError("Unknown Filter Type \"{$this->filter}\""); |
| 59 | 59 | } |
| 60 | 60 | $mapFilter = ['json'=>'json_encode']; |
| 61 | - if(isset($mapFilter[$this->filter])) |
|
| 61 | + if (isset($mapFilter[$this->filter])) |
|
| 62 | 62 | $this->filter = $mapFilter[$this->filter]; |
| 63 | 63 | $this->what = $what; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | function toHTML($escape = false) { |
| 67 | - if($escape) |
|
| 68 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
| 69 | - return "<?=" . $this->toPHP() . "?>"; |
|
| 67 | + if ($escape) |
|
| 68 | + return "<?=htmlspecialchars(".$this->toPHP().")?>"; |
|
| 69 | + return "<?=".$this->toPHP()."?>"; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | function toPHPpre() { |
| 73 | 73 | $pre = ''; |
| 74 | - if($this->chained) |
|
| 74 | + if ($this->chained) |
|
| 75 | 75 | $pre = $this->chained->toPHPpre(); |
| 76 | 76 | return "$pre{$this->filter}("; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | function toPHPpost() { |
| 80 | 80 | $post = ''; |
| 81 | - if($this->chained) |
|
| 81 | + if ($this->chained) |
|
| 82 | 82 | $post = $this->chained->toPHPpost(); |
| 83 | 83 | $o = ''; |
| 84 | - foreach($this->vars as $v) |
|
| 84 | + foreach ($this->vars as $v) |
|
| 85 | 85 | $o .= ','.$this->varToCode($v); |
| 86 | 86 | return "$o)$post"; |
| 87 | 87 | } |
@@ -96,24 +96,24 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | static function itersplit($v, $sep = ",") { |
| 98 | 98 | $o = []; |
| 99 | - foreach(explode($sep, $v) as $k=>$i) { |
|
| 100 | - if($i) |
|
| 101 | - $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k,'key'=>$k]; |
|
| 99 | + foreach (explode($sep, $v) as $k=>$i) { |
|
| 100 | + if ($i) |
|
| 101 | + $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k, 'key'=>$k]; |
|
| 102 | 102 | } |
| 103 | 103 | return new WrapArray($o); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | static function newlinebr($v) { |
| 107 | - return str_replace("\n","<br />\n",$v); |
|
| 107 | + return str_replace("\n", "<br />\n", $v); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | static function replace($v, $src, $dst) { |
| 111 | - return str_replace($src,$dst,$v); |
|
| 111 | + return str_replace($src, $dst, $v); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | static function ascents($v) { |
| 115 | - $v = str_replace(['$',' ',','],'', $v); |
|
| 116 | - return (int) round($v * 100,0); |
|
| 115 | + $v = str_replace(['$', ' ', ','], '', $v); |
|
| 116 | + return (int) round($v * 100, 0); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | } |
| 120 | 120 | \ No newline at end of file |
@@ -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 | } |
@@ -58,31 +59,36 @@ discard block |
||
| 58 | 59 | throw new ParseError("Unknown Filter Type \"{$this->filter}\""); |
| 59 | 60 | } |
| 60 | 61 | $mapFilter = ['json'=>'json_encode']; |
| 61 | - if(isset($mapFilter[$this->filter])) |
|
| 62 | - $this->filter = $mapFilter[$this->filter]; |
|
| 62 | + if(isset($mapFilter[$this->filter])) { |
|
| 63 | + $this->filter = $mapFilter[$this->filter]; |
|
| 64 | + } |
|
| 63 | 65 | $this->what = $what; |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | function toHTML($escape = false) { |
| 67 | - if($escape) |
|
| 68 | - return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
| 69 | + if($escape) { |
|
| 70 | + return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
|
| 71 | + } |
|
| 69 | 72 | return "<?=" . $this->toPHP() . "?>"; |
| 70 | 73 | } |
| 71 | 74 | |
| 72 | 75 | function toPHPpre() { |
| 73 | 76 | $pre = ''; |
| 74 | - if($this->chained) |
|
| 75 | - $pre = $this->chained->toPHPpre(); |
|
| 77 | + if($this->chained) { |
|
| 78 | + $pre = $this->chained->toPHPpre(); |
|
| 79 | + } |
|
| 76 | 80 | return "$pre{$this->filter}("; |
| 77 | 81 | } |
| 78 | 82 | |
| 79 | 83 | function toPHPpost() { |
| 80 | 84 | $post = ''; |
| 81 | - if($this->chained) |
|
| 82 | - $post = $this->chained->toPHPpost(); |
|
| 85 | + if($this->chained) { |
|
| 86 | + $post = $this->chained->toPHPpost(); |
|
| 87 | + } |
|
| 83 | 88 | $o = ''; |
| 84 | - foreach($this->vars as $v) |
|
| 85 | - $o .= ','.$this->varToCode($v); |
|
| 89 | + foreach($this->vars as $v) { |
|
| 90 | + $o .= ','.$this->varToCode($v); |
|
| 91 | + } |
|
| 86 | 92 | return "$o)$post"; |
| 87 | 93 | } |
| 88 | 94 | |
@@ -97,8 +103,9 @@ discard block |
||
| 97 | 103 | static function itersplit($v, $sep = ",") { |
| 98 | 104 | $o = []; |
| 99 | 105 | foreach(explode($sep, $v) as $k=>$i) { |
| 100 | - if($i) |
|
| 101 | - $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k,'key'=>$k]; |
|
| 106 | + if($i) { |
|
| 107 | + $o[] = ['v'=>trim($i), 'value'=>trim($i), 'k'=>$k,'key'=>$k]; |
|
| 108 | + } |
|
| 102 | 109 | } |
| 103 | 110 | return new WrapArray($o); |
| 104 | 111 | } |