Skip to content

Commit 13a5f9d

Browse files
committed
Initial Commit
0 parents  commit 13a5f9d

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
phpunit.xml
2+
composer.lock
3+
vendor/
4+
.idea
5+
.DS_STORE

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
===============
2+
Helper Functions for RxPHP
3+
===============
4+
5+
```PHP
6+
Observable::of(Observable::fromArray([1, 2, 3, 4, 5]))
7+
->filter(\Rx\is(Observable::class))
8+
->mergeAll()
9+
->filter(\Rx\notEqualTo(2))
10+
->filter('rx\even')
11+
->reduce('rx\add')
12+
->map(\Rx\p('rx\mult', 3))
13+
->map(\Rx\p('rx\sub', 2))
14+
->map(\Rx\p('rx\concatLeft', 'total: '))
15+
->compose(\Rx\echoFinally('Test '))
16+
->subscribe(\Rx\echoObserver('Test '));
17+
18+
19+
//Test Next value: total: 10
20+
//Test Complete!
21+
//Test Finally!
22+
```

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "rx/helpers",
3+
"type": "library",
4+
"description": "A collection of functions to use with RxPHP",
5+
"keywords": [
6+
"rx",
7+
"rx.php",
8+
"rxphp",
9+
"react",
10+
"reactive"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Matt Bonneau",
16+
"email": "[email protected]",
17+
"role": "Developer"
18+
},
19+
{
20+
"name": "David Dan",
21+
"email": "[email protected]",
22+
"role": "Developer"
23+
}
24+
],
25+
"autoload": {
26+
"psr-4": {
27+
"Rx\\Websocket\\": "src/"
28+
}
29+
},
30+
"require": {
31+
"reactivex/rxphp": "^2.0"
32+
},
33+
"autoload": {
34+
"files": [
35+
"src/functions.php"
36+
]
37+
}
38+
}

src/functions.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Rx;
4+
5+
use Rx\Observer\CallbackObserver;
6+
7+
function is($class): callable
8+
{
9+
return function ($value) use ($class): bool {
10+
return is_a($value, $class);
11+
};
12+
}
13+
14+
function equalTo($a): callable
15+
{
16+
return function ($b) use ($a): bool {
17+
return $a == $b;
18+
};
19+
}
20+
21+
function notEqualTo($a): callable
22+
{
23+
return function ($b) use ($a): bool {
24+
return $a != $b;
25+
};
26+
}
27+
28+
function same($a): callable
29+
{
30+
return function ($b) use ($a): bool {
31+
return $a === $b;
32+
};
33+
}
34+
35+
function notSame($a): callable
36+
{
37+
return function ($b) use ($a): bool {
38+
return $a !== $b;
39+
};
40+
}
41+
42+
function add($accum, $value)
43+
{
44+
return $accum + $value;
45+
}
46+
47+
function concat($accum, $value): string
48+
{
49+
return $accum . $value;
50+
}
51+
52+
function concatLeft($accum, $value): string
53+
{
54+
return $value . $accum;
55+
}
56+
57+
function sub($accum, $value)
58+
{
59+
return $accum - $value;
60+
}
61+
62+
function mult($accum, $value)
63+
{
64+
return $accum * $value;
65+
}
66+
67+
function echoLine($value)
68+
{
69+
echo $value, PHP_EOL;
70+
}
71+
72+
function odd($number)
73+
{
74+
return $number % 2 !== 0;
75+
}
76+
77+
function even($number)
78+
{
79+
return $number % 2 === 0;
80+
}
81+
82+
function echoObserver($prefix = '')
83+
{
84+
return new CallbackObserver(
85+
function ($value) use ($prefix) {
86+
echo $prefix . 'Next value: ' . asString($value) . PHP_EOL;
87+
},
88+
function (\Throwable $error) use ($prefix) {
89+
echo $prefix . 'Exception: ' . $error->getMessage() . PHP_EOL;
90+
},
91+
function () use ($prefix) {
92+
echo $prefix . 'Complete! ' . PHP_EOL;
93+
}
94+
);
95+
}
96+
97+
function echoFinally($prefix = ''): callable
98+
{
99+
return function (Observable $observable) use ($prefix): Observable {
100+
return $observable->finally(function () use ($prefix) {
101+
echo $prefix . 'Finally! ' . PHP_EOL;
102+
});
103+
};
104+
}
105+
106+
function asString($value): string
107+
{
108+
if (is_array($value)) {
109+
return json_encode($value);
110+
}
111+
112+
if (is_bool($value)) {
113+
return (string)(integer)$value;
114+
}
115+
116+
return (string)$value;
117+
}
118+
119+
/**
120+
* Partial Apply
121+
*
122+
* @param callable $func
123+
* @param $lastValue
124+
* @return callable
125+
*
126+
*/
127+
function p(callable $func, $lastValue): callable
128+
{
129+
return function ($value) use ($func, $lastValue) {
130+
return $func($value, $lastValue);
131+
};
132+
}

0 commit comments

Comments
 (0)