How to control XMPP XML
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am making a XMPP PHP client and currently while in testing phase I have made stanzas (i.e. presence) like this:
const PRESENCE = <<<PRESENCE
<presence from="{from}" to="{to}" type="{type}" />
PRESENCE;
const PRIORITY = <<<PRIORITY
<presence from="{from}">
<priority>{priority}</priority>
</presence>
PRIORITY;
However while developing a library I wanted to do it somewhat programatically as I feel this approach looks like it's hardcoded even though I do parse it like this for example:
$preparedString = str_replace(
['{from}', '{priority}'],
[$from, $priority],
Xml::PRIORITY
);
So I ended up creating a Presence
class which should hold all presence related methods and act as a sort of an XML builder, and it looks like this:
private $instance = null;
public function __construct()
{
$this->instance = new DOMDocument();
$this->instance->formatOutput = true;
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$presenceNode = $this->instance->createElement('presence');
$presenceNode->setAttribute("from", $from);
$presenceNode->setAttribute("to", $to);
$presenceNode->setAttribute("type", $type);
return $this->instance->saveXML($presenceNode);
}
public function setPriority(int $priority, string $from = null)
{
$presenceNode = $this->instance->createElement('presence');
if ($from)
$presenceNode->setAttribute("from", $from);
$priorityNode = $this->instance->createElement('priority');
$priorityNode->appendChild($this->instance->createTextNode($priority));
$presenceNode->appendChild($priorityNode);
return $this->instance->saveXML($presenceNode);
}
But now I have some doubts as I have tripled my code and it was actually more readable before. I would like to keep it simple and effective and without code duplication, but I feel like I'm missing something here. Is there a more slick way to do this?
php xml client xmpp
add a comment |
I am making a XMPP PHP client and currently while in testing phase I have made stanzas (i.e. presence) like this:
const PRESENCE = <<<PRESENCE
<presence from="{from}" to="{to}" type="{type}" />
PRESENCE;
const PRIORITY = <<<PRIORITY
<presence from="{from}">
<priority>{priority}</priority>
</presence>
PRIORITY;
However while developing a library I wanted to do it somewhat programatically as I feel this approach looks like it's hardcoded even though I do parse it like this for example:
$preparedString = str_replace(
['{from}', '{priority}'],
[$from, $priority],
Xml::PRIORITY
);
So I ended up creating a Presence
class which should hold all presence related methods and act as a sort of an XML builder, and it looks like this:
private $instance = null;
public function __construct()
{
$this->instance = new DOMDocument();
$this->instance->formatOutput = true;
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$presenceNode = $this->instance->createElement('presence');
$presenceNode->setAttribute("from", $from);
$presenceNode->setAttribute("to", $to);
$presenceNode->setAttribute("type", $type);
return $this->instance->saveXML($presenceNode);
}
public function setPriority(int $priority, string $from = null)
{
$presenceNode = $this->instance->createElement('presence');
if ($from)
$presenceNode->setAttribute("from", $from);
$priorityNode = $this->instance->createElement('priority');
$priorityNode->appendChild($this->instance->createTextNode($priority));
$presenceNode->appendChild($priorityNode);
return $this->instance->saveXML($presenceNode);
}
But now I have some doubts as I have tripled my code and it was actually more readable before. I would like to keep it simple and effective and without code duplication, but I feel like I'm missing something here. Is there a more slick way to do this?
php xml client xmpp
add a comment |
I am making a XMPP PHP client and currently while in testing phase I have made stanzas (i.e. presence) like this:
const PRESENCE = <<<PRESENCE
<presence from="{from}" to="{to}" type="{type}" />
PRESENCE;
const PRIORITY = <<<PRIORITY
<presence from="{from}">
<priority>{priority}</priority>
</presence>
PRIORITY;
However while developing a library I wanted to do it somewhat programatically as I feel this approach looks like it's hardcoded even though I do parse it like this for example:
$preparedString = str_replace(
['{from}', '{priority}'],
[$from, $priority],
Xml::PRIORITY
);
So I ended up creating a Presence
class which should hold all presence related methods and act as a sort of an XML builder, and it looks like this:
private $instance = null;
public function __construct()
{
$this->instance = new DOMDocument();
$this->instance->formatOutput = true;
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$presenceNode = $this->instance->createElement('presence');
$presenceNode->setAttribute("from", $from);
$presenceNode->setAttribute("to", $to);
$presenceNode->setAttribute("type", $type);
return $this->instance->saveXML($presenceNode);
}
public function setPriority(int $priority, string $from = null)
{
$presenceNode = $this->instance->createElement('presence');
if ($from)
$presenceNode->setAttribute("from", $from);
$priorityNode = $this->instance->createElement('priority');
$priorityNode->appendChild($this->instance->createTextNode($priority));
$presenceNode->appendChild($priorityNode);
return $this->instance->saveXML($presenceNode);
}
But now I have some doubts as I have tripled my code and it was actually more readable before. I would like to keep it simple and effective and without code duplication, but I feel like I'm missing something here. Is there a more slick way to do this?
php xml client xmpp
I am making a XMPP PHP client and currently while in testing phase I have made stanzas (i.e. presence) like this:
const PRESENCE = <<<PRESENCE
<presence from="{from}" to="{to}" type="{type}" />
PRESENCE;
const PRIORITY = <<<PRIORITY
<presence from="{from}">
<priority>{priority}</priority>
</presence>
PRIORITY;
However while developing a library I wanted to do it somewhat programatically as I feel this approach looks like it's hardcoded even though I do parse it like this for example:
$preparedString = str_replace(
['{from}', '{priority}'],
[$from, $priority],
Xml::PRIORITY
);
So I ended up creating a Presence
class which should hold all presence related methods and act as a sort of an XML builder, and it looks like this:
private $instance = null;
public function __construct()
{
$this->instance = new DOMDocument();
$this->instance->formatOutput = true;
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$presenceNode = $this->instance->createElement('presence');
$presenceNode->setAttribute("from", $from);
$presenceNode->setAttribute("to", $to);
$presenceNode->setAttribute("type", $type);
return $this->instance->saveXML($presenceNode);
}
public function setPriority(int $priority, string $from = null)
{
$presenceNode = $this->instance->createElement('presence');
if ($from)
$presenceNode->setAttribute("from", $from);
$priorityNode = $this->instance->createElement('priority');
$priorityNode->appendChild($this->instance->createTextNode($priority));
$presenceNode->appendChild($priorityNode);
return $this->instance->saveXML($presenceNode);
}
But now I have some doubts as I have tripled my code and it was actually more readable before. I would like to keep it simple and effective and without code duplication, but I feel like I'm missing something here. Is there a more slick way to do this?
php xml client xmpp
php xml client xmpp
asked Jan 4 at 11:57
NorgulNorgul
1,65511848
1,65511848
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
DOMDocument is a more verbose interface for XML, you could use SimpleXML which will reduce the boiler plate code.
class XML {
public static function requestPresence(string $from, string $to, string $type = "subscribe")
{
$instance = new SimpleXMLElement("<presence />");
$instance["from"] = $from;
$instance["to"] = $to;
$instance["type"] = $type;
return $instance->asXML();
}
public static function setPriority(int $priority, string $from = null)
{
$instance = new SimpleXMLElement("<presence />");
if ($from) {
$instance["from"] = $from;
}
$instance->priority = $priority;
return $instance->asXML();
}
}
This assumes that they are two separate requirements and they are just utility functions rather than having to maintain any state.
If you need to build a document with more options then the following may be more useful...
class XML2 {
private $instance = null;
public function __construct() {
$this->instance = new SimpleXMLElement("<presence />");
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$this->instance["from"] = $from;
$this->instance["to"] = $to;
$this->instance["type"] = $type;
return $this;
}
public function setPriority(int $priority, string $from = null)
{
if ($from) {
$this->instance["from"] = $from;
}
$this->instance->priority = $priority;
return $this;
}
public function getXML() {
return $this->instance->asXML();
}
}
which called using...
echo (new XML2())->requestPresence("from", "to", "type")
->setPriority(1)
->getXML();
creates...
<?xml version="1.0"?>
<presence from="from" to="to" type="type"><priority>1</priority></presence>
Using DOMDocument or SimpleXML solutions will feel more bloated than your original version - but will provide a more robust solution which is more maintainable than relying on string handling.
I was thinking about second approach, however if I have the priority included, than I don't needfrom
,to
andtype
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good
– Norgul
Jan 7 at 6:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54038537%2fhow-to-control-xmpp-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
DOMDocument is a more verbose interface for XML, you could use SimpleXML which will reduce the boiler plate code.
class XML {
public static function requestPresence(string $from, string $to, string $type = "subscribe")
{
$instance = new SimpleXMLElement("<presence />");
$instance["from"] = $from;
$instance["to"] = $to;
$instance["type"] = $type;
return $instance->asXML();
}
public static function setPriority(int $priority, string $from = null)
{
$instance = new SimpleXMLElement("<presence />");
if ($from) {
$instance["from"] = $from;
}
$instance->priority = $priority;
return $instance->asXML();
}
}
This assumes that they are two separate requirements and they are just utility functions rather than having to maintain any state.
If you need to build a document with more options then the following may be more useful...
class XML2 {
private $instance = null;
public function __construct() {
$this->instance = new SimpleXMLElement("<presence />");
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$this->instance["from"] = $from;
$this->instance["to"] = $to;
$this->instance["type"] = $type;
return $this;
}
public function setPriority(int $priority, string $from = null)
{
if ($from) {
$this->instance["from"] = $from;
}
$this->instance->priority = $priority;
return $this;
}
public function getXML() {
return $this->instance->asXML();
}
}
which called using...
echo (new XML2())->requestPresence("from", "to", "type")
->setPriority(1)
->getXML();
creates...
<?xml version="1.0"?>
<presence from="from" to="to" type="type"><priority>1</priority></presence>
Using DOMDocument or SimpleXML solutions will feel more bloated than your original version - but will provide a more robust solution which is more maintainable than relying on string handling.
I was thinking about second approach, however if I have the priority included, than I don't needfrom
,to
andtype
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good
– Norgul
Jan 7 at 6:40
add a comment |
DOMDocument is a more verbose interface for XML, you could use SimpleXML which will reduce the boiler plate code.
class XML {
public static function requestPresence(string $from, string $to, string $type = "subscribe")
{
$instance = new SimpleXMLElement("<presence />");
$instance["from"] = $from;
$instance["to"] = $to;
$instance["type"] = $type;
return $instance->asXML();
}
public static function setPriority(int $priority, string $from = null)
{
$instance = new SimpleXMLElement("<presence />");
if ($from) {
$instance["from"] = $from;
}
$instance->priority = $priority;
return $instance->asXML();
}
}
This assumes that they are two separate requirements and they are just utility functions rather than having to maintain any state.
If you need to build a document with more options then the following may be more useful...
class XML2 {
private $instance = null;
public function __construct() {
$this->instance = new SimpleXMLElement("<presence />");
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$this->instance["from"] = $from;
$this->instance["to"] = $to;
$this->instance["type"] = $type;
return $this;
}
public function setPriority(int $priority, string $from = null)
{
if ($from) {
$this->instance["from"] = $from;
}
$this->instance->priority = $priority;
return $this;
}
public function getXML() {
return $this->instance->asXML();
}
}
which called using...
echo (new XML2())->requestPresence("from", "to", "type")
->setPriority(1)
->getXML();
creates...
<?xml version="1.0"?>
<presence from="from" to="to" type="type"><priority>1</priority></presence>
Using DOMDocument or SimpleXML solutions will feel more bloated than your original version - but will provide a more robust solution which is more maintainable than relying on string handling.
I was thinking about second approach, however if I have the priority included, than I don't needfrom
,to
andtype
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good
– Norgul
Jan 7 at 6:40
add a comment |
DOMDocument is a more verbose interface for XML, you could use SimpleXML which will reduce the boiler plate code.
class XML {
public static function requestPresence(string $from, string $to, string $type = "subscribe")
{
$instance = new SimpleXMLElement("<presence />");
$instance["from"] = $from;
$instance["to"] = $to;
$instance["type"] = $type;
return $instance->asXML();
}
public static function setPriority(int $priority, string $from = null)
{
$instance = new SimpleXMLElement("<presence />");
if ($from) {
$instance["from"] = $from;
}
$instance->priority = $priority;
return $instance->asXML();
}
}
This assumes that they are two separate requirements and they are just utility functions rather than having to maintain any state.
If you need to build a document with more options then the following may be more useful...
class XML2 {
private $instance = null;
public function __construct() {
$this->instance = new SimpleXMLElement("<presence />");
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$this->instance["from"] = $from;
$this->instance["to"] = $to;
$this->instance["type"] = $type;
return $this;
}
public function setPriority(int $priority, string $from = null)
{
if ($from) {
$this->instance["from"] = $from;
}
$this->instance->priority = $priority;
return $this;
}
public function getXML() {
return $this->instance->asXML();
}
}
which called using...
echo (new XML2())->requestPresence("from", "to", "type")
->setPriority(1)
->getXML();
creates...
<?xml version="1.0"?>
<presence from="from" to="to" type="type"><priority>1</priority></presence>
Using DOMDocument or SimpleXML solutions will feel more bloated than your original version - but will provide a more robust solution which is more maintainable than relying on string handling.
DOMDocument is a more verbose interface for XML, you could use SimpleXML which will reduce the boiler plate code.
class XML {
public static function requestPresence(string $from, string $to, string $type = "subscribe")
{
$instance = new SimpleXMLElement("<presence />");
$instance["from"] = $from;
$instance["to"] = $to;
$instance["type"] = $type;
return $instance->asXML();
}
public static function setPriority(int $priority, string $from = null)
{
$instance = new SimpleXMLElement("<presence />");
if ($from) {
$instance["from"] = $from;
}
$instance->priority = $priority;
return $instance->asXML();
}
}
This assumes that they are two separate requirements and they are just utility functions rather than having to maintain any state.
If you need to build a document with more options then the following may be more useful...
class XML2 {
private $instance = null;
public function __construct() {
$this->instance = new SimpleXMLElement("<presence />");
}
public function requestPresence(string $from, string $to, string $type = "subscribe")
{
$this->instance["from"] = $from;
$this->instance["to"] = $to;
$this->instance["type"] = $type;
return $this;
}
public function setPriority(int $priority, string $from = null)
{
if ($from) {
$this->instance["from"] = $from;
}
$this->instance->priority = $priority;
return $this;
}
public function getXML() {
return $this->instance->asXML();
}
}
which called using...
echo (new XML2())->requestPresence("from", "to", "type")
->setPriority(1)
->getXML();
creates...
<?xml version="1.0"?>
<presence from="from" to="to" type="type"><priority>1</priority></presence>
Using DOMDocument or SimpleXML solutions will feel more bloated than your original version - but will provide a more robust solution which is more maintainable than relying on string handling.
edited Jan 5 at 8:33
answered Jan 5 at 8:26
Nigel RenNigel Ren
28.8k62034
28.8k62034
I was thinking about second approach, however if I have the priority included, than I don't needfrom
,to
andtype
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good
– Norgul
Jan 7 at 6:40
add a comment |
I was thinking about second approach, however if I have the priority included, than I don't needfrom
,to
andtype
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good
– Norgul
Jan 7 at 6:40
I was thinking about second approach, however if I have the priority included, than I don't need
from
, to
and type
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good– Norgul
Jan 7 at 6:40
I was thinking about second approach, however if I have the priority included, than I don't need
from
, to
and type
. Same about other stanzas, I can't use constructor in that way because I get XML's which server won't accept. Other looks good– Norgul
Jan 7 at 6:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54038537%2fhow-to-control-xmpp-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown