Skip to content

Commit d189ea5

Browse files
authored
Merge pull request #18 from 8fold/elemental
Deprecate: setVersion, setEncoding, setStandalone
2 parents 69800b7 + 1bb67d4 commit d189ea5

File tree

7 files changed

+88
-41
lines changed

7 files changed

+88
-41
lines changed

src/Callables/PropertyArrayToString.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function __invoke(string ...$properties): string
1717
foreach ($properties as $property) {
1818
$property = explode(' ', $property, 2);
1919
$b[] = $property[0] . '="' . $property[1] . '"';
20-
2120
}
2221

2322
return ' ' . implode(' ', $b);

src/Concatenate.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,17 @@
44
namespace Eightfold\XMLBuilder;
55

66
use Eightfold\XMLBuilder\Contracts\Buildable;
7+
use Eightfold\XMLBuilder\Contracts\ContentWithoutElement;
78

89
use Stringable;
910

1011
use Eightfold\XMLBuilder\Implementations\Buildable as BuildableImp;
12+
use Eightfold\XMLBuilder\Implementations\ContentWithoutElement as ContentWithoutElementImp;
1113

12-
class Concatenate implements Buildable
14+
class Concatenate implements Buildable, ContentWithoutElement
1315
{
1416
use BuildableImp;
15-
16-
/**
17-
* @var array<string|Stringable>
18-
*/
19-
private array $content = [];
20-
21-
public static function create(string|Stringable ...$content): self
22-
{
23-
return new self(...$content);
24-
}
25-
26-
final private function __construct(string|Stringable ...$content)
27-
{
28-
$this->content = $content;
29-
}
17+
use ContentWithoutElementImp;
3018

3119
public function __toString(): string
3220
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Eightfold\XMLBuilder\Contracts;
4+
5+
use Stringable;
6+
7+
interface ContentWithoutElement
8+
{
9+
public static function create(string|Stringable ...$content): self;
10+
}

src/Document.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Eightfold\XMLBuilder;
66

7+
use Eightfold\XMLBuilder\Contracts\Buildable;
8+
use Eightfold\XMLBuilder\Contracts\Contentable;
9+
710
use Stringable;
811

9-
use Eightfold\XMLBuilder\Callables\PropertyArrayToString;
12+
use Eightfold\XMLBuilder\Concatenate;
1013

11-
use Eightfold\XMLBuilder\Contracts\Buildable;
12-
use Eightfold\XMLBuilder\Contracts\Contentable;
14+
use Eightfold\XMLBuilder\Callables\PropertyArrayToString;
1315

1416
use Eightfold\XMLBuilder\Implementations\Properties as PropertiesImp;
1517
use Eightfold\XMLBuilder\Implementations\Buildable as BuildableImp;
@@ -27,14 +29,7 @@ class Document implements Buildable, Contentable
2729

2830
private string $standalone = 'yes';
2931

30-
final private function __construct(
31-
private string $name,
32-
string|Stringable ...$content
33-
) {
34-
$this->content = $content;
35-
}
36-
37-
public function setVersion(string|float|int $version): self
32+
public function withVersion(string|float|int $version): self
3833
{
3934
if (is_int($version)) {
4035
$version = strval($version) . '.0';
@@ -43,24 +38,48 @@ public function setVersion(string|float|int $version): self
4338
return $this;
4439
}
4540

46-
public function setEncoding(string $encoding): self
41+
/**
42+
* @deprecated 1.4 Use `withVersion` instead.
43+
*/
44+
public function setVersion(string|float|int $version): self
45+
{
46+
return $this->withVersion($version);
47+
}
48+
49+
public function withEncoding(string $encoding): self
4750
{
4851
$this->encoding = $encoding;
4952
return $this;
5053
}
5154

55+
/**
56+
* @deprecated 1.4 Use `withEncoding` instead.
57+
*/
58+
public function setEncoding(string $encoding): self
59+
{
60+
return $this->withEncoding($encoding);
61+
}
62+
5263
public function removeEncoding(): self
5364
{
5465
$this->encoding = '';
5566
return $this;
5667
}
5768

58-
public function setStandalone(bool $standalone = true): self
69+
public function withStandalone(bool $standalone): self
5970
{
6071
$this->standalone = ($standalone) ? 'yes' : 'no';
6172
return $this;
6273
}
6374

75+
/**
76+
* @deprecated 1.4 Use `withStandalone` instead.
77+
*/
78+
public function setStandalone(bool $standalone = true): self
79+
{
80+
return $this->withStandalone($standalone);
81+
}
82+
6483
public function removeStandalone(): self
6584
{
6685
$this->standalone = '';
@@ -83,7 +102,9 @@ public function __toString(): string
83102
$doctype =
84103
'<?xml' . PropertyArrayToString::convert(...$declarationProps) . ' ?>'
85104
. "\n";
86-
return $doctype . $this->opening() . implode('', $this->content)
105+
return $doctype
106+
. $this->opening()
107+
. Concatenate::create(...$this->content)
87108
. $this->closing();
88109
}
89110

src/Element.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace Eightfold\XMLBuilder;
66

7-
use Stringable;
8-
97
use Eightfold\XMLBuilder\Contracts\Buildable;
108
use Eightfold\XMLBuilder\Contracts\Contentable;
119

10+
use Stringable;
11+
12+
use Eightfold\XMLBuilder\Concatenate;
13+
1214
use Eightfold\XMLBuilder\Implementations\Properties as PropertiesImp;
1315
use Eightfold\XMLBuilder\Implementations\Buildable as BuildableImp;
1416
use Eightfold\XMLBuilder\Implementations\Contentable as ContentableImp;
@@ -21,13 +23,6 @@ class Element implements Buildable, Contentable
2123

2224
private bool $omitEndTag = false;
2325

24-
final private function __construct(
25-
private string $name,
26-
string|Stringable ...$content
27-
) {
28-
$this->content = $content;
29-
}
30-
3126
public function omitEndTag(): static
3227
{
3328
$this->omitEndTag = true;
@@ -42,7 +37,9 @@ protected function omitEndTagClosingString(): string
4237

4338
public function __toString(): string
4439
{
45-
return $this->opening() . implode('', $this->content) . $this->closing();
40+
return $this->opening()
41+
. Concatenate::create(...$this->content)
42+
. $this->closing();
4643
}
4744

4845
private function opening(): string
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Eightfold\XMLBuilder\Implementations;
6+
7+
use Stringable;
8+
9+
trait ContentWithoutElement
10+
{
11+
/**
12+
* @var array<string|Stringable>
13+
*/
14+
private array $content = [];
15+
16+
public static function create(string|Stringable ...$content): static
17+
{
18+
return new static(...$content);
19+
}
20+
21+
final private function __construct(string|Stringable ...$content)
22+
{
23+
$this->content = $content;
24+
}
25+
}

src/Implementations/Contentable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public static function create(
2929
): static {
3030
return new static($name, ...$content);
3131
}
32+
33+
final private function __construct(
34+
private string $name,
35+
string|Stringable ...$content
36+
) {
37+
$this->content = $content;
38+
}
3239
}

0 commit comments

Comments
 (0)