Skip to content

Commit

Permalink
Add helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Aug 28, 2024
1 parent faaad6f commit 97fde01
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 109 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ $hello = $epp->getHello();

```php
use YOCLIB\EPP\EPPDocumentHelper;
use YOCLIB\EPP\EPPNamespaces;
use YOCLIB\EPP\Connections\EPPTCPConnection;
use YOCLIB\EPP\Registries\SIDNTest;

$doc = EPPDocumentHelper::createEPPDocument();

$epp = $doc->createElementNS('urn:ietf:params:xml:ns:epp-1.0','epp');
$epp = $doc->createElementNS(EPPNamespaces::EPP_1_0,'epp');

$hello = $doc->createElementNS('urn:ietf:params:xml:ns:epp-1.0','hello');
$hello = $doc->createElementNS(EPPNamespaces::EPP_1_0,'hello');

$epp->appendChild($hello);

Expand Down
53 changes: 53 additions & 0 deletions src/EPPElementHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace YOCLIB\EPP;

use Wikimedia\IDLeDOM\Document;
use Wikimedia\IDLeDOM\Node;
use YOCLIB\EPP\Elements\EPPExtensionValueElement;
use YOCLIB\EPP\Elements\EPPMessageElement;
use YOCLIB\EPP\Elements\EPPResultElement;
use YOCLIB\EPP\Elements\EPPValueElement;

class EPPElementHelper{

/**
* @param Document $document
* @param string $code
* @param EPPMessageElement $msg
* @param ?EPPValueElement|?EPPExtensionValueElement|null $valueOrExtValue
* @return EPPResultElement
*/
public static function createEPPResultElement(Document $document,string $code,EPPMessageElement $msg,$valueOrExtValue=null): EPPResultElement{
/**@var EPPResultElement $resultElement*/
$resultElement = $document->createElementNS(EPPNamespaces::EPP_1_0,'result');
$resultElement->setAttribute('code',$code);

$resultElement->appendChild($msg);

if($valueOrExtValue){
$resultElement->appendChild($valueOrExtValue);
}

return $resultElement;
}

/**
* @param Document $document
* @param string $lang
* @param string|Node $content
* @return EPPMessageElement
*/
public static function createEPPMessageElement(Document $document,string $lang,... $content): EPPMessageElement{
/**@var EPPMessageElement $messageElement*/
$messageElement = $document->createElementNS(EPPNamespaces::EPP_1_0,'msg');
if(is_string($content[0] ?? null)){
$messageElement->textContent = $content;
}else{
foreach($content AS $node){
$messageElement->appendChild($node);
}
}
return $messageElement;
}

}
4 changes: 2 additions & 2 deletions src/EPPElementImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function __createElementNS(Document $doc, ?string $ns, string $lna
}

private static function __getElements(): array{
if(!isset(self::$ELEMENTS['urn:ietf:params:xml:ns:epp-1.0'])){
if(!isset(self::$ELEMENTS[EPPNamespaces::EPP_1_0])){
self::$ELEMENTS = array_merge(self::$ELEMENTS,self::getCustomElementsEPP());
}
return self::$ELEMENTS;
Expand All @@ -99,7 +99,7 @@ private static function __lookupClass(?string $ns, string $lname){

public static function getCustomElementsEPP(): array{
return [
'urn:ietf:params:xml:ns:epp-1.0' => [
EPPNamespaces::EPP_1_0 => [
'absolute' => EPPAbsoluteElementImpl::class,
'access' => EPPAccessElementImpl::class,
'admin' => EPPAdministrationElementImpl::class,
Expand Down
20 changes: 20 additions & 0 deletions src/EPPNamespaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace YOCLIB\EPP;

class EPPNamespaces{

public const CONTACT_1_0 = 'urn:ietf:params:xml:ns:contact-1.0';

public const DOMAIN_1_0 = 'urn:ietf:params:xml:ns:domain-1.0';

public const EPP_1_0 = 'urn:ietf:params:xml:ns:epp-1.0';

public const EPPCOM_1_0 = 'urn:ietf:params:xml:ns:eppcom-1.0';

public const EPP_ORG_1_0 = 'urn:ietf:params:xml:ns:epp:org-1.0';

public const EPP_ORGEXT_1_0 = 'urn:ietf:params:xml:ns:epp:orgext-1.0';

public const HOST_1_0 = 'urn:ietf:params:xml:ns:host-1.0';

}
13 changes: 7 additions & 6 deletions tests/EPPDocumentHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use YOCLIB\EPP\Elements\EPPHelloElement;
use YOCLIB\EPP\Elements\EPPUnknownElement;
use YOCLIB\EPP\EPPDocumentHelper;
use YOCLIB\EPP\EPPNamespaces;

class EPPDocumentHelperTest extends TestCase{

Expand All @@ -29,16 +30,16 @@ public function testSetContentType(): void{
public function testCreateElementNS(): void{
$document = EPPDocumentHelper::createEPPDocument();

$this->assertInstanceOf(EPPEppElement::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','epp'));
$this->assertInstanceOf(EPPHelloElement::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','hello'));
$this->assertInstanceOf(EPPUnknownElement::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','does-not-exist'));
$this->assertInstanceOf(EPPEppElement::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'epp'));
$this->assertInstanceOf(EPPHelloElement::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'hello'));
$this->assertInstanceOf(EPPUnknownElement::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'does-not-exist'));
$this->assertInstanceOf(Element::class,$document->createElementNS('does-not-exist','does-not-exist'));

$document->setContentType('application/xml');

$this->assertInstanceOf(Element::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','epp'));
$this->assertInstanceOf(Element::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','hello'));
$this->assertInstanceOf(Element::class,$document->createElementNS('urn:ietf:params:xml:ns:epp-1.0','does-not-exist'));
$this->assertInstanceOf(Element::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'epp'));
$this->assertInstanceOf(Element::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'hello'));
$this->assertInstanceOf(Element::class,$document->createElementNS(EPPNamespaces::EPP_1_0,'does-not-exist'));
$this->assertInstanceOf(Element::class,$document->createElementNS('does-not-exist','does-not-exist'));
}

Expand Down
Loading

0 comments on commit 97fde01

Please sign in to comment.