Skip to content

Commit ff8a6e9

Browse files
authored
Merge pull request #9 from drupol/interfaces-cleanup
Interfaces cleanup
2 parents a494787 + 2809276 commit ff8a6e9

25 files changed

+192
-313
lines changed

src/Combinatorics.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(array $dataset = [], $length = null)
5353
*/
5454
public function setLength($length = null)
5555
{
56-
$length = is_null($length) ? $this->datasetCount : $length;
56+
$length = (null === $length) ? $this->datasetCount : $length;
5757
$this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length;
5858

5959
return $this;
@@ -96,6 +96,42 @@ public function getDataset()
9696
return $this->dataset;
9797
}
9898

99+
/**
100+
* Count elements of an object.
101+
*
102+
* @return int
103+
* The number of element
104+
*/
105+
public function count()
106+
{
107+
return count($this->toArray());
108+
}
109+
110+
/**
111+
* Convert the iterator into an array.
112+
*
113+
* @return array
114+
* The elements
115+
*/
116+
public function toArray()
117+
{
118+
$data = [];
119+
120+
for ($this->rewind(); $this->valid(); $this->next()) {
121+
$data[] = $this->current();
122+
}
123+
124+
return $data;
125+
}
126+
127+
/**
128+
* {@inheritdoc}
129+
*/
130+
public function key()
131+
{
132+
return $this->key;
133+
}
134+
99135
/**
100136
* Compute the factorial of an integer.
101137
*

src/GeneratorInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace drupol\phpermutations;
4+
5+
interface GeneratorInterface
6+
{
7+
/**
8+
* Get the generator.
9+
*
10+
* @return \Generator
11+
* The generator
12+
*/
13+
public function generator();
14+
15+
/**
16+
* Convert the generator into an array.
17+
*
18+
* @return array
19+
*/
20+
public function toArray();
21+
}

src/Generators/Combinations.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Combinations as CombinationsIterator;
67

78
/**
@@ -10,24 +11,18 @@
1011
*
1112
* @author Mark Wilson <mark@89allport.co.uk>
1213
*/
13-
class Combinations extends CombinationsIterator
14+
class Combinations extends CombinationsIterator implements GeneratorInterface
1415
{
1516
/**
16-
* Alias of the get() method.
17-
*
18-
* @return \Generator
19-
* The prime generator
17+
* {@inheritdoc}
2018
*/
2119
public function generator()
2220
{
2321
return $this->get($this->getDataset(), $this->getLength());
2422
}
2523

2624
/**
27-
* Convert the generator into an array.
28-
*
29-
* @return array
30-
* The elements
25+
* {@inheritdoc}
3126
*/
3227
public function toArray()
3328
{

src/Generators/Fibonacci.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Fibonacci as FibonacciIterator;
67

78
/**
89
* Class Fibonacci.
910
*/
10-
class Fibonacci extends FibonacciIterator
11+
class Fibonacci extends FibonacciIterator implements GeneratorInterface
1112
{
1213
/**
13-
* The maximum value.
14-
*
15-
* @var int
16-
*/
17-
protected $max;
18-
19-
/**
20-
* Alias of the get() method.
21-
*
22-
* @return \Generator
23-
* The prime generator
14+
* {@inheritdoc}
2415
*/
2516
public function generator()
2617
{

src/Generators/FiniteGroup.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\FiniteGroup as FiniteGroupIterator;
67

78
/**
89
* Class FiniteGroup.
910
*
1011
* The finite group is an abelian finite cyclic group.
1112
*/
12-
class FiniteGroup extends FiniteGroupIterator
13+
class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface
1314
{
1415
/**
15-
* Alias of the get() method.
16-
*
17-
* @return \Generator
18-
* The finite group generator
16+
* {@inheritdoc}
1917
*/
2018
public function generator()
2119
{

src/Generators/NGrams.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace drupol\phpermutations\Generators;
4+
5+
use drupol\phpermutations\GeneratorInterface;
6+
use drupol\phpermutations\Iterators\NGrams as NGramsIterator;
7+
8+
/**
9+
* Class NGrams.
10+
*/
11+
class NGrams extends NGramsIterator implements GeneratorInterface
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function generator()
17+
{
18+
return $this->get();
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function toArray()
25+
{
26+
return [];
27+
}
28+
29+
/**
30+
* Get the generator.
31+
*
32+
* @codingStandardsIgnoreStart
33+
*
34+
* @return \Generator
35+
* The generator
36+
* @codingStandardsIgnoreEnd
37+
*/
38+
protected function get()
39+
{
40+
while (true) {
41+
$this->next();
42+
yield $this->current();
43+
}
44+
}
45+
}

src/Generators/Perfect.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Perfect as PerfectIterator;
67

78
/**
89
* Class Perfect.
910
*/
10-
class Perfect extends PerfectIterator
11+
class Perfect extends PerfectIterator implements GeneratorInterface
1112
{
1213
/**
13-
* The maximum value.
14-
*
15-
* @var int
16-
*/
17-
protected $max;
18-
19-
/**
20-
* Alias of the get() method.
21-
*
22-
* @return \Generator
23-
* The prime generator
14+
* {@inheritdoc}
2415
*/
2516
public function generator()
2617
{

src/Generators/Permutations.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace drupol\phpermutations\Generators;
44

55
use drupol\phpermutations\Combinatorics;
6+
use drupol\phpermutations\GeneratorInterface;
67

78
/**
89
* Class Permutations.
910
*/
10-
class Permutations extends Combinatorics
11+
class Permutations extends Combinatorics implements GeneratorInterface
1112
{
1213
/**
1314
* The combinations generator.
@@ -31,12 +32,7 @@ public function __construct(array $dataset = [], $length = null)
3132
}
3233

3334
/**
34-
* Alias of the get() method.
35-
*
36-
* @codingStandardsIgnoreStart
37-
*
38-
* @return \Generator
39-
* @codingStandardsIgnoreEnd
35+
* {@inheritdoc}
4036
*/
4137
public function generator()
4238
{
@@ -48,10 +44,7 @@ public function generator()
4844
}
4945

5046
/**
51-
* Convert the generator into an array.
52-
*
53-
* @return array
54-
* The elements
47+
* {@inheritdoc}
5548
*/
5649
public function toArray()
5750
{

src/Generators/Prime.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Prime as PrimeIterator;
67

78
/**
89
* Class Prime.
910
*/
10-
class Prime extends PrimeIterator
11+
class Prime extends PrimeIterator implements GeneratorInterface
1112
{
1213
/**
13-
* Alias of the get() method.
14-
*
15-
* @return \Generator
16-
* The prime generator
14+
* {@inheritdoc}
1715
*/
1816
public function generator()
1917
{

src/Generators/PrimeFactors.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\PrimeFactors as PrimeFactorsIterator;
67

78
/**
89
* Class PrimeFactors.
910
*/
10-
class PrimeFactors extends PrimeFactorsIterator
11+
class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface
1112
{
1213
/**
13-
* Alias of the get() method.
14-
*
15-
* @return \Generator
16-
* The prime factors generator
14+
* {@inheritdoc}
1715
*/
1816
public function generator()
1917
{

src/Generators/Product.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Product as ProductIterator;
67

78
/**
89
* Class Product.
910
*/
10-
class Product extends ProductIterator
11+
class Product extends ProductIterator implements GeneratorInterface
1112
{
1213
/**
13-
* Get the generator.
14-
*
15-
* @return \Generator
16-
* The generator
14+
* {@inheritdoc}
1715
*/
1816
public function generator()
1917
{

src/Generators/Shift.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22

33
namespace drupol\phpermutations\Generators;
44

5+
use drupol\phpermutations\GeneratorInterface;
56
use drupol\phpermutations\Iterators\Shift as ShiftIterator;
67

78
/**
89
* Class Shift.
910
*/
10-
class Shift extends ShiftIterator
11+
class Shift extends ShiftIterator implements GeneratorInterface
1112
{
1213
/**
13-
* Get the generator.
14-
*
15-
* @return \Generator
16-
* The generator
14+
* {@inheritdoc}
1715
*/
1816
public function generator()
1917
{
2018
return $this->get();
2119
}
2220

21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function toArray()
25+
{
26+
return [];
27+
}
28+
2329
/**
2430
* Get the generator.
2531
*

src/IteratorInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace drupol\phpermutations;
4+
5+
interface IteratorInterface extends \Iterator
6+
{
7+
}

0 commit comments

Comments
 (0)