<?php
declare(strict_types=1);
namespace Termwind\ValueObjects;
use Generator;
/**
* @internal
*/
final class Node
{
/**
* A value object with helper methods for working with DOM node.
*/
public function __construct(private \DOMNode $node)
{
}
/**
* Gets the value of the node.
*/
public function getValue(): string
{
return $this->node->nodeValue ?? '';
}
/**
* Gets child nodes of the node.
*
* @return Generator<Node>
*/
public function getChildNodes(): Generator
{
foreach ($this->node->childNodes as $node) {
yield new static($node);
}
}
/**
* Checks if the node is a text.
*/
public function isText(): bool
{
return $this->node instanceof \DOMText;
}
/**
* Checks if the node is a comment.
*/
public function isComment(): bool
{
return $this->node instanceof \DOMComment;
}
/**
* Compares the current node name with a given name.
*/
public function isName(string $name): bool
{
return $this->getName() === $name;
}
/**
* Returns the current node type name.
*/
public function getName(): string
{
return $this->node->nodeName;
}
/**
* Returns value of [class] attribute.
*/
public function getClassAttribute(): string
{
return $this->getAttribute('class');
}
/**
* Returns value of attribute with a given name.
*/
public function getAttribute(string $name): string
{
if ($this->node instanceof \DOMElement) {
return $this->node->getAttribute($name);
}
return '';
}
/**
* Checks if the node is empty.
*/
public function isEmpty(): bool
{
return $this->isText() && preg_replace('/\s+/', '', $this->getValue()) === '';
}
/**
* Gets the previous sibling from the node.
*/
public function getPreviousSibling(): static|null
{
$node = $this->node;
while ($node = $node->previousSibling) {
$node = new static($node);
if ($node->isEmpty()) {
$node = $node->node;
continue;
}
if (! $node->isComment()) {
return $node;
}
$node = $node->node;
}
return is_null($node) ? null : new static($node);
}
/**
* Gets the next sibling from the node.
*/
public function getNextSibling(): static|null
{
$node = $this->node;
while ($node = $node->nextSibling) {
$node = new static($node);
if ($node->isEmpty()) {
$node = $node->node;
continue;
}
if (! $node->isComment()) {
return $node;
}
$node = $node->node;
}
return is_null($node) ? null : new static($node);
}
/**
* Checks if the node is the first child.
*/
public function isFirstChild(): bool
{
return is_null($this->getPreviousSibling());
}
/**
* Gets the inner HTML representation of the node including child nodes.
*/
public function getHtml(): string
{
$html = '';
foreach ($this->node->childNodes as $child) {
if ($child->ownerDocument instanceof \DOMDocument) {
$html .= $child->ownerDocument->saveXML($child);
}
}
return html_entity_decode($html);
}
/**
* Converts the node to a string.
*/
public function __toString(): string
{
if ($this->isComment()) {
return '';
}
if ($this->getValue() === ' ') {
return ' ';
}
if ($this->isEmpty()) {
return '';
}
$text = preg_replace('/\s+/', ' ', $this->getValue()) ?? '';
if (is_null($this->getPreviousSibling())) {
$text = ltrim($text);
}
if (is_null($this->getNextSibling())) {
$text = rtrim($text);
}
return $text;
}
}
Initiates a new payment transaction.
create-order
| Parameter | Type | Details |
|---|---|---|
| amount | decimal | Your Amount , Must be rounded at 2 precision. |
| currency | string | Currency Code, Must be in Upper Case (Alpha-3 code) |
| success_url | string | Enter your return or success URL |
| cancel_url | string (optional) | Enter your cancel or failed URL |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url.'create-order', [
'headers' => [
'Authorization' => 'Bearer '. $authorizationToken,
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'amount' => '$amount',
'currency' => 'currency',
'success_url' => 'success_url',
'cancel_url' => 'cancel_url',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Order created successfully."
]
},
"data": {
"redirect_url":"https://example.com/login/OISADFDFSDFSF",
"order_details":{
"amount" : "10",
"fixed_charge" : 2,
"percent_charge" : 1,
"total_charge" : 3,
"total_payable" : 13,
"currency" : "USD",
"expiry_time": "2024-04-25T06:48:35.984285Z",
"success_url": "http://127.0.0.1/nfcpay/user/transaction/success",
"cancel_url": "http://127.0.0.1/nfcpay/user/transaction/cancel"
}
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid token."
]
},
"data": null,
"type": "error"
}