Skip to content

Commit f609840

Browse files
committed
first version
1 parent 480c0fe commit f609840

9 files changed

+357
-8
lines changed

.gitignore

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
composer.phar
2-
vendor/
3-
4-
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
1+
vendor
2+
composer.lock

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
- 5.6
8+
9+
install: composer install --prefer-source
10+
11+
script: vendor/bin/phpunit

README.md

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
1-
# php-repeat
2-
Repeater utility for composer/php
1+
Repeat
2+
=========
3+
4+
Repeat is a repeater utility for PHP / Composer.
5+
It makes it easy to repeat calling a function or similar operations.
6+
7+
Installation
8+
-----
9+
10+
#### Via composer
11+
Install the latest version with `composer require jamsouf/repeat`
12+
13+
``` php
14+
require 'vendor/autoload.php';
15+
```
16+
17+
Examples
18+
-----
19+
20+
##### Call a anonymous function 100 times
21+
22+
``` php
23+
$results = Repeat::_function(100, function () {
24+
// your function code
25+
});
26+
```
27+
28+
##### Call a named function and access in each call the current result
29+
30+
``` php
31+
$calculate = function ($result) {
32+
// your function code
33+
};
34+
35+
$results = Repeat::_function(20, $calculate);
36+
```
37+
38+
##### Call a named function until a specific condition
39+
40+
``` php
41+
$calculate = function ($result) {
42+
// your function code
43+
};
44+
45+
$until = function ($result) {
46+
// return true or false
47+
};
48+
49+
$results = Repeat::_function(50, $calculate, $until);
50+
```
51+
52+
##### Repeat a string with index reference and delimiter until a specific condition
53+
54+
``` php
55+
$result = Repeat::_string(10, 'v1.{j}.{i}', function ($result) {
56+
return strpos($result, '.4.') !== false ? true : false;
57+
}, ', ');
58+
59+
// => v1.1.0, v1.2.1, v1.3.2, v1.4.3
60+
```
61+
62+
API Documentation
63+
-----
64+
65+
#### **Repeat::_function**($count, $func, $until = _null_)
66+
_Repeat calling a function_
67+
* _integer_ `$count`: How often the function should be called
68+
* _callable_ `$func`: Function to call repeated
69+
* _callable_ `$until` (optional): Repeat calling until this function returns true
70+
* => return _array_: Results from each function call
71+
72+
#### **Repeat::_string**($count, $string, $until = _null_, $delimiter = _null_)
73+
_Repeat a string_
74+
* _integer_ `$count`: How often the string should be repeated
75+
* _string_ `$string`: String to repeat
76+
* _callable_ `$until` (optional): Repeat the string until this function returns true
77+
* _string_ `$delimiter` (optional): Signs to separate the strings
78+
* => return _string_: Repeated string

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "jamsouf/repeat",
3+
"description": "Repeater utility. Work with repeated actions in a simple way.",
4+
"keywords": ["repeat","times","call","iterate"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jamil Soufan",
10+
"homepage": "http://jamilsoufan.de"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.3.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "4.5.*"
18+
},
19+
"autoload": {
20+
"psr-4": {"Repeat\\": "src/Repeat"}
21+
},
22+
"extra": {
23+
"branch-alias": {
24+
"dev-master": "1.0-dev"
25+
}
26+
},
27+
"minimum-stability": "dev"
28+
}

phpunit.xml.dist

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="tests/bootstrap.php">
2+
<testsuites>
3+
<testsuite name="Repeat Test Suite">
4+
<directory>tests/Repeat/</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Repeat/Repeat.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Repeat;
4+
5+
/**
6+
* Repeater utility
7+
* @author Jamil Soufan (@jamsouf)
8+
*/
9+
class Repeat
10+
{
11+
/**
12+
* Repeat calling a function
13+
*
14+
* @param integer $count How often the function should be called
15+
* @param callable $func Function to call repeated
16+
* @param callable (optional) $until Repeat calling until this function returns true
17+
* @return array Results from each function call
18+
*/
19+
public static function _function($count, $func, $until = null)
20+
{
21+
$results = array();
22+
23+
for ($i = 0; $i < $count; $i++) {
24+
if ($until !== null && call_user_func($until, $results) === true) {
25+
break;
26+
}
27+
$results[] = call_user_func($func, $results);
28+
}
29+
30+
return $results;
31+
}
32+
33+
/**
34+
* Repeat a string
35+
*
36+
* @param integer $count How often the string should be repeated
37+
* @param string $string String to repeat
38+
* @param callable (optional) $until Repeat the string until this function returns true
39+
* @param string (optional) $delimiter Signs to separate the strings
40+
* @return string Repeated string
41+
*/
42+
public static function _string($count, $string, $until = null, $delimiter = null)
43+
{
44+
$result = '';
45+
46+
for ($i = 0; $i < $count; $i++) {
47+
if ($until !== null && call_user_func($until, $result) === true) {
48+
break;
49+
}
50+
$newString = str_replace('{i}', $i, $string);
51+
$newString = str_replace('{j}', $i+1, $newString);
52+
$newString = $newString . $delimiter;
53+
$result .= $newString;
54+
}
55+
56+
return trim($result, $delimiter);
57+
}
58+
}

tests/Repeat/RepeatFunctionTest.php

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Repeat;
4+
5+
class RepeatFunctionTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function testAnonymousFunction()
8+
{
9+
$count = 0;
10+
Repeat::_function(7, function () use (&$count) {
11+
$count++;
12+
});
13+
14+
$this->assertSame(7, $count);
15+
}
16+
17+
public function testAnonymousFunctionUntil()
18+
{
19+
$count = 0;
20+
Repeat::_function(12,
21+
function () use (&$count) {
22+
$count++;
23+
},
24+
function () use (&$count) {
25+
return $count == 5 ? true : false;
26+
}
27+
);
28+
29+
$this->assertSame(5, $count);
30+
}
31+
32+
public function testAnonymousFunctionWithReturnValue()
33+
{
34+
$count = 0;
35+
$results = Repeat::_function(23, function () use (&$count) {
36+
$count++;
37+
return 'foo';
38+
});
39+
40+
$this->assertSame(23, $count);
41+
$this->assertCount(23, $results);
42+
$this->assertSame('foo', $results[0]);
43+
$this->assertSame('foo', $results[22]);
44+
}
45+
46+
public function testNamedFunctionWithReturnValue()
47+
{
48+
$a = 3;
49+
$b = 6;
50+
51+
$multiplication = function () use (&$a, &$b) {
52+
return $a++ * $b++;
53+
};
54+
55+
$results = Repeat::_function(4, $multiplication);
56+
57+
$this->assertCount(4, $results);
58+
$this->assertSame(18, $results[0]);
59+
$this->assertSame(28, $results[1]);
60+
$this->assertSame(40, $results[2]);
61+
$this->assertSame(54, $results[3]);
62+
}
63+
64+
public function testNamedFunctionAnonymousUntilWithReturnValue()
65+
{
66+
$a = 84;
67+
$b = 7;
68+
69+
$subtraction = function () use (&$a, &$b) {
70+
return --$a - ++$b;
71+
};
72+
73+
$results = Repeat::_function(6, $subtraction, function () use (&$a) {
74+
return $a <= 81 ? true : false;
75+
});
76+
77+
$this->assertCount(3, $results);
78+
$this->assertSame(75, $results[0]);
79+
$this->assertSame(73, $results[1]);
80+
$this->assertSame(71, $results[2]);
81+
}
82+
83+
public function testNamedFunctionNamedUntilWithResultReferenceAndReturnValue()
84+
{
85+
$createRandomNumbers = function () {
86+
return mt_rand(0, 1000);
87+
};
88+
89+
$until = function ($result) {
90+
return count($result) == 3 ? true : false;
91+
};
92+
93+
$results = Repeat::_function(9, $createRandomNumbers, $until);
94+
95+
$this->assertCount(3, $results);
96+
$this->assertTrue($results[0] >= 0 && $results[0] <= 1000);
97+
$this->assertTrue($results[1] >= 0 && $results[1] <= 1000);
98+
$this->assertTrue($results[2] >= 0 && $results[2] <= 1000);
99+
}
100+
101+
public function testNamedFunctionWithResultReferenceAndReturnValue()
102+
{
103+
$start = 4;
104+
$math = function ($result) use ($start) {
105+
$value = count($result) == 0 ? $start : $result[max(array_keys($result))];
106+
return $value * $value;
107+
};
108+
109+
$results = Repeat::_function(4, $math);
110+
111+
$this->assertCount(4, $results);
112+
$this->assertSame(16, $results[0]);
113+
$this->assertSame(256, $results[1]);
114+
$this->assertSame(65536, $results[2]);
115+
$this->assertSame(4294967296, $results[3]);
116+
}
117+
}

tests/Repeat/RepeatStringTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Repeat;
4+
5+
class RepeatStringTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function testString()
8+
{
9+
$result = Repeat::_string(4, 'Lorem ipsum ');
10+
11+
$this->assertSame('Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum ', $result);
12+
}
13+
14+
public function testStringWithIndexAndStep()
15+
{
16+
$result = Repeat::_string(3, '[{i}] Round{j} ');
17+
18+
$this->assertSame('[0] Round1 [1] Round2 [2] Round3 ', $result);
19+
}
20+
21+
public function testStringCount0()
22+
{
23+
$result = Repeat::_string(0, 'blub');
24+
25+
$this->assertEmpty($result);
26+
}
27+
28+
public function testStringUntilWithResultReference()
29+
{
30+
$result = Repeat::_string(17, 'foo', function ($result) {
31+
return substr_count($result, 'foo') == 2 ? true : false;
32+
});
33+
34+
$this->assertSame('foofoo', $result);
35+
}
36+
37+
public function testStringWithDelimiter()
38+
{
39+
$result = Repeat::_string(4, 'attribute-{i}', null, ', ');
40+
41+
$this->assertSame('attribute-0, attribute-1, attribute-2, attribute-3', $result);
42+
}
43+
44+
public function testStringUntilWithDelimiter()
45+
{
46+
$result = Repeat::_string(10, 'v1.{i}', function ($result) {
47+
return strpos($result, '.4') !== false ? true : false;
48+
}, ' / ');
49+
50+
$this->assertSame('v1.0 / v1.1 / v1.2 / v1.3 / v1.4', $result);
51+
}
52+
}

tests/bootstrap.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$loader = require __DIR__ . "/../vendor/autoload.php";
4+
$loader->addPsr4('Repeat\\', __DIR__.'/Repeat');

0 commit comments

Comments
 (0)