Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 654d03a

Browse files
committed
JSON Encoder and examples
1 parent c679c45 commit 654d03a

9 files changed

Lines changed: 114 additions & 70 deletions

File tree

examples/encodedping.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/pubsub/jsonencodedpub.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<?php
2-
require_once __DIR__ . "/../../vendor/autoload.php";
2+
require_once __DIR__.'/../../vendor/autoload.php';
33

4-
$encoder = new \Nats\Encoders\JSONEncoder();
4+
$encoder = new \Nats\Encoders\JSONEncoder();
55
$connectionOptions = new \Nats\ConnectionOptions();
6-
$connectionOptions
7-
->setHost('localhost')
8-
->setPort(4222);
6+
7+
$connectionOptions->setHost('localhost')->setPort(4222);
98
$c = new Nats\EncodedConnection($connectionOptions, $encoder);
109
$c->connect();
1110

1211
$c->reconnect();
1312

14-
$c->publish("foo", "bar");
15-
$c->publish("foo", "bar");
16-
$c->publish("foo", "bar");
17-
$c->publish("foo", "bar");
18-
$c->publish("foo", "bar");
13+
$c->publish('foo', 'bar');
14+
$c->publish('foo', 'bar');
15+
$c->publish('foo', 'bar');
16+
$c->publish('foo', 'bar');
17+
$c->publish('foo', 'bar');

examples/pubsub/jsonencodedsub.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?php
2-
require_once __DIR__ . "/../../vendor/autoload.php";
2+
require_once __DIR__.'/../../vendor/autoload.php';
33

4-
$encoder = new \Nats\Encoders\JSONEncoder();
4+
$encoder = new \Nats\Encoders\JSONEncoder();
55
$connectionOptions = new \Nats\ConnectionOptions();
6-
$connectionOptions
7-
->setHost('localhost')
8-
->setPort(4222);
6+
7+
$connectionOptions->setHost('localhost')->setPort(4222);
98
$c = new Nats\EncodedConnection($connectionOptions, $encoder);
109
$c->connect();
1110

1211
$callback = function ($payload) {
1312
printf("Data: %s\r\n", $payload);
1413
};
1514

16-
$sid = $c->subscribe("foo", $callback);
15+
$sid = $c->subscribe('foo', $callback);
1716

1817
$c->wait(2);
1918

spec/Nats/ConnectionOptionsSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function it_has_default_lang_value_as_php() {
3333
}
3434

3535
function it_has_default_version_value_as_null() {
36-
$this->getVersion()->shouldEqual("0.8.0");
36+
$this->getVersion()->shouldEqual("0.8.2");
3737
}
3838

3939
function it_has_default_verbose_value_as_null() {

src/Nats/Connection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66

77
/**
88
* Connection Class.
9+
*
10+
* Handles the connection to a NATS server or cluster of servers.
911
*/
1012
class Connection
1113
{
1214

1315
/**
14-
* Number of PINGS.
16+
* Number of PINGs.
1517
*
16-
* @var int number of pings
18+
* @var integer number of pings.
1719
*/
1820
private $pings = 0;
1921

2022
/**
21-
* Chunk size in bytes to use when reading with fread.
23+
* Chunk size in bytes to use when reading an stream of data.
2224
*
23-
* @var integer
25+
* @var integer size of chunk.
2426
*/
2527
private $chunkSize = 1500;
2628

src/Nats/EncodedConnection.php

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,95 @@
33

44
/**
55
* Class EncodedConnection
6+
*
67
* @package Nats
78
*/
8-
class EncodedConnection extends Connection {
9+
class EncodedConnection extends Connection
10+
{
911

1012
/**
11-
* @var Encoder|null
13+
* Encoder for this connection.
14+
*
15+
* @var \Nats\Encoders\Encoder|null
1216
*/
1317
private $encoder = null;
1418

19+
1520
/**
1621
* EncodedConnection constructor.
17-
* @param ConnectionOptions|null $options
18-
* @param Encoder|null $encoder
22+
*
23+
* @param ConnectionOptions $options Connection options object.
24+
* @param \Nats\Encoders\Encoder|null $encoder Encoder to use with the payload.
1925
*/
20-
public function __construct(ConnectionOptions $options = null, \Nats\Encoders\Encoder $encoder = null) {
26+
public function __construct(ConnectionOptions $options = null, \Nats\Encoders\Encoder $encoder = null)
27+
{
2128
$this->encoder = $encoder;
2229
parent::__construct($options);
2330
}
2431

2532
/**
26-
* @param string $subject
27-
* @param string $payload
28-
* @param mixed $callback
29-
* @param int $wait
33+
* Request does a request and executes a callback with the response.
34+
*
35+
* @param string $subject Message topic.
36+
* @param string $payload Message data.
37+
* @param mixed $callback Closure to be executed as callback.
38+
* @param integer $wait Number of messages to wait for.
39+
*
40+
* @return void
3041
*/
31-
public function request($subject, $payload, $callback, $wait = 1) {
32-
$payload = $this->encoder->encode($payload);
33-
$decode_callback = function ($payload) use ($callback) {
42+
public function request($subject, $payload, $callback, $wait = 1)
43+
{
44+
$payload = $this->encoder->encode($payload);
45+
$decodeCallback = function ($payload) use ($callback) {
3446
$callback($this->encoder->decode($payload));
3547
};
36-
parent::request($subject, $payload, $decode_callback, $wait);
48+
parent::request($subject, $payload, $decodeCallback, $wait);
3749
}
3850

3951
/**
40-
* @param string $subject
41-
* @param null $payload
52+
* Publish publishes the data argument to the given subject.
53+
*
54+
* @param string $subject Message topic.
55+
* @param string $payload Message data.
56+
*
57+
* @return void
4258
*/
43-
public function publish($subject, $payload = null) {
59+
public function publish($subject, $payload = null)
60+
{
4461
$payload = $this->encoder->encode($payload);
4562
parent::publish($subject, $payload);
4663
}
4764

4865
/**
49-
* @param string $subject
50-
* @param \Closure $callback
66+
* Subscribes to an specific event given a subject.
67+
*
68+
* @param string $subject Message topic.
69+
* @param \Closure $callback Closure to be executed as callback.
70+
*
71+
* @return string
5172
*/
52-
public function subscribe($subject, \Closure $callback) {
53-
$decode_callback = function ($payload) use ($callback) {
73+
public function subscribe($subject, \Closure $callback)
74+
{
75+
$decodeCallback = function ($payload) use ($callback) {
5476
$callback($this->encoder->decode($payload));
5577
};
56-
parent::subscribe($subject, $decode_callback);
78+
return parent::subscribe($subject, $decodeCallback);
5779
}
5880

5981
/**
60-
* @param string $subject
61-
* @param string $queue
62-
* @param \Closure $callback
82+
* Subscribes to an specific event given a subject and a queue.
83+
*
84+
* @param string $subject Message topic.
85+
* @param string $queue Queue name.
86+
* @param \Closure $callback Closure to be executed as callback.
87+
*
88+
* @return string
6389
*/
64-
public function queueSubscribe($subject, $queue, \Closure $callback) {
65-
$decode_callback = function ($payload) use ($callback) {
90+
public function queueSubscribe($subject, $queue, \Closure $callback)
91+
{
92+
$decodeCallback = function ($payload) use ($callback) {
6693
$callback($this->encoder->decode($payload));
6794
};
68-
parent::queueSubscribe($subject, $queue, $decode_callback);
95+
return parent::queueSubscribe($subject, $queue, $decodeCallback);
6996
}
70-
71-
}
97+
}

src/Nats/Encoders/Encoder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@
33

44
/**
55
* Interface Encoder
6+
*
67
* @package Nats\Encoders
78
*/
89
interface Encoder
910
{
11+
12+
1013
/**
14+
* Encodes a message.
15+
*
16+
* @param string $payload Message to decode.
17+
*
1118
* @return mixed
1219
*/
1320
public function encode($payload);
1421

1522
/**
23+
* Decodes a message.
24+
*
25+
* @param string $payload Message to decode.
26+
*
1627
* @return mixed
1728
*/
1829
public function decode($payload);

src/Nats/Encoders/JSONEncoder.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@
33

44
/**
55
* Class JSONEncoder
6+
*
7+
* Encodes and decodes messages in JSON format.
8+
*
69
* @package Nats
710
*/
8-
class JSONEncoder implements \Nats\Encoders\Encoder {
11+
class JSONEncoder implements \Nats\Encoders\Encoder
12+
{
913

10-
public function encode($payload) {
14+
15+
/**
16+
* Encodes a message to JSON.
17+
*
18+
* @param string $payload Message to decode.
19+
*
20+
* @return mixed
21+
*/
22+
public function encode($payload)
23+
{
1124
$payload = json_encode($payload);
1225
return $payload;
1326
}
1427

15-
public function decode($payload) {
28+
/**
29+
* Decodes a message from JSON.
30+
*
31+
* @param string $payload Message to decode.
32+
*
33+
* @return mixed
34+
*/
35+
public function decode($payload)
36+
{
1637
$payload = json_decode($payload);
1738
return $payload;
1839
}
19-
}
40+
}

test/ConnectionOptionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testSettersAndGettersWithoutCredentials()
6262
public function testStringRepresentation()
6363
{
6464
$options = new ConnectionOptions();
65-
$this->assertEquals('{"lang":"php","version":"0.8.0","verbose":false,"pedantic":false}', $options->__toString());
65+
$this->assertEquals('{"lang":"php","version":"0.8.2","verbose":false,"pedantic":false}', $options->__toString());
6666
}
6767

6868

@@ -76,6 +76,6 @@ public function testStringRepresentationWithCredentials()
7676
$options = new ConnectionOptions();
7777
$options->setUser('username');
7878
$options->setPass('password');
79-
$this->assertEquals('{"lang":"php","version":"0.8.0","verbose":false,"pedantic":false,"user":"username","pass":"password"}', $options->__toString());
79+
$this->assertEquals('{"lang":"php","version":"0.8.2","verbose":false,"pedantic":false,"user":"username","pass":"password"}', $options->__toString());
8080
}
8181
}

0 commit comments

Comments
 (0)