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: * デフォルトの改行ルールです.
32: */
33: class DefaultBreakControl implements BreakControl
34: {
35: /**
36: * 外部からインスタンス化できません.
37: */
38: private function __construct() {}
39:
40: /**
41: * 指定された要素の開始タグの後ろに改行を付けるかどうかを決定します.
42: * 条件は以下の通りです.
43: *
44: * - もしも指定された要素に子要素がない場合は改行なし
45: * - 子要素を一つだけ含み, それが整形済テキストの場合は改行あり
46: * - 子要素を一つだけ含み, それがコンテナ要素の場合, 再帰的にチェックした結果
47: * - 子要素を一つだけ含み, 上記以外のノードの場合は改行なし
48: * - 子要素が二つ以上の場合は改行あり
49: *
50: * @param ContainerElement $node
51: * @return bool
52: */
53: public function breaks(ContainerElement $node)
54: {
55: $size = $node->size();
56: switch ($size) {
57: case 0:
58: return false;
59: case 1:
60: $childNodes = $node->getChildNodes();
61: $child = $childNodes[0];
62: if ($child instanceof Code) {
63: return true;
64: }
65: if ($child instanceof ContainerElement) {
66: return $this->breaks($child);
67: }
68: return false;
69: default:
70: return true;
71: }
72: }
73:
74: /**
75: * 唯一のインスタンスを取得します.
76: *
77: * @return DefaultBreakControl
78: * @codeCoverageIgnore
79: */
80: public static function getInstance()
81: {
82: static $instance = null;
83: if ($instance === null) {
84: $instance = new self();
85: }
86: return $instance;
87: }
88: }
89: