Skip to content

Commit e498b60

Browse files
committed
XmlStylesheet.php
1 parent 84e53c4 commit e498b60

File tree

6 files changed

+146
-12
lines changed

6 files changed

+146
-12
lines changed

src/ProcessingInstruction.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Lib16\XML;
44

5-
final class ProcessingInstruction
5+
class ProcessingInstruction
66
{
7-
private $target;
8-
private $content;
9-
private $attributes;
7+
protected $target;
8+
protected $content;
9+
protected $attributes;
1010

11-
private function __construct(string $target, string $content = null)
11+
protected function __construct(string $target, string $content = null)
1212
{
1313
$this->target = $target;
1414
$this->content = $content;
@@ -19,7 +19,7 @@ public static function create(string $target, string $content = null): self
1919
return new ProcessingInstruction($target, $content);
2020
}
2121

22-
public function attrib(string $name, string $value): self
22+
public function attrib(string $name, string $value = null): self
2323
{
2424
if ($this->attributes == null) {
2525
$this->attributes = new Attributes(null);
@@ -35,7 +35,7 @@ public function getMarkup(): string
3535
$markup .= ' ' . $this->content;
3636
}
3737
if (!is_null($this->attributes)) {
38-
$markup .= $this->attributes->getMarkup('');
38+
$markup .= $this->attributes->getMarkup();
3939
}
4040
$markup .= ' ?>';
4141
return $markup;

src/shared/TitleAttribute.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Lib16\XML\Shared;
4+
5+
trait TitleAttribute
6+
{
7+
public function setTitle(string $title = null): self
8+
{
9+
return $this->attrib('title', $title);
10+
}
11+
}

src/shared/XmlStylesheet.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Lib16\XML\Shared;
4+
5+
Use Lib16\Utils\Enums\Media;
6+
7+
trait XmlStylesheet
8+
{
9+
public function xmlStylesheet(string $href,
10+
bool $alternate = false,
11+
string $title = null,
12+
$setType = false,
13+
$charset = null,
14+
Media ...$media): XmlStylesheetInstruction
15+
{
16+
return $this->instructions[] = XmlStylesheetInstruction::createXmlStylesheetInstruction(
17+
$href, $alternate, $title, $setType, $charset, ...$media);
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Lib16\XML\Shared;
4+
5+
use Lib16\Utils\Enums\Media;
6+
use Lib16\XML\ProcessingInstruction;
7+
8+
class XmlStylesheetInstruction extends ProcessingInstruction
9+
{
10+
use MediaAttribute, TitleAttribute;
11+
12+
public static function createXmlStylesheetInstruction(string $href,
13+
bool $alternate = false,
14+
string $title = null,
15+
bool $setType = false,
16+
string $charset = null,
17+
Media ...$media): self
18+
{
19+
return (new XmlStylesheetInstruction('xml-stylesheet'))
20+
->attrib('href', $href)
21+
->setType($setType)
22+
->setMedia(...$media)
23+
->setAlternate($alternate)
24+
->setTitle($title)
25+
->setCharset($charset);
26+
}
27+
28+
public function setAlternate(bool $alternate = true): self
29+
{
30+
return $this->attrib('alternate', $alternate ? 'yes' : null);
31+
}
32+
33+
public function setCharset(string $charset = null): self
34+
{
35+
return $this->attrib('charset', $charset);
36+
}
37+
38+
public function setType(bool $setType = true)
39+
{
40+
return $this->attrib('type', $setType ? 'text/css' : null);
41+
}
42+
}

tests/shared/SharedAttributesTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
require_once 'src/shared/MediaAttribute.php';
1111
require_once 'src/shared/Target.php';
1212
require_once 'src/shared/TargetAttribute.php';
13+
require_once 'src/shared/TitleAttribute.php';
1314
require_once 'tests/XmlTestCase.php';
1415
require_once 'tests/xmlClasses.php';
1516

1617
use Lib16\XML\Tests\{XmlTestCase, Tml};
17-
use Lib16\XML\Shared\MediaAttribute;
18-
use Lib16\XML\Shared\Target;
19-
use Lib16\XML\Shared\TargetAttribute;
18+
use Lib16\XML\Shared\{ClassAttribute, MediaAttribute, Target, TargetAttribute, TitleAttribute};
2019
use Lib16\Utils\Enums\Media;
21-
use Lib16\XML\Shared\ClassAttribute;
2220

2321
class MyXml extends Tml
2422
{
25-
use ClassAttribute, MediaAttribute, TargetAttribute;
23+
use ClassAttribute, MediaAttribute, TargetAttribute, TitleAttribute;
2624
}
2725

2826
class SharedAttributesTest extends XmlTestCase
@@ -93,6 +91,7 @@ public function provider()
9391
[MyXml::cs('e')->setTarget(Target::PARENT()), '<e target="_parent"/>'],
9492
[MyXml::cs('e')->setTarget(Target::SELF()), '<e target="_self"/>'],
9593
[MyXml::cs('e')->setTarget(Target::TOP()), '<e target="_top"/>'],
94+
[MyXml::cs('e')->setTitle('Lorem Ipsum'), '<e title="Lorem Ipsum"/>']
9695
];
9796
}
9897

tests/shared/XmlStylesheetTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Lib16\XML\Shared\Tests;
4+
5+
require_once 'vendor/myclabs/php-enum/src/Enum.php';
6+
require_once 'vendor/lib16/utils/src/enums/Media.php';
7+
require_once 'src/Xml.php';
8+
require_once 'src/Attributes.php';
9+
require_once 'src/shared/MediaAttribute.php';
10+
require_once 'src/shared/TitleAttribute.php';
11+
require_once 'src/shared/XmlStylesheetInstruction.php';
12+
require_once 'src/shared/XmlStylesheet.php';
13+
require_once 'tests/XmlTestCase.php';
14+
require_once 'tests/xmlClasses.php';
15+
16+
use Lib16\Utils\Enums\Media;
17+
use Lib16\XML\Shared\XmlStylesheet;
18+
use Lib16\XML\Tests\XmlTestCase;
19+
use Lib16\XML\Tests\Tml;
20+
21+
class StyledXml extends Tml
22+
{
23+
use XmlStylesheet;
24+
}
25+
26+
class XmlStylesheetTest extends XmlTestCase
27+
{
28+
public function provider()
29+
{
30+
return [
31+
[
32+
function() {
33+
$xml = StyledXml::createRoot('styled');
34+
$xml->xmlStylesheet('style.css');
35+
return $xml;
36+
},
37+
self::XML_DECL . "\n" .
38+
'<?xml-stylesheet href="style.css" ?>' . "\n" .
39+
'<styled/>'
40+
],
41+
[
42+
function() {
43+
$xml = StyledXml::createRoot('styled');
44+
$xml->xmlStylesheet('one.css',
45+
true, 'One', true, 'UTF-8', Media::SCREEN(), Media::PRINT());
46+
$xml->xmlStylesheet('two.css')
47+
->setMedia(Media::SCREEN(), Media::PRINT())
48+
->setType()
49+
->setAlternate()
50+
->setTitle('Two')
51+
->setCharset('UTF-8');
52+
return $xml;
53+
},
54+
self::XML_DECL . "\n" .
55+
'<?xml-stylesheet href="one.css" type="text/css"' .
56+
' media="screen,print" alternate="yes" title="One" charset="UTF-8" ?>' . "\n" .
57+
'<?xml-stylesheet href="two.css" type="text/css"' .
58+
' media="screen,print" alternate="yes" title="Two" charset="UTF-8" ?>' . "\n" .
59+
'<styled/>'
60+
]
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)