Passed
Push — master ( 17602b...453007 )
by Chris
04:37
created

Text::varToCode()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 1
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;
27
28
/**
29
 * HAMLE String Conversion Library
30
 *
31
 * @author Chris Seufert <[email protected]>
32
 * @package hamle
33
 */
34
class Text {
35
  const TOKEN_CONTROL = 0x07;
36
  const TOKEN_HTML = 0x06;
37
  const TOKEN_CODE = 0x04;
38
39
  const REGEX_HTML = '/(\\$[a-zA-Z_][a-zA-Z0-9_]*)|({\\$.*?})/';
40
  const REGEX_CODE = '//';
41
42
  const FIND_DOLLARFUNC = 0x01;
43
  const FIND_DOLLARVAR = 0x02;
44
  const FIND_BARDOLLAR = 0x04;
45
46
  /**
47
   * @var \Seufert\Hamle\Text[] Array of Child String Objects
48
   */
49
  protected $nodes;
50
51
  function __construct($s, $mode = self::TOKEN_HTML) {
52
    $m = array();
53
    $pos = 0;
54
    $this->nodes = array();
55
    $rFlag = PREG_OFFSET_CAPTURE + PREG_SET_ORDER;
56
    if (strlen(trim($s)) == 0) return;
57
    if ($mode == self::TOKEN_CONTROL) {
58
      if (preg_match('/^"(.*)"$/', trim($s), $m)) {
59
        $this->nodes[] = new Text($m[1]);
60
      } else
61
        $this->nodes[] = new Text\Complex(trim($s));
62
      return;
63
    }
64
    preg_match_all(self::REGEX_HTML, $s, $m, $rFlag);
65
    foreach ($m as $match) {
66
      if ($mode & self::FIND_BARDOLLAR && isset($match[2])) {
67
        if ($match[2][1] != $pos)
68
          $this->nodes[] = new Text\Plain(
69
              substr($s, $pos, $match[2][1] - $pos), $mode);
70
        $this->nodes[] = new Text\Complex(substr($match[2][0], 1, -1));
71
        $pos = $match[2][1] + strlen($match[2][0]);
72
      } elseif ($mode & self::FIND_DOLLARVAR) {
73
        if ($match[1][1] > 0 && $s[$match[1][1] - 1] == '\\') continue;
74
        if ($match[1][1] != $pos)
75
          $this->nodes[] = new Text\Plain(
76
              substr($s, $pos, $match[1][1] - $pos), $mode);
77
        $this->nodes[] = new Text\SimpleVar($match[1][0]);
78
        $pos = $match[1][1] + strlen($match[1][0]);
79
      }
80
    }
81
    if ($pos != strlen($s))
82
      $this->nodes[] = new Text\Plain(substr($s, $pos), $mode);
83
  }
84
85
  function toHTML($escape = false) {
86
    $out = array();
87
    foreach ($this->nodes as $string)
88
      $out[] = $string->toHTML($escape);
89
    return implode("", $out);
90
  }
91
92
  function toHTMLAtt() {
93
    return $this->toHtml(true);
94
  }
95
96
  function toPHP() {
97
    $out = array();
98
    foreach ($this->nodes as $string)
99
      $out[] = $string->toPHP();
100
    return implode(".", $out);
101
  }
102
103
  function doEval() {
104
    return eval('use Seufert\Hamle; return ' . $this->toPHP() . ';');
105
  }
106
107
  static function varToCode($var) {
108
    if (is_array($var)) {
109
      $code = array();
110
      foreach ($var as $key => $value)
111
        $code[] = self::varToCode($key) . "=>" . self::varToCode($value);
112
      return 'array(' . implode(",", $code) . ')'; //remove unnecessary coma
113
    } elseif (is_bool($var)) {
114
      return ($var ? 'TRUE' : 'FALSE');
115
    } elseif (is_int($var) || is_float($var) || is_numeric($var)) {
116
      return $var;
117
    } elseif ($var instanceof Text) {
118
      return $var->toPHP();
119
    } else {
120
      return "'" . str_replace(array('$', "'"), array('\\$', "\\'"), $var) . "'";
121
    }
122
  }
123
124
  /**
125
   * @param $value
126
   * @return WriteModel
127
   */
128
  function setValue($value) {
129
    throw new \RuntimeException('Unsupported');
130
  }
131
132
}
133
134