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

Tag::render()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 8.2111
c 0
b 0
f 0
cc 8
nc 128
nop 2
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
/**
30
 * HAMLE Tag object
31
 *
32
 * @author Chris Seufert <[email protected]>
33
 * @package hamle
34
 */
35
class Tag {
36
  /**
37
   * @var Tag[] Array of children tag elements
38
   */
39
  public $tags = array();
40
  /**
41
   * @var string Tag Type for Printable Tags
42
   */
43
  public $type;
44
  /**
45
   * @var array Array of lines of Content
46
   */
47
  public $content;
48
49
  public $opt;
50
51
  public $source;
52
  /**
53
   * Number of spaces for each Indent when doing pretty format of output
54
   */
55
  const INDENT_SIZE = 2;
56
57
  /**
58
   * Initialize instance vars
59
   */
60
  function __construct() {
61
    $this->tags = array();
62
    $this->content = array();
63
  }
64
65
  /**
66
   * @param array $path array of arrays[type/class/id]
67
   * @return Tag[] The tags you are looking for
68
   */
69
  function find($path) {
70
    //var_dump($this->type, json_encode($path), $this->compare($path[0]));
71
    $list = array();
72
    if ($this->compare($path[0])) {
73
      if (count($path) == 1) {
74
        $list[] = $this;
75
        return $list;
76
      }
77
      array_shift($path);
78
    }
79
    foreach ($this->tags as $tag)
80
      if ($found = $tag->find($path))
81
        $list = array_merge($list, $found);
82
    return $list;
83
  }
84
85
  /**
86
   * Replace a tag at $path with a new tag ($newTag)
87
   * @param $path array Path Array
88
   * @param $newTag Tag New tag to replace old tag with
89
   * @return Tag
90
   */
91
  function replace($path, Tag $newTag) {
92
    if ($this->compare($path[0])) {
93
      if (count($path) == 1) return $newTag;
94
      array_shift($path);
95
    }
96
    foreach ($this->tags as $k => $tag) {
97
      if ($r = $tag->replace($path, $newTag)) {
98
        $r->addSnipContent($this->tags[$k]);
99
        array_splice($this->tags, $k, 1, $r->tags);
100
      }
101
    }
102
    return null;
103
  }
104
105
  function addSnipContent($contentTag, &$tagArray = array(), $key = 0) {
106
    foreach ($this->tags as $k => $tag)
107
      $tag->addSnipContent($contentTag, $this->tags, $k);
108
  }
109
110
  function compare($tic) {
111
    if (isset($tic['type']) && $this->type != $tic['type'])
112
      return false;
113
    if (isset($tic['id']) &&
114
        !(isset($this->opt['id']) && $tic['id'] == $this->opt['id'])
115
    )
116
      return false;
117
    if (isset($tic['class']) && !(isset($this->opt['class'])
118
            && !array_diff($tic['class'], $this->opt['class']))
119
    )
120
      return false;
121
    return true;
122
  }
123
124
  /**
125
   * Add a child tag to this tag
126
   * @param Tag $tag Tag to add as child
127
   * @param string $mode Mode to add child [append|prepend]
128
   */
129
  function addChild(Tag $tag, $mode = "append") {
130
    if ($mode == "prepend")
131
      array_unshift($this->tags, $tag);
132
    else
133
      $this->tags[] = $tag;
134
  }
135
136
  /**
137
   * Render html/php output to disk
138
   *
139
   * @param int $indent Number of spaces in current indent level
140
   * @param boolean $doIndent Indent this tag
141
   * @return string HTML/PHP Output
142
   */
143
  function render($indent = 0, $doIndent = true) {
144
    //if(strtoupper($this->type) == "A") var_dump($this);
145
    $ind = $doIndent ? str_pad("", $indent, " ") : "";
146
    $oneliner = ((count($this->content) > 1 || $this->tags) ? false : true);
147
    $out = $ind . $this->renderStTag() . ($oneliner ? "" : "\n");
148
    if ($this->content) $out .= $this->renderContent($ind, $oneliner);
149
    foreach ($this->tags as $tag)
150
      $out .= $tag->render($indent + self::INDENT_SIZE);
151
    $out .= ($oneliner ? "" : $ind) . $this->renderEnTag() . "\n";
152
    return $out;
153
  }
154
155
  /**
156
   * Apply indent, to content, and return as string
157
   *
158
   * @param string $pad Indent String
159
   * @param bool $oneliner Render to fit single line
160
   * @return string Indented Content
161
   */
162
  function renderContent($pad = "", $oneliner = false) {
163
    $out = "";
164
    foreach ($this->content as $c)
165
      $out .= ($oneliner ? "" : $pad) . $c . ($oneliner ? "" : "\n");
166
    return $out;
167
  }
168
169
  /**
170
   * Output the Start Tag for this element
171
   */
172
  function renderStTag() {
173
  }
174
175
  /**
176
   * Output the End Tag for this element
177
   */
178
  function renderEnTag() {
179
  }
180
181
  /**
182
   * Add content to this tag, one line at a time
183
   *
184
   * @param string $s One line of content
185
   * @param int $strtype Type of string to parse, hamleString::TOKEN_*
186
   */
187
  function addContent($s, $strtype = Text::TOKEN_HTML) {
188
    if (trim($s)) {
189
      $parse = new Text($s, $strtype);
190
      $this->content[] = $parse->toHTML();
191
    }
192
  }
193
194
}
195
196
197
198
199
200
201
202