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 http\Exception\RuntimeException; |
29
|
|
|
use Seufert\Hamle\Model; |
30
|
|
|
use Seufert\Hamle\Run; |
31
|
|
|
use Seufert\Hamle\Text; |
32
|
|
|
use Seufert\Hamle\Exception\ParseError; |
33
|
|
|
use Seufert\Hamle\WriteModel; |
34
|
|
|
|
35
|
|
|
class Complex extends Text { |
36
|
|
|
protected $func; |
37
|
|
|
protected $sel = null; |
38
|
|
|
protected $filter; |
39
|
|
|
|
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); |
44
|
|
|
} |
45
|
|
|
$s = preg_split("/-[>!]/", $s); |
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]); |
54
|
|
|
array_shift($s); |
55
|
|
|
$this->sel = $s; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function toHTML($escape = false) { |
|
|
|
|
59
|
|
|
if($escape) |
60
|
|
|
return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
61
|
|
|
return "<?=" . $this->toPHP() . "?>"; |
62
|
|
|
} |
63
|
|
|
function toPHP() { |
|
|
|
|
64
|
|
|
return $this->filter?$this->filter->toPHP():$this->toPHPVar(); |
65
|
|
|
} |
66
|
|
|
function toPHPVar() { |
|
|
|
|
67
|
|
|
if ($this->sel) { |
|
|
|
|
68
|
|
|
$sel = array(); |
69
|
|
|
foreach ($this->sel as $s) |
70
|
|
|
$sel[] = "hamleGet('$s')"; |
71
|
|
|
return $this->func->toPHP() . "->" . implode('->', $sel); |
72
|
|
|
} else |
73
|
|
|
return $this->func->toPHP(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
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); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $value |
85
|
|
|
* @return WriteModel |
86
|
|
|
*/ |
87
|
|
|
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'); |
90
|
|
|
$model = $this->getOrCreateModel(); |
91
|
|
|
if(!$model instanceof WriteModel) |
92
|
|
|
throw new \RuntimeException('Can only set values on Runtime Exceptions'); |
93
|
|
|
$model->hamleSet($this->sel[0], $value); |
94
|
|
|
return $model; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.