1 | <?php |
||
32 | class SimpleVar extends Text { |
||
33 | protected $var; |
||
34 | |||
35 | protected $filter; |
||
36 | |||
37 | function __construct($s) { |
||
38 | if(FALSE !== $pos = strpos($s,'|')) { |
||
39 | $this->var = substr($s,1,$pos-1); |
||
40 | $this->filter = new Filter(substr($s, $pos+1), $this); |
||
41 | } else { |
||
42 | $this->var = substr($s, 1); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | function toHTML($escape = false) { |
||
47 | if($escape) |
||
48 | return "<?=htmlspecialchars(" .$this->toPHP() . ")?>"; |
||
49 | return "<?=" . $this->toPHP() . "?>"; |
||
50 | } |
||
51 | |||
52 | function toPHP() { |
||
53 | return $this->filter?$this->filter->toPHP():$this->toPHPVar(); |
||
54 | } |
||
55 | |||
56 | function toPHPVar() { |
||
57 | return "Hamle\\Scope::get()->hamleGet(" . Text::varToCode($this->var) . ")"; |
||
58 | } |
||
59 | |||
60 | function getOrCreateModel(Model $parent = null) { |
||
61 | return \Seufert\Hamle\Scope::get(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $value |
||
66 | * @return WriteModel |
||
67 | */ |
||
68 | function setValue($value) { |
||
69 | $model = $this->getOrCreateModel(); |
||
70 | if(!$model instanceof WriteModel) |
||
71 | throw new \RuntimeException('Can only write to model that implements WriteModel'); |
||
72 | $model->hamleSet($this->var, $value); |
||
73 | return $model; |
||
74 | } |
||
75 | |||
76 | } |