Skip to content

Commit

Permalink
Merge pull request #1 from BCDH/dev
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
zilbertrubb committed May 2, 2016
2 parents 29a7354 + 766b6b6 commit c1ff943
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 124 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,65 @@ foreach($result as $xml) {
var_dump($xml->somePredicateAttribute);
}
```

## Return types

- Query::setStringReturnType()
result is instance of [DOMElement](http://php.net/manual/en/class.domelement.php)

- Query::setSimpleXMLReturnType()
result is instance of [SimpleXMLElement](http://php.net/manual/en/class.simplexmlelement.php)

- Query::setDomXMLReturnType()
result is string

#### Get result field

- DomXmlResult
```php
$document = $result->getDocument();
$title = $doc->getElementsByTagName('TITLE')->item(0)->nodeValue;
```

- SimpleXML
```php
$document = $result->getDocument();
$title = $doc->TITLE;
```

#### Get result attribute

- DomXmlResult
```php
$document = $result->getDocument();
$isFavorite = $doc->hasAttribute('favourite');
```

- SimpleXML
```php
$document = $result->getDocument();
$attributes = $document->attributes();
$isFavorite = isset($attributes['favourite']);
```

## XLS transformations

- Single result (DomXmlResult|SimpleXmlResult)

```php
$resultPool = $stmt->execute();
$results = $resultPool->getAllResults();
$res = $results[0];

$html = $res->transform(__DIR__.'/xml/cd_catalog_simplified.xsl');
```

- ResultSet

```php
$resultPool = $stmt->execute();
$results = $resultPool->getAllResults();
$rootTagName = 'catalog';

$html = $resultPool->transform($rootTagName, $results, __DIR__.'/xml/cd_catalog_simplified.xsl');
```
34 changes: 0 additions & 34 deletions src/DOMResultSet.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/DomXMLResult.php → src/DomXml/DomXMLResult.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace BCDH\ExistDbClient;
namespace BCDH\ExistDbClient\DomXml;

use BCDH\ExistDbClient\ResultInterface;
use XSLTProcessor;
use DOMElement;

Expand All @@ -11,7 +12,7 @@
* Class DomXMLResult
* @package BCDH\ExistDbClient
*/
class DomXMLResult
class DomXMLResult implements ResultInterface
{
/**
* @var DOMElement
Expand Down
12 changes: 4 additions & 8 deletions src/DomXMLResultSet.php → src/DomXml/DomXMLResultSet.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

namespace BCDH\ExistDbClient;
namespace BCDH\ExistDbClient\DomXml;

use DOMElement;
use BCDH\ExistDbClient\ResultInterface;
use BCDH\ExistDbClient\ResultSet;

class DomXMLResultSet extends ResultSet
{
function __construct($client, $resultId, $options)
{
parent::__construct($client, $resultId, $options);
}

public function getNextResult()
{
$result = $this->client->retrieve(
Expand All @@ -36,7 +32,7 @@ public function transform($rootTagName, $results, $view)
$rootDocument = $root->getDocument();

foreach($results as $res) {
/** @var DOMElement $res */
/** @var ResultInterface $res */
$resultDocument = $res->getDocument();

$this->appendChild($rootDocument, $resultDocument);
Expand Down
27 changes: 3 additions & 24 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace BCDH\ExistDbClient;

use BCDH\ExistDbClient\DomXml\DomXMLResultSet;
use BCDH\ExistDbClient\SimpleXml\SimpleXMLResultSet;

class Query
{
protected $xql;
Expand Down Expand Up @@ -41,16 +44,6 @@ public function setSimpleXMLReturnType()
{
$this->returnType = "SimpleXML";
}

public function setDOMReturnType()
{
$this->returnType = "DOM";
}

public function setExtendedXMLReturnType()
{
$this->returnType = "ExtendedXML";
}

public function setDomXMLReturnType()
{
Expand Down Expand Up @@ -99,27 +92,13 @@ public function execute()

switch($this->returnType)
{
case "DOM":
$result = new DOMResultSet(
$this->client,
$resultId,
$this->options
);
break;
case "SimpleXML":
$result = new SimpleXMLResultSet(
$this->client,
$resultId,
$this->options
);
break;
case "ExtendedXML":
$result = new ExtendedXMLResultSet(
$this->client,
$resultId,
$this->options
);
break;
case "DomXML":
$result = new DomXMLResultSet(
$this->client,
Expand Down
12 changes: 12 additions & 0 deletions src/ResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace BCDH\ExistDbClient;

interface ResultInterface {
/**
* Get actual XML content displayed as DOM|Object|string
*
* @return mixed
*/
public function getDocument();
}
31 changes: 0 additions & 31 deletions src/SimpleXMLResultSet.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace BCDH\ExistDbClient;
namespace BCDH\ExistDbClient\SimpleXml;

use SimpleXMLElement;
use XSLTProcessor;

class ExtendedSimpleXMLElement extends SimpleXMLElement
class SimpleXMLElement extends \SimpleXMLElement
{
public function transform($view)
{
Expand Down
16 changes: 12 additions & 4 deletions src/ExtendedXMLResult.php → src/SimpleXml/SimpleXMLResult.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php

namespace BCDH\ExistDbClient;
namespace BCDH\ExistDbClient\SimpleXml;

use BCDH\ExistDbClient\ResultInterface;
use XSLTProcessor;

class ExtendedXMLResult
class SimpleXMLResult implements ResultInterface
{
/**
* @var ExtendedSimpleXMLElement
* @var SimpleXMLElement
*/
private $document;

function __construct($documentScalar)
{
$this->document = new ExtendedSimpleXMLElement($documentScalar);
$this->document = new SimpleXMLElement($documentScalar);
}

public function transform($view) {
Expand All @@ -22,4 +23,11 @@ public function transform($view) {
$xsltProcessor->importStylesheet(simplexml_load_file($view));
return $xsltProcessor->transformToXml($this->document);
}

/**
* @return SimpleXMLElement
*/
public function getDocument() {
return $this->document;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<?php

namespace BCDH\ExistDbClient;
namespace BCDH\ExistDbClient\SimpleXml;

use SimpleXMLElement;
use BCDH\ExistDbClient\ResultSet;

class ExtendedXMLResultSet extends ResultSet
class SimpleXMLResultSet extends ResultSet
{
function __construct($client, $resultId, $options)
{
parent::__construct($client, $resultId, $options);
}

public function getNextResult()
{
$result = $this->client->retrieve(
Expand All @@ -22,12 +17,12 @@ public function getNextResult()
$this->currentHit++;
$this->hasMoreHits = $this->currentHit < $this->hits;

return new ExtendedSimpleXMLElement($result->scalar);
return new SimpleXMLResult($result->scalar);
}

public function current()
{
return new ExtendedSimpleXMLElement($this->retrieve()->scalar);
return new SimpleXMLResult($this->retrieve()->scalar);
}

public function transform($rootTagName, $results, $view)
Expand All @@ -37,11 +32,11 @@ public function transform($rootTagName, $results, $view)
$rootDom = dom_import_simplexml($root);

foreach($results as $res) {
$this->appendChild($rootDom, $res);
$this->appendChild($rootDom, $res->getDocument());
}

$xml = $rootDom->ownerDocument->saveXML();
$mergedXml = new ExtendedSimpleXMLElement($xml);
$mergedXml = new SimpleXMLResult($xml);

return $mergedXml->transform($view);
}
Expand Down
23 changes: 23 additions & 0 deletions src/String/StringResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace BCDH\ExistDbClient\String;

use BCDH\ExistDbClient\ResultInterface;

class StringResult implements ResultInterface
{
/** @var string */
private $document;

function __construct($documentScalar)
{
$this->document = $documentScalar;
}

/**
* @return string
*/
public function getDocument() {
return $this->document;
}
}
Loading

0 comments on commit c1ff943

Please sign in to comment.