Skip to content

Commit

Permalink
finalise?
Browse files Browse the repository at this point in the history
-
  • Loading branch information
lifeofguenter committed Apr 27, 2017
1 parent 3f5104a commit 6fa31fb
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 176 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ if (!($response instanceof Response)) {
exit(1);
}

echo $response->code() . PHP_EOL;
echo $response->message() . PHP_EOL;
$result = $response->results()[0];

echo $result->code() . PHP_EOL;
echo $result->message() . PHP_EOL;
echo $response->clientTransactionId() . PHP_EOL;
echo $response->serverTransactionId() . PHP_EOL;
$data = $response->data();
Expand Down
99 changes: 99 additions & 0 deletions src/AfriCC/EPP/DOM/DOMTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of the php-epp2 library.
*
* (c) Gunter Grodotzki <gunter@afri.cc>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace AfriCC\EPP\DOM;

use DOMElement;

class DOMTools
{
/**
* transform epp style DOMElement to a php-array
*
* @param DOMElement $node
* @param array $forceArrayKeys force certain tags into an array that are
* expected to be iterated (forex: street).
* @param array $ignoreAttributeKeys ignore certain attributes as they
* contain duplicate information (forex: ipType).
*/
public static function nodeToArray(
DOMElement $node,
$forceArrayKeys = ['hostAttr', 'hostObj', 'street', 'hostAddr'],
$ignoreAttributeKeys = ['hostAddr']
)
{
$tmp = [];
foreach ($node->childNodes as $each) {
if ($each->nodeType !== XML_ELEMENT_NODE) {
continue;
}

// if node only has a type attribute lets distinguish them directly
// and then ignore the attribtue
if ($each->hasAttribute('type')) {
$key = $each->localName . '@' . $each->getAttribute('type');
$ignore_attributes = true;
} else {
$key = $each->localName;
$ignore_attributes = false;
}

// in case of special keys, always create array
if (in_array($key, $forceArrayKeys)) {
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// if key already exists, dynamically create an array
elseif (isset($tmp[$key])) {
if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) {
$tmp[$key] = [$tmp[$key]];
}
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// key was not yet set, so lets start off with a string
else {
$current = &$tmp[$key];
$insert_key = null;
}

if ($each->hasChildNodes()) {
$current = static::nodeToArray($each, $forceArrayKeys, $ignoreAttributeKeys);
} else {
$current = $each->nodeValue;

if (!$ignore_attributes && !in_array($each->localName, $ignoreAttributeKeys) && $each->hasAttributes()) {
foreach ($each->attributes as $attr) {

// single attribute with empty node, use the attr-value directly
if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) {
$current = $attr->nodeValue;
break;
}

if ($insert_key) {
if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) {
$tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]];
}
$tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue;
} else {
$tmp['@' . $key][$attr->nodeName] = $attr->nodeValue;
}
}
}
}
}

return $tmp;
}
}
2 changes: 1 addition & 1 deletion src/AfriCC/EPP/Frame/Command/Transfer/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Domain extends TransferCommand
public function setDomain($domain)
{
if (!Validator::isHostname($domain)) {
throw new Exception(sprintf('%s is not a valid domain name'));
throw new Exception(sprintf('%s is not a valid domain name', $domain));
}

$this->set('domain:name', $domain);
Expand Down
100 changes: 3 additions & 97 deletions src/AfriCC/EPP/Frame/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,15 @@
namespace AfriCC\EPP\Frame;

use AfriCC\EPP\AbstractFrame;
use AfriCC\EPP\DOM\DOMTools;
use AfriCC\EPP\Frame\Response\Result;
use DOMNode;
use DOMNodeList;

/**
* @see http://tools.ietf.org/html/rfc5730#section-2.6
*/
class Response extends AbstractFrame
{
/**
* nodeToArray force array values for the following tags. Usually the upper
* level will expect them as an array to traverse. Otherwise, if only one
* value exists it will be converted directly to a string
*
* @var array
*/
protected $n2a_force_array = [
'hostAttr' => true,
'hostObj' => true,
'street' => true,
'hostAddr' => true,
];

/**
* nodeToArray ignore conversion of following attributes. Usually because
* the information is redundant or useless (like the definition of IP types
* which should be done on the higher level)
*
* @var array
*/
protected $n2a_ignore_attr = [
'hostAddr' => true,
];

public function results()
{
$results = [];
Expand Down Expand Up @@ -103,84 +78,15 @@ public function data()
if ($nodes === false || !($nodes instanceof DOMNodeList) || $nodes->length === 0 || !$nodes->item(0)->hasChildNodes()) {
$data = [];
} else {
$data = $this->nodeToArray($nodes->item(0));
$data = DOMTools::nodeToArray($nodes->item(0));
}

// check for extension data
$nodes = $this->get('//epp:epp/epp:response/epp:extension');
if ($nodes !== false && $nodes instanceof DOMNodeList && $nodes->length > 0 && $nodes->item(0)->hasChildNodes()) {
$data = array_merge_recursive($data, $this->nodeToArray($nodes->item(0)));
$data = array_merge_recursive($data, DOMTools::nodeToArray($nodes->item(0)));
}

return $data;
}

private function nodeToArray(DOMNode $node)
{
$tmp = [];
foreach ($node->childNodes as $each) {
if ($each->nodeType !== XML_ELEMENT_NODE) {
continue;
}

// if node only has a type attribute lets distinguish them directly
// and then ignore the attribtue
if ($each->hasAttribute('type')) {
$key = $each->localName . '@' . $each->getAttribute('type');
$ignore_attributes = true;
} else {
$key = $each->localName;
$ignore_attributes = false;
}

// in case of special keys, always create array
if (isset($this->n2a_force_array[$key])) {
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// if key already exists, dynamically create an array
elseif (isset($tmp[$key])) {
if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) {
$tmp[$key] = [$tmp[$key]];
}
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// key was not yet set, so lets start off with a string
else {
$current = &$tmp[$key];
$insert_key = null;
}

if ($each->hasChildNodes()) {
$current = $this->nodeToArray($each);
} else {
$current = $each->nodeValue;

if (!$ignore_attributes && !isset($this->n2a_ignore_attr[$each->localName]) && $each->hasAttributes()) {
foreach ($each->attributes as $attr) {

// single attribute with empty node, use the attr-value directly
if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) {
$current = $attr->nodeValue;
break;
}

if ($insert_key) {
if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) {
$tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]];
}
$tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue;
} else {
$tmp['@' . $key][$attr->nodeName] = $attr->nodeValue;
}
}
}
}
}

return $tmp;
}
}
76 changes: 4 additions & 72 deletions src/AfriCC/EPP/Frame/Response/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace AfriCC\EPP\Frame\Response;

use AfriCC\EPP\DOM\DOMTools;
use DOMElement;
use DOMNode;

class Result
{
Expand Down Expand Up @@ -76,7 +76,7 @@ protected function parseResultNode($node)
continue;
}

switch($each->localName) {
switch ($each->localName) {
case 'msg':
$this->msg = $each->nodeValue;
if ($each->hasAttribute('lang')) {
Expand All @@ -85,84 +85,16 @@ protected function parseResultNode($node)
break;

case 'value':
$this->values = $this->nodeToArray($each);
$this->values = array_merge_recursive($this->values, DOMTools::nodeToArray($each));
break;

case 'extValue':
$this->extValues = array_merge_recursive($this->extValues, DOMTools::nodeToArray($each));
break;

default:
break;
}
}
}

private function nodeToArray(DOMNode $node)
{
$tmp = [];
foreach ($node->childNodes as $each) {
if ($each->nodeType !== XML_ELEMENT_NODE) {
continue;
}

// if node only has a type attribute lets distinguish them directly
// and then ignore the attribtue
if ($each->hasAttribute('type')) {
$key = $each->localName . '@' . $each->getAttribute('type');
$ignore_attributes = true;
} else {
$key = $each->localName;
$ignore_attributes = false;
}

// in case of special keys, always create array
if (isset($this->n2a_force_array[$key])) {
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// if key already exists, dynamically create an array
elseif (isset($tmp[$key])) {
if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) {
$tmp[$key] = [$tmp[$key]];
}
$current = &$tmp[$key][];
end($tmp[$key]);
$insert_key = key($tmp[$key]);
}
// key was not yet set, so lets start off with a string
else {
$current = &$tmp[$key];
$insert_key = null;
}

if ($each->hasChildNodes()) {
$current = $this->nodeToArray($each);
} else {
$current = $each->nodeValue;

if (!$ignore_attributes && !isset($this->n2a_ignore_attr[$each->localName]) && $each->hasAttributes()) {
foreach ($each->attributes as $attr) {

// single attribute with empty node, use the attr-value directly
if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) {
$current = $attr->nodeValue;
break;
}

if ($insert_key) {
if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) {
$tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]];
}
$tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue;
} else {
$tmp['@' . $key][$attr->nodeName] = $attr->nodeValue;
}
}
}
}
}

return $tmp;
}
}
Loading

0 comments on commit 6fa31fb

Please sign in to comment.