Skip to content

Commit

Permalink
Enum helper class, because why not?
Browse files Browse the repository at this point in the history
  • Loading branch information
kadet1090 committed Mar 20, 2017
1 parent f6cba5a commit 58b7f00
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Component/Subscription/SubscriptionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2017, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/

namespace Kadet\Xmpp\Component\Subscription;


class SubscriptionEvent
{

}
7 changes: 3 additions & 4 deletions Stanza/Presence.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Kadet\Xmpp\Exception\InvalidArgumentException;
use Kadet\Xmpp\Jid;
use Kadet\Xmpp\Stanza\Presence\Show;
use Kadet\Xmpp\Utils\filter;
use Kadet\Xmpp\Xml\XmlElement;

Expand All @@ -34,8 +35,6 @@
*/
class Presence extends Stanza
{
const POSSIBLE_SHOW = ['available', 'unavailable', 'chat', 'dnd', 'away', 'xa'];

/**
* Presence constructor.
* @param array $options {
Expand Down Expand Up @@ -69,9 +68,9 @@ public function getShow()
*/
public function setShow(string $show = 'available')
{
if(!in_array($show, self::POSSIBLE_SHOW)) {
if(Show::valid($show)) {
throw new InvalidArgumentException(format('$show must be one of: {possible}. {show} given.', [
'possible' => implode(',', self::POSSIBLE_SHOW),
'possible' => implode(',', Show::available()),
'show' => $show
]));
}
Expand Down
4 changes: 3 additions & 1 deletion Stanza/Presence/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
namespace Kadet\Xmpp\Stanza\Presence;


class Show
use Kadet\Xmpp\Utils\Enum;

class Show extends Enum
{
const AVAILABLE = "available";

Expand Down
2 changes: 1 addition & 1 deletion Tests/Modules/Subscription/SubscriptionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use PHPUnit_Framework_MockObject_MockObject as Mock;

use function Kadet\Xmpp\Utils\filter\{
all, not, pass
all, not
};
use function Kadet\Xmpp\Utils\filter\stanza\{
to, type, id
Expand Down
33 changes: 33 additions & 0 deletions Utils/Enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2017, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/

namespace Kadet\Xmpp\Utils;


class Enum
{
public static function available() {
static $reflection = null;
if(!$reflection) {
$reflection = new \ReflectionClass(self::class);
}

return $reflection->getConstants();
}

public static function valid($value) {
return in_array($value, self::available());
}
}
2 changes: 1 addition & 1 deletion XmppClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function send(Stanza $stanza) : ExtendedPromiseInterface
} else {
$deferred->resolve($stanza);
}
}, with\stanza\id($stanza->id));
}, with\stanza\id($stanza->id));
$this->write($stanza);

return $deferred->promise();
Expand Down

0 comments on commit 58b7f00

Please sign in to comment.