Commit inicial con archivos existentes
This commit is contained in:
3
vendor/react/partial/.gitignore
vendored
Executable file
3
vendor/react/partial/.gitignore
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
composer.phar
|
||||
composer.lock
|
||||
/vendor/
|
||||
16
vendor/react/partial/.travis.yml
vendored
Executable file
16
vendor/react/partial/.travis.yml
vendored
Executable file
@@ -0,0 +1,16 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- nightly
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- php: nightly
|
||||
|
||||
before_script:
|
||||
- composer install
|
||||
|
||||
script: composer run test
|
||||
31
vendor/react/partial/CHANGELOG.md
vendored
Executable file
31
vendor/react/partial/CHANGELOG.md
vendored
Executable file
@@ -0,0 +1,31 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
* 3.0.0 (2017-10-01)
|
||||
|
||||
* Modernize implementation (@shadowhand)
|
||||
|
||||
* 2.0.2 (2014-12-12)
|
||||
|
||||
* bind_right (@lstrojny)
|
||||
|
||||
* 2.0.1 (2014-10-20)
|
||||
|
||||
* Docblocks to make IDEs happy (@indeyets)
|
||||
|
||||
* 2.0.0 (2013-08-13)
|
||||
|
||||
* Rename from react/curry to react/partial
|
||||
|
||||
* 1.1.1 (2013-01-04)
|
||||
|
||||
* Remove `declare` statement, as it can cause notices when zend multibyte is disabled
|
||||
|
||||
* 1.1.0 (2013-01-03)
|
||||
|
||||
* Switch to function-based API (BC is provided)
|
||||
* Feature: Placeholders (@lstrojny)
|
||||
|
||||
* 1.0.0 (2012-11-07)
|
||||
|
||||
* First tagged release
|
||||
19
vendor/react/partial/LICENSE
vendored
Executable file
19
vendor/react/partial/LICENSE
vendored
Executable file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2012 Igor Wiedler
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
160
vendor/react/partial/README.md
vendored
Executable file
160
vendor/react/partial/README.md
vendored
Executable file
@@ -0,0 +1,160 @@
|
||||
# React/Partial
|
||||
|
||||
Partial function application.
|
||||
|
||||
[](http://travis-ci.org/reactphp/partial) [](https://codeclimate.com/github/reactphp/partial)
|
||||
|
||||
## Install
|
||||
|
||||
The recommended way to install react/partial is [through composer](http://getcomposer.org).
|
||||
|
||||
```JSON
|
||||
{
|
||||
"require": {
|
||||
"react/partial": "~2.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Concept
|
||||
|
||||
> Partial application (or partial function application) refers to the process
|
||||
> of fixing a number of arguments to a function, producing another function of
|
||||
> smaller arity. Given a function `f:(X x Y x Z) -> N`, we might fix (or
|
||||
> 'bind') the first argument, producing a function of type `f:(Y x Z) -> N`.
|
||||
> Evaluation of this function might be represented as `f partial(2, 3)`.
|
||||
> Note that the result of partial function application in this case is a
|
||||
> function that takes two arguments.
|
||||
|
||||
Basically, what this allows you to do is pre-fill arguments of a function,
|
||||
which is particularly useful if you don't have control over the function
|
||||
caller.
|
||||
|
||||
Let's say you have an async operation which takes a callback. How about a file
|
||||
download. The callback is called with a single argument: The contents of the
|
||||
file. Let's also say that you have a function that you want to be called once
|
||||
that file download completes. This function however needs to know an
|
||||
additional piece of information: the filename.
|
||||
|
||||
```php
|
||||
public function handleDownload($filename)
|
||||
{
|
||||
$this->downloadFile($filename, ...);
|
||||
}
|
||||
|
||||
public function downloadFile($filename, $callback)
|
||||
{
|
||||
$contents = get the darn file asynchronously...
|
||||
$callback($contents);
|
||||
}
|
||||
|
||||
public function processDownloadResult($filename, $contents)
|
||||
{
|
||||
echo "The file $filename contained a shitload of stuff:\n";
|
||||
echo $contents;
|
||||
}
|
||||
```
|
||||
|
||||
The conventional approach to this problem is to wrap everything in a closure
|
||||
like so:
|
||||
|
||||
```php
|
||||
public function handleDownload($filename)
|
||||
{
|
||||
$this->downloadFile($filename, function ($contents) use ($filename) {
|
||||
$this->processDownloadResult($filename, $contents);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
This is not too bad, especially with PHP 5.4, but with 5.3 you need to do the
|
||||
annoying `$that = $this` dance, and in general it's a lot of verbose
|
||||
boilerplate that you don't really want to litter your code with.
|
||||
|
||||
This is where partial application can help. Since we want to pre-fill an
|
||||
argument to the function that will be called, we just call `bind`, which will
|
||||
insert it to the left of the arguments list. The return value of `bind` is a
|
||||
new function which takes one `$content` argument.
|
||||
|
||||
```php
|
||||
use function React\Partial\bind;
|
||||
|
||||
public function handleDownload($filename)
|
||||
{
|
||||
$this->downloadFile($filename, bind([$this, 'processDownloadResult'], $filename));
|
||||
}
|
||||
```
|
||||
|
||||
Partialing is dependency injection for functions! How awesome is that?
|
||||
|
||||
## Examples
|
||||
|
||||
### bind
|
||||
|
||||
```php
|
||||
use function React\Partial\bind;
|
||||
|
||||
$add = function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
|
||||
$addOne = bind($add, 1);
|
||||
|
||||
echo sprintf("%d\n", $addOne(5));
|
||||
// outputs 6
|
||||
```
|
||||
|
||||
### bind_right
|
||||
|
||||
```php
|
||||
use function React\Partial\bind_right;
|
||||
|
||||
$div = function ($a, $b, $c) {
|
||||
return $a / $b / $c;
|
||||
};
|
||||
|
||||
$divMore = bind_right($div, 20, 10);
|
||||
|
||||
echo sprintf("%F\n", $divMore(100)); // 100 / 20 / 10
|
||||
// outputs 0.5
|
||||
```
|
||||
|
||||
### placeholder
|
||||
|
||||
It is possible to use the `…` function (there is an alias called
|
||||
`placeholder`) to skip some arguments when partially applying.
|
||||
|
||||
This allows you to pre-define arguments on the right, and have the left ones
|
||||
bound at call time.
|
||||
|
||||
This example skips the first argument and sets the second and third arguments
|
||||
to `0` and `1` respectively. The result is a function that returns the first
|
||||
character of a string.
|
||||
|
||||
**Note:** Usually your IDE should help but accessing the "…"-character
|
||||
(HORIZONTAL ELLIPSIS, U+2026) differs on various platforms.
|
||||
|
||||
- Windows: `ALT + 0133`
|
||||
- Mac: `ALT + ;` or `ALT + .`
|
||||
- Linux: `AltGr + .`
|
||||
|
||||
```php
|
||||
use function React\Partial\bind;
|
||||
use function React\Partial\…;
|
||||
|
||||
$firstChar = bind('substr', …(), 0, 1);
|
||||
$mapped = array_map($firstChar, array('foo', 'bar', 'baz'));
|
||||
|
||||
var_dump($mapped);
|
||||
// outputs ['f', 'b', 'b']
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
To run the test suite, you need PHPUnit.
|
||||
|
||||
$ phpunit
|
||||
|
||||
## License
|
||||
|
||||
MIT, see LICENSE.
|
||||
23
vendor/react/partial/composer.json
vendored
Executable file
23
vendor/react/partial/composer.json
vendored
Executable file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "react/partial",
|
||||
"description": "Partial function application.",
|
||||
"keywords": ["partial", "functional-programming"],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=5.6"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"React\\Partial\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"test": "phpunit"
|
||||
}
|
||||
}
|
||||
14
vendor/react/partial/examples/partial.php
vendored
Executable file
14
vendor/react/partial/examples/partial.php
vendored
Executable file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
use function React\Partial\bind;
|
||||
|
||||
$add = function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
|
||||
$addOne = bind($add, 1);
|
||||
|
||||
echo sprintf("%s\n", $addOne(5));
|
||||
// outputs 6
|
||||
30
vendor/react/partial/phpunit.xml.dist
vendored
Executable file
30
vendor/react/partial/phpunit.xml.dist
vendored
Executable file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="React/Partial Test Suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./src/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
<logging>
|
||||
<log type="coverage-text" target="php://stdout"/>
|
||||
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||
</logging>
|
||||
</phpunit>
|
||||
32
vendor/react/partial/src/Placeholder.php
vendored
Executable file
32
vendor/react/partial/src/Placeholder.php
vendored
Executable file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
final class Placeholder
|
||||
{
|
||||
private static $instance = null;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function resolve(array &$args, $position)
|
||||
{
|
||||
if (count($args) === 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Cannot resolve parameter placeholder at position %d. Parameter stack is empty.', $position)
|
||||
);
|
||||
}
|
||||
|
||||
return array_shift($args);
|
||||
}
|
||||
}
|
||||
16
vendor/react/partial/src/Util.php
vendored
Executable file
16
vendor/react/partial/src/Util.php
vendored
Executable file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
final class Util
|
||||
{
|
||||
public static function bind(/*$fn, $args...*/)
|
||||
{
|
||||
return call_user_func_array('React\Partial\bind', func_get_args());
|
||||
}
|
||||
|
||||
public static function bindRight(/*$fn, $args...*/)
|
||||
{
|
||||
return call_user_func_array('React\Partial\bind_right', func_get_args());
|
||||
}
|
||||
}
|
||||
63
vendor/react/partial/src/functions.php
vendored
Executable file
63
vendor/react/partial/src/functions.php
vendored
Executable file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
/**
|
||||
* @return callable
|
||||
*/
|
||||
function bind(callable $fn, ...$bound)
|
||||
{
|
||||
return function (...$args) use ($fn, $bound) {
|
||||
return $fn(...mergeLeft($bound, $args));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return callable
|
||||
*/
|
||||
function bind_right(callable $fn, ...$bound)
|
||||
{
|
||||
return function (...$args) use ($fn, $bound) {
|
||||
return $fn(...mergeRight($bound, $args));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Placeholder
|
||||
**/
|
||||
function …()
|
||||
{
|
||||
return Placeholder::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Placeholder
|
||||
**/
|
||||
function placeholder()
|
||||
{
|
||||
return …();
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function mergeLeft(array $left, array $right)
|
||||
{
|
||||
resolvePlaceholder($left, $right);
|
||||
return array_merge($left, $right);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function mergeRight(array $left, array $right)
|
||||
{
|
||||
resolvePlaceholder($left, $right);
|
||||
return array_merge($right, $left);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function resolvePlaceholder(array &$parameters, array &$source)
|
||||
{
|
||||
foreach ($parameters as $position => &$param) {
|
||||
if ($param instanceof Placeholder) {
|
||||
$param = $param->resolve($source, $position);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
vendor/react/partial/tests/BindRightTest.php
vendored
Executable file
81
vendor/react/partial/tests/BindRightTest.php
vendored
Executable file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
class BindRightTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testBindWithNoArgs()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$newDiv = bind_right($div);
|
||||
$this->assertSame(2, $newDiv(4, 2));
|
||||
}
|
||||
|
||||
public function testBindWithOneArg()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divOne = bind_right($div, 4);
|
||||
$this->assertSame(0.5, $divOne(2));
|
||||
}
|
||||
|
||||
public function testBindWithTwoArgs()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divTwo = bind_right($div, 2, 4);
|
||||
$this->assertSame(0.5, $divTwo());
|
||||
}
|
||||
|
||||
public function testBindWithPlaceholder()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divFun = bind_right($div, …(), 4);
|
||||
$this->assertSame(5, $divFun(20));
|
||||
$this->assertSame(10, $divFun(40));
|
||||
}
|
||||
|
||||
public function testBindWithMultiplePlaceholders()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divTwo = bind_right($div, …(), 2, …());
|
||||
$this->assertSame(1, $divTwo(4, 2));
|
||||
$this->assertSame(1, $divTwo(10, 5));
|
||||
$this->assertSame(25, $divTwo(100, 2));
|
||||
}
|
||||
|
||||
public function testPlaceholderParameterPosition()
|
||||
{
|
||||
$substr = bind_right('substr', …(), 0, …());
|
||||
$this->assertSame('foo', $substr('foo', 3));
|
||||
$this->assertSame('fo', $substr('foo', 2));
|
||||
$this->assertSame('f', $substr('foo', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage Cannot resolve parameter placeholder at position 0. Parameter stack is empty
|
||||
*/
|
||||
public function testStringConversion()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divTwo = bind_right($div, …(), 2);
|
||||
|
||||
$divTwo();
|
||||
}
|
||||
|
||||
public function testAliasForUnicodePlaceholderFunction()
|
||||
{
|
||||
$this->assertSame(…(), placeholder());
|
||||
}
|
||||
|
||||
private function createDivFunction()
|
||||
{
|
||||
return function () {
|
||||
$args = func_get_args();
|
||||
$value = array_shift($args);
|
||||
foreach ($args as $arg) {
|
||||
$value /= $arg;
|
||||
}
|
||||
return $value;
|
||||
};
|
||||
}
|
||||
}
|
||||
85
vendor/react/partial/tests/BindTest.php
vendored
Executable file
85
vendor/react/partial/tests/BindTest.php
vendored
Executable file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
class BindTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testBindWithNoArgs()
|
||||
{
|
||||
$add = $this->createAddFunction();
|
||||
$newAdd = bind($add);
|
||||
$this->assertSame(6, $newAdd(1, 5));
|
||||
}
|
||||
|
||||
public function testBindWithOneArg()
|
||||
{
|
||||
$add = $this->createAddFunction();
|
||||
$addOne = bind($add, 1);
|
||||
$this->assertSame(6, $addOne(5));
|
||||
}
|
||||
|
||||
public function testBindWithTwoArgs()
|
||||
{
|
||||
$add = $this->createAddFunction();
|
||||
$addOneAndFive = bind($add, 1, 5);
|
||||
$this->assertSame(6, $addOneAndFive());
|
||||
}
|
||||
|
||||
public function testBindWithPlaceholder()
|
||||
{
|
||||
$add = $this->createAddFunction();
|
||||
$addFun = bind($add, …(), 10);
|
||||
$this->assertSame(20, $addFun(10));
|
||||
$this->assertSame(30, $addFun(20));
|
||||
}
|
||||
|
||||
public function testBindWithMultiplePlaceholders()
|
||||
{
|
||||
$prod = $this->createProdFunction();
|
||||
$prodTwo = bind($prod, …(), 2, …());
|
||||
$this->assertSame(4, $prodTwo(1, 2));
|
||||
$this->assertSame(6, $prodTwo(1, 3));
|
||||
$this->assertSame(8, $prodTwo(2, 2));
|
||||
$this->assertSame(24, $prodTwo(3, 4));
|
||||
$this->assertSame(48, $prodTwo(3, 8));
|
||||
}
|
||||
|
||||
public function testPlaceholderParameterPosition()
|
||||
{
|
||||
$substr = bind('substr', …(), 0, …());
|
||||
$this->assertSame('foo', $substr('foo', 3));
|
||||
$this->assertSame('fo', $substr('foo', 2));
|
||||
$this->assertSame('f', $substr('foo', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage Cannot resolve parameter placeholder at position 0. Parameter stack is empty
|
||||
*/
|
||||
public function testStringConversion()
|
||||
{
|
||||
$add = $this->createAddFunction();
|
||||
$addTwo = bind($add, …(), 2);
|
||||
|
||||
$addTwo();
|
||||
}
|
||||
|
||||
public function testAliasForUnicodePlaceholderFunction()
|
||||
{
|
||||
$this->assertSame(…(), placeholder());
|
||||
}
|
||||
|
||||
private function createAddFunction()
|
||||
{
|
||||
return function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
}
|
||||
|
||||
private function createProdFunction()
|
||||
{
|
||||
return function ($a, $b, $c) {
|
||||
return $a * $b * $c;
|
||||
};
|
||||
}
|
||||
}
|
||||
29
vendor/react/partial/tests/UtilTest.php
vendored
Executable file
29
vendor/react/partial/tests/UtilTest.php
vendored
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace React\Partial;
|
||||
|
||||
use React\Partial\Util as Partial;
|
||||
|
||||
class UtilTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testBind()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divFun = Partial::bind($div, 10, 5);
|
||||
$this->assertSame(0.02, $divFun(100));
|
||||
}
|
||||
|
||||
public function testBindRight()
|
||||
{
|
||||
$div = $this->createDivFunction();
|
||||
$divFun = Partial::bindRight($div, 10, 5);
|
||||
$this->assertSame(2, $divFun(100));
|
||||
}
|
||||
|
||||
private function createDivFunction()
|
||||
{
|
||||
return function ($a, $b, $c) {
|
||||
return $a / $b / $c;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user