1: <?php
2: /*
3: * Copyright (c) 2014 @trashtoy
4: * https://github.com/trashtoy/
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a copy of
7: * this software and associated documentation files (the "Software"), to deal in
8: * the Software without restriction, including without limitation the rights to use,
9: * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10: * Software, and to permit persons to whom the Software is furnished to do so,
11: * subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in all
14: * copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22: */
23: /**
24: * PHP class file.
25: * @auhtor trashtoy
26: * @since 2.0.0
27: */
28: namespace Peach\Markup;
29:
30: /**
31: * SGML や XML をマークアップするための必要最低限の書式化ルールを定義した抽象基底クラスです.
32: *
33: * {@link SgmlRenderer} および {@link XmlRenderer}
34: * の共通部分の実装です.
35: */
36: abstract class AbstractRenderer implements Renderer
37: {
38: /**
39: * 開始タグをマークアップします.
40: * この書式化ルールは SGML, XML 共通です.
41: * @param Element $element 要素
42: * @return string 開始タグ ("<tagName attr... >")
43: */
44: public final function formatStartTag(Element $element)
45: {
46: return $this->formatTagPrefix($element) . ">";
47: }
48:
49: /**
50: * 終了タグをマークアップします.
51: * この書式化ルールは SGML, XML 共通です.
52: * @param Element $element 要素
53: * @return string 終了タグ ("</tagName>")
54: */
55: public final function formatEndTag(Element $element)
56: {
57: return "</" . $element->getName() . ">";
58: }
59:
60: /**
61: * 空要素タグをマークアップします.
62: * タグの末尾の書式化方法は各サブクラスに依存します.
63: * @param Element $element 要素
64: * @return string 空要素タグ ("<tagName attr... >" または <tagName attr... />")
65: */
66: public final function formatEmptyTag(Element $element)
67: {
68: return $this->formatTagPrefix($element) . $this->formatEmptyTagSuffix();
69: }
70:
71: /**
72: * 開始タグまたは空要素タグの共通部分を書式化します.
73: * @param Element $element 書式化対象の要素
74: * @return string "<elementName ... "
75: */
76: protected final function formatTagPrefix(Element $element)
77: {
78: $tag = "<";
79: $tag .= $element->getName();
80: foreach ($element->getAttributes() as $name => $value) {
81: $tag .= " ";
82: $tag .= ($value === null) ?
83: $this->formatBooleanAttribute($name) : $this->formatAttribute($name, $value);
84: }
85: return $tag;
86: }
87:
88: /**
89: * 指定された属性を書式化します.
90: *
91: * @param string $name 属性名
92: * @param string $value 属性値
93: * @return string 属性
94: */
95: protected abstract function formatAttribute($name, $value);
96:
97: /**
98: * 値の省略された属性を書式化します.
99: *
100: * @param string $name 属性名
101: * @return string 属性
102: */
103: protected abstract function formatBooleanAttribute($name);
104:
105: /**
106: * 空要素タグの末尾を書式化します.
107: * @return string 空要素の末尾. SGML は ">", XML は " />".
108: */
109: protected abstract function formatEmptyTagSuffix();
110: }
111: