Skip to content

Commit

Permalink
Merge pull request #9 from drupol/interfaces-cleanup
Browse files Browse the repository at this point in the history
Interfaces cleanup
  • Loading branch information
drupol committed Apr 20, 2018
2 parents a494787 + 2809276 commit ff8a6e9
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 313 deletions.
38 changes: 37 additions & 1 deletion src/Combinatorics.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(array $dataset = [], $length = null)
*/
public function setLength($length = null)
{
$length = is_null($length) ? $this->datasetCount : $length;
$length = (null === $length) ? $this->datasetCount : $length;
$this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length;

return $this;
Expand Down Expand Up @@ -96,6 +96,42 @@ public function getDataset()
return $this->dataset;
}

/**
* Count elements of an object.
*
* @return int
* The number of element
*/
public function count()
{
return count($this->toArray());
}

/**
* Convert the iterator into an array.
*
* @return array
* The elements
*/
public function toArray()
{
$data = [];

for ($this->rewind(); $this->valid(); $this->next()) {
$data[] = $this->current();
}

return $data;
}

/**
* {@inheritdoc}
*/
public function key()
{
return $this->key;
}

/**
* Compute the factorial of an integer.
*
Expand Down
21 changes: 21 additions & 0 deletions src/GeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace drupol\phpermutations;

interface GeneratorInterface
{
/**
* Get the generator.
*
* @return \Generator
* The generator
*/
public function generator();

/**
* Convert the generator into an array.
*
* @return array
*/
public function toArray();
}
13 changes: 4 additions & 9 deletions src/Generators/Combinations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Combinations as CombinationsIterator;

/**
Expand All @@ -10,24 +11,18 @@
*
* @author Mark Wilson <mark@89allport.co.uk>
*/
class Combinations extends CombinationsIterator
class Combinations extends CombinationsIterator implements GeneratorInterface
{
/**
* Alias of the get() method.
*
* @return \Generator
* The prime generator
* {@inheritdoc}
*/
public function generator()
{
return $this->get($this->getDataset(), $this->getLength());
}

/**
* Convert the generator into an array.
*
* @return array
* The elements
* {@inheritdoc}
*/
public function toArray()
{
Expand Down
15 changes: 3 additions & 12 deletions src/Generators/Fibonacci.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Fibonacci as FibonacciIterator;

/**
* Class Fibonacci.
*/
class Fibonacci extends FibonacciIterator
class Fibonacci extends FibonacciIterator implements GeneratorInterface
{
/**
* The maximum value.
*
* @var int
*/
protected $max;

/**
* Alias of the get() method.
*
* @return \Generator
* The prime generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Generators/FiniteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\FiniteGroup as FiniteGroupIterator;

/**
* Class FiniteGroup.
*
* The finite group is an abelian finite cyclic group.
*/
class FiniteGroup extends FiniteGroupIterator
class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface
{
/**
* Alias of the get() method.
*
* @return \Generator
* The finite group generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
45 changes: 45 additions & 0 deletions src/Generators/NGrams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\NGrams as NGramsIterator;

/**
* Class NGrams.
*/
class NGrams extends NGramsIterator implements GeneratorInterface
{
/**
* {@inheritdoc}
*/
public function generator()
{
return $this->get();
}

/**
* {@inheritdoc}
*/
public function toArray()
{
return [];
}

/**
* Get the generator.
*
* @codingStandardsIgnoreStart
*
* @return \Generator
* The generator
* @codingStandardsIgnoreEnd
*/
protected function get()
{
while (true) {
$this->next();
yield $this->current();
}
}
}
15 changes: 3 additions & 12 deletions src/Generators/Perfect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Perfect as PerfectIterator;

/**
* Class Perfect.
*/
class Perfect extends PerfectIterator
class Perfect extends PerfectIterator implements GeneratorInterface
{
/**
* The maximum value.
*
* @var int
*/
protected $max;

/**
* Alias of the get() method.
*
* @return \Generator
* The prime generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
15 changes: 4 additions & 11 deletions src/Generators/Permutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace drupol\phpermutations\Generators;

use drupol\phpermutations\Combinatorics;
use drupol\phpermutations\GeneratorInterface;

/**
* Class Permutations.
*/
class Permutations extends Combinatorics
class Permutations extends Combinatorics implements GeneratorInterface
{
/**
* The combinations generator.
Expand All @@ -31,12 +32,7 @@ public function __construct(array $dataset = [], $length = null)
}

/**
* Alias of the get() method.
*
* @codingStandardsIgnoreStart
*
* @return \Generator
* @codingStandardsIgnoreEnd
* {@inheritdoc}
*/
public function generator()
{
Expand All @@ -48,10 +44,7 @@ public function generator()
}

/**
* Convert the generator into an array.
*
* @return array
* The elements
* {@inheritdoc}
*/
public function toArray()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Generators/Prime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Prime as PrimeIterator;

/**
* Class Prime.
*/
class Prime extends PrimeIterator
class Prime extends PrimeIterator implements GeneratorInterface
{
/**
* Alias of the get() method.
*
* @return \Generator
* The prime generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Generators/PrimeFactors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\PrimeFactors as PrimeFactorsIterator;

/**
* Class PrimeFactors.
*/
class PrimeFactors extends PrimeFactorsIterator
class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface
{
/**
* Alias of the get() method.
*
* @return \Generator
* The prime factors generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Generators/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Product as ProductIterator;

/**
* Class Product.
*/
class Product extends ProductIterator
class Product extends ProductIterator implements GeneratorInterface
{
/**
* Get the generator.
*
* @return \Generator
* The generator
* {@inheritdoc}
*/
public function generator()
{
Expand Down
16 changes: 11 additions & 5 deletions src/Generators/Shift.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

namespace drupol\phpermutations\Generators;

use drupol\phpermutations\GeneratorInterface;
use drupol\phpermutations\Iterators\Shift as ShiftIterator;

/**
* Class Shift.
*/
class Shift extends ShiftIterator
class Shift extends ShiftIterator implements GeneratorInterface
{
/**
* Get the generator.
*
* @return \Generator
* The generator
* {@inheritdoc}
*/
public function generator()
{
return $this->get();
}

/**
* {@inheritdoc}
*/
public function toArray()
{
return [];
}

/**
* Get the generator.
*
Expand Down
7 changes: 7 additions & 0 deletions src/IteratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace drupol\phpermutations;

interface IteratorInterface extends \Iterator
{
}

0 comments on commit ff8a6e9

Please sign in to comment.