Skip to content

Commit 5869360

Browse files
committed
README.md etc.
1 parent e498b60 commit 5869360

13 files changed

+167
-86
lines changed

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#`lib16/xml-builder-php`
2+
3+
Some utility classes for lib16.
4+
5+
## Installation with Composer
6+
7+
Add the following to your project's `composer.json` file:
8+
```json
9+
"require": {
10+
"lib16/xml": "^1.0"
11+
}
12+
```
13+
14+
Run `composer update`
15+
16+
Include Composer's `autoload.php`:
17+
```php
18+
require_once 'vendor/autoload.php';
19+
```
20+
## Basic Usage
21+
22+
```php
23+
<?php
24+
require_once 'vendor/autoload.php';
25+
26+
use Lib16\XML\Xml;
27+
28+
class Kml extends Xml
29+
{
30+
const MIME_TYPE = 'application/vnd.google-earth.kml+xml';
31+
const FILENAME_EXTENSION = 'kml';
32+
const XML_NAMESPACE = 'http://www.opengis.net/kml/2.2';
33+
34+
public static function createKml()
35+
{
36+
return static::createRoot('kml');
37+
}
38+
39+
public function placemark($name, $description, $longitude, $latitude, $altitude = 0)
40+
{
41+
$pm = $this->append('Placemark');
42+
$pm->append('name', $name);
43+
$pm->append('description', $description);
44+
$pm->append('Point')
45+
->append('coordinates', $longitude . ',' . $latitude . ',' . $altitude);
46+
return $pm;
47+
}
48+
}
49+
50+
$myKml = Kml::createKml();
51+
$myKml->placemark('Cologne Cathedral',
52+
'Cologne Cathedral is a Roman Catholic cathedral in Cologne, Germany.',
53+
'50.9413', '6.958');
54+
$myKml->headerfields('cologne-cathedral');
55+
print $myKml;
56+
```
57+
58+
The generated markup:
59+
60+
```xml
61+
<?xml version="1.0" encoding="UTF-8" ?>
62+
<kml xmlns="http://www.opengis.net/kml/2.2">
63+
<Placemark>
64+
<name>Cologne Cathedral</name>
65+
<description>Cologne Cathedral is a Roman Catholic cathedral in Cologne, Germany.</description>
66+
<Point>
67+
<coordinates>50.9413,6.958,0</coordinates>
68+
</Point>
69+
</Placemark>
70+
</kml>
71+
```
72+
73+
74+
### The `Adhoc` trait
75+
76+
`Adhoc` allows you to use any method name not previously defined to add XML elements or attributes.
77+
78+
```php
79+
<?php
80+
require_once 'vendor/autoload.php';
81+
use Lib16\XML\{Xml, Adhoc};
82+
83+
class Html extends Xml
84+
{
85+
use Adhoc;
86+
87+
const XML_DECLARATION = false;
88+
const DOCTYPE = '<!DOCTYPE html>';
89+
const HTML_MODE_ENABLED = true;
90+
91+
public static function createHtml($lang = null, $manifest = null)
92+
{
93+
return static::createRoot('html')
94+
->attrib('lang', $lang)
95+
->setManifest($manifest);
96+
}
97+
}
98+
99+
$html = Html::createHtml('en');
100+
$body = $html->body();
101+
$article = $body->article();
102+
$article->h1('Scripting languages');
103+
$article->p(Html::abbr('PHP')->setTitle('PHP: Hypertext Preprocessor') . ' is a
104+
server-side scripting language designed for web development
105+
but also used as a general-purpose programming language.');
106+
print $html;
107+
```
108+
109+
The generated markup:
110+
111+
```html
112+
<?xml version="1.0" encoding="UTF-8" ?>
113+
<!DOCTYPE html>
114+
<html lang="en">
115+
<body>
116+
<article>
117+
<h1>Scripting languages</h1>
118+
<p><abbr title="PHP: Hypertext Preprocessor">PHP</abbr> is a
119+
server-side scripting language designed for web development
120+
but also used as a general-purpose programming language.</p>
121+
</article>
122+
</body>
123+
</html>
124+
```

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
{
22
"name": "lib16/xml",
3-
"type": "library",
43
"description": "lib16 XML Builder is a PHP 7 library for creating XML documents.",
54
"keywords": ["xml", "builder", "writer"],
65
"homepage" : "https://github.com/lib16/xml-builder-php",
76
"license": "MIT",
8-
"minimum-stability": "dev",
97
"require": {
108
"php": ">=7.0",
11-
"lib16/utils": ">=1.0",
12-
"myclabs/php-enum" : ">=1.5"
9+
"myclabs/php-enum" : "^1.5",
10+
"lib16/utils": "^1.0"
1311
},
1412
"autoload": {
1513
"psr-4": {
16-
"Lib16\\Xml\\": "src/"
14+
"Lib16\\XML\\": "src/"
1715
}
1816
},
1917
"autoload-dev" : {
20-
"psr-4" : {
21-
"Lib16\\Utils\\Tests\\" : "tests/"
22-
}
18+
"psr-4": {
19+
"Lib16\\XML\\Tests\\" : "tests/"
20+
},
21+
"files": ["tests/xmlClasses.php"]
2322
}
2423
}

src/Attributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Lib16\XML;
44

55
use MyCLabs\Enum\Enum;
6-
use Lib16\Utils\Enums\Unit;
6+
use Lib16\Utils\Enums\CSS\Unit;
77
use Lib16\Utils\NumberFormatter;
88

99
/**

src/shared/MediaAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Lib16\XML\Shared;
44

5-
use Lib16\Utils\Enums\Media;
5+
use Lib16\Utils\Enums\CSS\Media;
66

77
trait MediaAttribute
88
{

src/shared/XmlStylesheet.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace Lib16\XML\Shared;
44

5-
Use Lib16\Utils\Enums\Media;
5+
Use Lib16\Utils\Enums\CSS\Media;
6+
Use Lib16\Utils\Enums\Mime\StyleType;
67

78
trait XmlStylesheet
89
{
910
public function xmlStylesheet(string $href,
1011
bool $alternate = false,
1112
string $title = null,
12-
$setType = false,
13-
$charset = null,
13+
StyleType $type = null,
14+
string $charset = null,
1415
Media ...$media): XmlStylesheetInstruction
1516
{
1617
return $this->instructions[] = XmlStylesheetInstruction::createXmlStylesheetInstruction(
17-
$href, $alternate, $title, $setType, $charset, ...$media);
18+
$href, $alternate, $title, $type, $charset, ...$media);
1819
}
1920
}

src/shared/XmlStylesheetInstruction.php

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

33
namespace Lib16\XML\Shared;
44

5-
use Lib16\Utils\Enums\Media;
5+
use Lib16\Utils\Enums\CSS\Media;
6+
use Lib16\Utils\Enums\Mime\StyleType;
67
use Lib16\XML\ProcessingInstruction;
78

89
class XmlStylesheetInstruction extends ProcessingInstruction
@@ -12,13 +13,13 @@ class XmlStylesheetInstruction extends ProcessingInstruction
1213
public static function createXmlStylesheetInstruction(string $href,
1314
bool $alternate = false,
1415
string $title = null,
15-
bool $setType = false,
16+
StyleType $type = null,
1617
string $charset = null,
1718
Media ...$media): self
1819
{
1920
return (new XmlStylesheetInstruction('xml-stylesheet'))
2021
->attrib('href', $href)
21-
->setType($setType)
22+
->setType($type)
2223
->setMedia(...$media)
2324
->setAlternate($alternate)
2425
->setTitle($title)
@@ -35,8 +36,8 @@ public function setCharset(string $charset = null): self
3536
return $this->attrib('charset', $charset);
3637
}
3738

38-
public function setType(bool $setType = true)
39+
public function setType(StyleType $type = null)
3940
{
40-
return $this->attrib('type', $setType ? 'text/css' : null);
41+
return $this->attrib('type', $type);
4142
}
4243
}

tests/AdhocTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Lib16\XML\Tests;
44

5-
require_once 'src/Xml.php';
6-
require_once 'src/Attributes.php';
7-
require_once 'src/Adhoc.php';
8-
require_once 'tests/XmlTestCase.php';
5+
require_once 'vendor/autoload.php';
96
require_once 'tests/xmlClasses.php';
107

118
use Lib16\XML\Xml;

tests/AttributesTest.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace Lib16\XML\Tests;
44

5-
//require_once 'vendor/autoload.php';
6-
7-
require_once 'vendor/myclabs/php-enum/src/Enum.php';
8-
require_once 'vendor/lib16/utils/src/enums/Unit.php';
9-
require_once 'vendor/lib16/utils/src/enums/Media.php';
10-
require_once 'vendor/lib16/utils/src/NumberFormatter.php';
11-
require_once 'src/Attributes.php';
5+
require_once 'vendor/autoload.php';
126

137
use Lib16\Utils\NumberFormatter;
14-
use Lib16\Utils\Enums\{Unit, Media};
8+
use Lib16\Utils\Enums\CSS\{LengthUnit, Media};
159
use Lib16\XML\Attributes;
1610
use PHPUnit\Framework\TestCase;
1711

@@ -119,9 +113,9 @@ public function provider(): array
119113
[(new Attributes())->setNumber('a', 12.3456789, $f), ' a="12.3457"'],
120114
[(new Attributes())->setNumber('a', 12.3, $f), ' a="12.3"'],
121115
[(new Attributes())->setNumber('a', 12, $f), ' a="12"'],
122-
[(new Attributes())->setNumber('a', 16, $f, Unit::PX()), ' a="16px"'],
123-
[(new Attributes())->setNumber('a', 50, $f, Unit::PERCENT()), ' a="50%"'],
124-
[(new Attributes())->setNumber('a', 1.5, $f, Unit::NONE()), ' a="1.5"'],
116+
[(new Attributes())->setNumber('a', 16, $f, LengthUnit::PX()), ' a="16px"'],
117+
[(new Attributes())->setNumber('a', 50, $f, LengthUnit::PERCENT()), ' a="50%"'],
118+
[(new Attributes())->setNumber('a', 1.5, $f, LengthUnit::NONE()), ' a="1.5"'],
125119
[(new Attributes())->setNumber('a', 1.5, $f, null), ' a="1.5"'],
126120
[(new Attributes())->setNumber('a', null, $f), ''],
127121

@@ -134,11 +128,11 @@ public function provider(): array
134128
' a="10.5 5.25 10.5"'
135129
],
136130
[(new Attributes())->setNumbers('a', ' ', $f), ''],
137-
[(new Attributes())->setNumbers('a', ' ', $f, Unit::PX()), ''],
131+
[(new Attributes())->setNumbers('a', ' ', $f, LengthUnit::PX()), ''],
138132
[
139133
(new Attributes())
140-
->setNumbers('a', ' ', $f, Unit::PX(), 10.00001, 5)
141-
->setNumbers('a', ' ', $f, Unit::PX(), 10)
134+
->setNumbers('a', ' ', $f, LengthUnit::PX(), 10.00001, 5)
135+
->setNumbers('a', ' ', $f, LengthUnit::PX(), 10)
142136
->setNumbers('a', ' ', $f),
143137
' a="10px 5px 10px"'
144138
],

tests/ProcessingInstructionTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace Lib16\XML\Tests;
44

5-
//require_once 'vendor/autoload.php';
5+
require_once 'vendor/autoload.php';
66

7-
require_once 'src/Attributes.php';
8-
require_once 'src/ProcessingInstruction.php';
9-
10-
use Lib16\XML\Attributes;
117
use Lib16\XML\ProcessingInstruction;
128
use PHPUnit\Framework\TestCase;
139

tests/XmlTest.php

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

33
namespace Lib16\XML\Tests;
44

5-
require_once 'vendor/lib16/utils/src/enums/Media.php';
6-
require_once 'src/Xml.php';
7-
require_once 'src/Attributes.php';
8-
require_once 'src/Space.php';
9-
require_once 'tests/XmlTestCase.php';
10-
require_once 'tests/xmlClasses.php';
5+
require_once 'vendor/autoload.php';
116

12-
use Lib16\Utils\Enums\Media;
7+
use Lib16\Utils\Enums\CSS\Media;
138
use Lib16\XML\{Xml, Attributes, Space};
149

1510
const MULTILINE = "lorem\nipsum\ndolor\nsit";

0 commit comments

Comments
 (0)