Skip to content

Commit f7637ee

Browse files
committed
Added glue option.
1 parent 3e2c7d2 commit f7637ee

File tree

4 files changed

+60
-52
lines changed

4 files changed

+60
-52
lines changed

README.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -110,52 +110,52 @@ cb
110110
cc
111111
```
112112

113-
**Using an array as first argument**
113+
**Using an array as first argument, and a separator**
114114

115115
```php
116-
foreach (string_combinations(['woof', 'meow', 'roar'], 2, 3) as $combination) {
116+
foreach (string_combinations(['woof', 'meow', 'roar'], 2, 3, '-') as $combination) {
117117
echo $combination . PHP_EOL;
118118
}
119119
```
120120

121121
Output:
122122
```
123-
woofwoof
124-
woofmeow
125-
woofroar
126-
meowwoof
127-
meowmeow
128-
meowroar
129-
roarwoof
130-
roarmeow
131-
roarroar
132-
woofwoofwoof
133-
woofwoofmeow
134-
woofwoofroar
135-
woofmeowwoof
136-
woofmeowmeow
137-
woofmeowroar
138-
woofroarwoof
139-
woofroarmeow
140-
woofroarroar
141-
meowwoofwoof
142-
meowwoofmeow
143-
meowwoofroar
144-
meowmeowwoof
145-
meowmeowmeow
146-
meowmeowroar
147-
meowroarwoof
148-
meowroarmeow
149-
meowroarroar
150-
roarwoofwoof
151-
roarwoofmeow
152-
roarwoofroar
153-
roarmeowwoof
154-
roarmeowmeow
155-
roarmeowroar
156-
roarroarwoof
157-
roarroarmeow
158-
roarroarroar
123+
woof-woof
124+
woof-meow
125+
woof-roar
126+
meow-woof
127+
meow-meow
128+
meow-roar
129+
roar-woof
130+
roar-meow
131+
roar-roar
132+
woof-woof-woof
133+
woof-woof-meow
134+
woof-woof-roar
135+
woof-meow-woof
136+
woof-meow-meow
137+
woof-meow-roar
138+
woof-roar-woof
139+
woof-roar-meow
140+
woof-roar-roar
141+
meow-woof-woof
142+
meow-woof-meow
143+
meow-woof-roar
144+
meow-meow-woof
145+
meow-meow-meow
146+
meow-meow-roar
147+
meow-roar-woof
148+
meow-roar-meow
149+
meow-roar-roar
150+
roar-woof-woof
151+
roar-woof-meow
152+
roar-woof-roar
153+
roar-meow-woof
154+
roar-meow-meow
155+
roar-meow-roar
156+
roar-roar-woof
157+
roar-roar-meow
158+
roar-roar-roar
159159
```
160160

161161
Performance considerations

src/StringCombinations.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ class StringCombinations implements \IteratorAggregate, \Countable
2626
*/
2727
private $count;
2828

29+
/**
30+
* @var string
31+
*/
32+
private $glue;
33+
2934
/**
3035
* StringCombination constructor.
3136
* @param mixed $charset
3237
* @param int $min
3338
* @param int $max
39+
* @param string $glue
40+
* @throws \InvalidArgumentException
3441
*/
35-
public function __construct($charset, $min = 1, $max = null)
42+
public function __construct($charset, $min = 1, $max = null, $glue = '')
3643
{
3744
if (is_string($charset) || is_integer($charset)) {
3845
$this->charset = preg_split('/(?<!^)(?!$)/u', $charset);
@@ -45,6 +52,7 @@ public function __construct($charset, $min = 1, $max = null)
4552
}
4653
$this->min = (int) $min;
4754
$this->max = is_null($max) ? count($this->charset) : (int) $max;
55+
$this->glue = $glue;
4856
}
4957

5058
/**
@@ -67,7 +75,7 @@ public function getIterator()
6775
{
6876
foreach ($this->generateSets() as $set) {
6977
foreach (cartesian_product($set) as $combination) {
70-
yield implode('', $combination);
78+
yield implode($this->glue, $combination);
7179
}
7280
}
7381
}

src/function.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @param int|null $max
1111
* @return StringCombinations
1212
*/
13-
function string_combinations($charset, $min = 1, $max = null)
13+
function string_combinations($charset, $min = 1, $max = null, $glue = '')
1414
{
15-
return new StringCombinations($charset, $min, $max);
15+
return new StringCombinations($charset, $min, $max, $glue);
1616
}

tests/TestStringCombinations.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ public function testWithMinAndMaxValue()
133133

134134
public function testWithArrayCharset()
135135
{
136-
$sc = string_combinations(['a1', 'b2', 'c3'], 2, 2);
136+
$sc = string_combinations(['a1', 'b2', 'c3'], 2, 2, '-');
137137
$expectedCount = 9; // (3^2)
138138
$expectedResult = [
139-
'a1a1',
140-
'a1b2',
141-
'a1c3',
142-
'b2a1',
143-
'b2b2',
144-
'b2c3',
145-
'c3a1',
146-
'c3b2',
147-
'c3c3',
139+
'a1-a1',
140+
'a1-b2',
141+
'a1-c3',
142+
'b2-a1',
143+
'b2-b2',
144+
'b2-c3',
145+
'c3-a1',
146+
'c3-b2',
147+
'c3-c3',
148148
];
149149
$this->assertCount($expectedCount, $sc);
150150
$result = $sc->asArray();

0 commit comments

Comments
 (0)