Skip to content

Commit

Permalink
PHP 5.6 is the new minimum version.
Browse files Browse the repository at this point in the history
Closes #2.
  • Loading branch information
daniele-orlando committed Dec 17, 2015
1 parent 2697ccb commit bc107a0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 45 deletions.
93 changes: 51 additions & 42 deletions source/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
* @license https://opensource.org/licenses/BSD-2-Clause
*/

if (version_compare(phpversion(), '7', '<')) {
require_once __DIR__ . \DIRECTORY_SEPARATOR . 'FluidXml.php56.php';
} else {
require_once __DIR__ . \DIRECTORY_SEPARATOR . 'FluidXml.php70.php';
}

/**
* Constructs a new FluidXml instance.
*
Expand Down Expand Up @@ -135,18 +141,56 @@ public function attr(...$arguments);
public function text($text);
}

class FluidXml implements FluidInterface
trait FluidNamespaceTrait
{
use FluidNamespaceTrait;
use FluidNamespaceShadowTrait; // For compatibility with PHP 5.6.

const ROOT_NODE = 'doc';
private $namespaces = [];

private $dom;
public function namespaces()
{
return $this->namespaces;
}

public static function new(...$arguments)
// This method should be called 'namespace',
// but for compatibility with PHP 5.6
// it is shadowed by a facade method:
// - in PHP 7 by a real public 'namespace' method
// - in PHP 5 by a public __call method which understands a 'namespace' call.
protected function registerNamespace(...$arguments)
{
return new FluidXml(...$arguments);
$namespaces = [];

if (\is_string($arguments[0])) {
$args = [ $arguments[0], $arguments[1] ];

if (isset($arguments[2])) {
$args[] = $arguments[2];
}

$namespaces[] = new FluidNamespace(...$args);
} else if (\is_array($arguments[0])) {
$namespaces = $arguments[0];
} else {
$namespaces = $arguments;
}

foreach ($namespaces as $n) {
$this->namespaces[$n->id()] = $n;
}

return $this;
}
}

class FluidXml implements FluidInterface
{
use FluidNamespaceTrait,
FluidXmlShadowTrait; // For compatibility with PHP 5.6.

const ROOT_NODE = 'doc';

private $dom;

public static function load($document)
{
Expand Down Expand Up @@ -181,7 +225,7 @@ public static function load($document)
throw new \Exception('Document not recognized.');
}

return FluidXml::new(['root' => null])->appendXml($xml);
return (new FluidXml(['root' => null]))->appendXml($xml);
}

public function __construct($root = null, $options = [])
Expand Down Expand Up @@ -880,41 +924,6 @@ protected function insertNode($fn, $node, ...$optionals)
}
}

trait FluidNamespaceTrait
{
private $namespaces = [];

public function namespaces()
{
return $this->namespaces;
}

public function namespace(...$arguments)
{
$namespaces = [];

if (\is_string($arguments[0])) {
$args = [ $arguments[0], $arguments[1] ];

if (isset($arguments[2])) {
$args[] = $arguments[2];
}

$namespaces[] = new FluidNamespace(...$args);
} else if (\is_array($arguments[0])) {
$namespaces = $arguments[0];
} else {
$namespaces = $arguments;
}

foreach ($namespaces as $n) {
$this->namespaces[$n->id()] = $n;
}

return $this;
}
}

class FluidNamespace
{
const ID = 'id' ;
Expand Down
17 changes: 17 additions & 0 deletions source/FluidXml.php56.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

trait FluidXmlShadowTrait
{
}

trait FluidNamespaceShadowTrait
{
public function __call($method, $arguments)
{
if ($method === 'namespace') {
return $this->registerNamespace(...$arguments);
}

throw new \Exception("Method '$method' not found.");
}
}
17 changes: 17 additions & 0 deletions source/FluidXml.php70.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

trait FluidXmlShadowTrait
{
public static function new(...$arguments)
{
return new FluidXml(...$arguments);
}
}

trait FluidNamespaceShadowTrait
{
public function namespace(...$arguments)
{
return $this->registerNamespace(...$arguments);
}
}
6 changes: 4 additions & 2 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@
});
});

if (version_compare(phpversion(), '7', '>=')) {
describe(':new', function() {
it('should behave like FluidXml::__construct', function() {
$xml = new FluidXml();
$alias = FluidXml::new();
eval('$alias = FluidXml::new();');

$actual = $alias->xml();
$expected = $xml->xml();
Expand All @@ -186,13 +187,14 @@
'stylesheet' => 'stylesheet.xsl' ];

$xml = new FluidXml($options);
$alias = FluidXml::new($options);
eval('$alias = FluidXml::new($options);');

$actual = $alias->xml();
$expected = $xml->xml();
assert($actual === $expected, __($actual, $expected));
});
});
}

describe('.dom', function() {
it('should return the DOMDocument of the document', function() {
Expand Down
2 changes: 1 addition & 1 deletion support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"vendor-dir": "./composer"
},
"require": {
"php": ">=7"
"php": ">=5.6"
},
"require-dev": {
"peridot-php/peridot": "1.*",
Expand Down

0 comments on commit bc107a0

Please sign in to comment.