Skip to content

Commit 7438d30

Browse files
committed
Xml::setOption replaced by Xml::setFlag
1 parent 3241a82 commit 7438d30

File tree

5 files changed

+64
-33
lines changed

5 files changed

+64
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#lib16/xml-builder-php
1+
# lib16/xml-builder-php
22

33
lib16 XML Builder is a PHP 7 library for creating XML documents.
44

src/Adhoc.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ trait Adhoc
2323
*
2424
* @param string $method Start the name of the method with “set” to add an attribute.
2525
* @param array $arguments Expected length is 0 or 1 (content/value).
26-
* @return Xml
2726
*/
28-
public function __call($method, $arguments)
27+
public function __call(string $method, array $arguments): self
2928
{
3029
if (strpos($method, 'set') === 0) {
3130
$method = strtolower(substr($method, 3, strlen($method) - 3));
@@ -38,15 +37,14 @@ public function __call($method, $arguments)
3837
}
3938

4039
/**
41-
* Generates the markup for an XML element.
40+
* Creates an XML element.
4241
*
4342
* @param string $method Name of the XML element.
4443
* @param array $arguments Expected length is 0 or 1 (content).
45-
* @return string
4644
*/
47-
public static function __callstatic($method, $arguments)
45+
public static function __callstatic(string $method, array $arguments): self
4846
{
4947
$content = count($arguments) ? $arguments[0] : null;
50-
return self::createSub($method, $content);
48+
return self::create($method, $content);
5149
}
5250
}

src/Xml.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Xml
3535
protected $root;
3636
protected $parent;
3737

38-
protected $options;
38+
protected $flags;
3939
protected $sub;
4040

4141
/**
@@ -218,9 +218,14 @@ public function addProcessingInstruction(
218218
return $instr;
219219
}
220220

221+
public function disableLineBreak(): self
222+
{
223+
return $this->setFlag('lineBreakDisabled');
224+
}
225+
221226
public function disableTextIndentation(): self
222227
{
223-
return $this->setOption('textIndentationDisabled', true);
228+
return $this->setFlag('textIndentationDisabled');
224229
}
225230

226231
public function attrib(string $name, $value = true): self
@@ -302,10 +307,10 @@ private function isRoot(): bool
302307
return $this->root == $this;
303308
}
304309

305-
private function setOption(string $option, bool $value): self
310+
private function setFlag(string $flagName): self
306311
{
307-
$this->options = $this->options ?? new \stdClass();
308-
$this->options->$option = $value;
312+
$this->flags = $this->flags ?? [];
313+
$this->flags[$flagName] = true;
309314
return $this;
310315
}
311316

@@ -352,19 +357,16 @@ private function buildContainer(\stdClass $v): string
352357
$markup .= $v->whitespace . $this->openingTag($v);
353358
}
354359
if ($this->cdata) {
355-
$markup .= $v->whitespace . self::CDATA_START;
356-
}
357-
if (!empty($v->content)) {
358-
$markup .= $v->whitespace . $v->content;
360+
$markup .= $v->whitespaceCData . self::CDATA_START;
359361
}
360362
foreach ($this->children as $child) {
361363
$markup .= $child->buildMarkup($v);
362364
}
363365
if ($this->cdata) {
364-
$markup .= $v->whitespace . self::CDATA_STOP;
366+
$markup .= $v->whitespaceCData . self::CDATA_STOP;
365367
}
366368
if ($v->hasTags) {
367-
$markup .= $v->whitespace . $this->closingTag($v);
369+
$markup .= $v->whitespaceContainerEnd . $this->closingTag($v);
368370
}
369371
return $markup;
370372
}
@@ -409,26 +411,40 @@ private function standaloneTag(\stdClass $v): string
409411
private function calculateVars(\stdClass $parentVars = null)
410412
{
411413
$vars = new \stdClass();
412-
$vars->hasTags = !empty($this->name);
414+
415+
if (isset($this->flags['textIndentationDisabled'])) {
416+
$vars->textIndentDisabled = true;
417+
}
418+
else if (isset($parentVars)) {
419+
$vars->textIndentDisabled = $parentVars->textIndentDisabled;
420+
}
421+
else {
422+
$vars->textIndentDisabled = false;
423+
}
413424

414425
if (isset($parentVars)) {
415-
$vars->whitespace = $parentVars->whitespace;
416-
if ($this->parent->name != '' && $vars->whitespace != '') {
417-
$vars->whitespace .= self::$indentation;
426+
if (isset($parentVars->lineBreakDisabled)) {
427+
$vars->whitespace = '';
428+
}
429+
else {
430+
$vars->whitespace = $parentVars->whitespace;
431+
if ($this->parent->name != '' && $vars->whitespace != '') {
432+
$vars->whitespace .= self::$indentation;
433+
}
418434
}
419435
}
420436
else {
421437
$vars->whitespace = self::$lineBreak;
422438
}
423439

424-
if (isset($this->options->textIndentationDisabled)) {
425-
$vars->textIndentDisabled = true;
426-
}
427-
else if (isset($parentVars)) {
428-
$vars->textIndentDisabled = $parentVars->textIndentDisabled;
440+
if (isset($this->flags['lineBreakDisabled'])) {
441+
$vars->lineBreakDisabled = true;
442+
$vars->whitespaceContainerEnd = '';
443+
$vars->whitespaceCData = '';
429444
}
430445
else {
431-
$vars->textIndentDisabled = false;
446+
$vars->whitespaceContainerEnd = $vars->whitespace;
447+
$vars->whitespaceCData = $vars->whitespace;
432448
}
433449

434450
if (!empty($this->content) && $vars->whitespace != '' && !$vars->textIndentDisabled) {
@@ -438,12 +454,12 @@ private function calculateVars(\stdClass $parentVars = null)
438454
$vars->content = $this->content;
439455
}
440456

457+
$vars->hasTags = !empty($this->name);
441458
if ($vars->hasTags) {
442-
$vars->attributes = $this->attributes->getMarkup(
443-
static::HTML_MODE_ENABLED,
459+
$vars->attributes = $this->attributes->getMarkup(static::HTML_MODE_ENABLED,
444460
static::VERTICAL_ATTRIBUTES_ENABLED && $vars->whitespace != ''
445-
? $vars->whitespace . self::$indentation . self::$indentation
446-
: ' ');
461+
? $vars->whitespace . self::$indentation . self::$indentation
462+
: ' ');
447463
$vars->name = empty(static::NAMESPACE_PREFIX)
448464
? $this->name
449465
: static::NAMESPACE_PREFIX . ':' . $this->name;

tests/AdhocTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function provider(): array
2424
[Aml::c()->option()->setSelected(), '<option selected>'],
2525
[Aml::c()->option()->setSelected(true), '<option selected>'],
2626
[Aml::c()->option()->setSelected(false), '<option>'],
27-
[Aml::em('lorem') . " ipsum" . Aml::br(), '<em>lorem</em> ipsum<br>']
27+
[Aml::em('lorem') . " ipsum" . Aml::br(), '<em>lorem</em> ipsum<br>'],
28+
[
29+
Aml::article()->setClass('overview')->header()->h1('PHP'),
30+
"<article class=\"overview\">\n\t<header>\n\t\t<h1>PHP</h1>\n\t</header>\n</article>"
31+
]
2832
];
2933
}
3034
}

tests/XmlTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ function() {
261261
"<e>\n\t<f>lorem\n\tipsum\n\tdolor\n\tsit</f>" .
262262
"\n\t<g>lorem\nipsum\ndolor\nsit</g>\n</e>"
263263
],
264+
// disableLineBreak()
265+
[
266+
Tml::c()
267+
->append('e')->append('f')->disableLineBreak()
268+
->append('g')->append('h'),
269+
"<e>\n\t<f><g><h/></g></f>\n</e>"
270+
],
271+
[
272+
Tml::c()
273+
->append('e')->append('f')->disableLineBreak()
274+
->append('g')->cdata()->appendText("lorem ipsum\ndolor sit"),
275+
"<e>\n\t<f><g><![CDATA[lorem ipsum\ndolor sit]]></g></f>\n</e>"
276+
],
264277

265278
// attrib()
266279
[Tml::c('e')->attrib('a', 'foo'), '<e a="foo"/>'],

0 commit comments

Comments
 (0)