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: * 開始タグの名前をもとに, 改行するかどうかを判定する BreakControl です.
32: */
33: class NameBreakControl extends BreakControlWrapper
34: {
35: /**
36: * 常に改行する要素名の一覧
37: * @var array
38: */
39: private $breakNames;
40:
41: /**
42: * 常に改行しない要素名の一覧
43: * @var array
44: */
45: private $noBreakNames;
46:
47: /**
48: * 指定された要素の改行ルールを強制する NameBreakControl インスタンスを生成します。
49: *
50: * @param array $breakNames 強制的に改行する要素名
51: * @param array $noBreakNames 強制的に改行しない要素名
52: * @param BreakControl $original オリジナルの BreakControl
53: */
54: public function __construct(array $breakNames, array $noBreakNames, BreakControl $original = null)
55: {
56: parent::__construct($original);
57: $this->breakNames = $breakNames;
58: $this->noBreakNames = $noBreakNames;
59: }
60:
61: /**
62: * 強制的に改行する (または強制的に改行しない) 要素名のリストをもとに,
63: * 指定された要素を改行するかどうかを決定します.
64: * 改行リスト・非改行リストの両方に含まれている要素名の場合は,
65: * 改行リストのほうが優先されます. (つまり常に改行されます)
66: *
67: * 改行リスト・非改行リストのいずれにも含まれない場合は,
68: * オリジナルの BreakControl の結果を返します.
69: *
70: * @param ContainerElement $node
71: * @return bool
72: */
73: public function breaks(ContainerElement $node)
74: {
75: $name = $node->getName();
76: if (in_array($name, $this->breakNames)) {
77: return true;
78: }
79: if (in_array($name, $this->noBreakNames)) {
80: return false;
81: }
82:
83: return parent::breaks($node);
84: }
85: }
86: