Skip to content

Commit

Permalink
:w Refactor ConvertManager, change constructor, replace array convert…
Browse files Browse the repository at this point in the history
…ers into Collection converters
  • Loading branch information
apiotrowski committed Nov 13, 2018
1 parent 4e1a5e3 commit 5a1ee64
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ If you want to convert values not using Convert Manager you can do it directly b
Unit Converter is really simple to use and easy to extend. In a bellow example I show how to use this tool.

```php
$convertManager = new ConvertManager([ new LengthConverter(), new WeightConverter() ]);
$convertManager = new ConvertManager(new ArrayCollection([ new LengthConverter(), new WeightConverter() ]));
$convertedValue = $convertManager->convert('10cm to ?in');
```

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"require": {
"php": "~7.1",
"ext-bcmath": "*"
"ext-bcmath": "*",
"doctrine/collections": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "~7.4"
Expand Down
74 changes: 71 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/ConvertManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UnitConverter;

use Doctrine\Common\Collections\Collection;
use UnitConverter\Converter\Converter;
use UnitConverter\Exception\NotSupportedConversionException;
use UnitConverter\Exception\NotSupportedUnitException;
Expand All @@ -22,10 +23,10 @@ class ConvertManager
private $queryResolver;

/**
* @param Converter[] $converters
* @param Converter[]|Collection $converters
* @param QueryResolver $queryResolver
*/
public function __construct(array $converters, QueryResolver $queryResolver)
public function __construct(Collection $converters, QueryResolver $queryResolver)
{
$this->converters = $converters;
$this->queryResolver = $queryResolver;
Expand All @@ -43,7 +44,6 @@ public function __construct(array $converters, QueryResolver $queryResolver)
public function convert(string $rawQuery) : Value
{
$query = $this->queryResolver->resolve($rawQuery);

$converter = $this->getSupportedConverter($query);

return $converter->convertFromQuery($query);
Expand All @@ -58,15 +58,17 @@ public function convert(string $rawQuery) : Value
*/
protected function getSupportedConverter(Query $query) : Converter
{
$valueUnit = $query->getValue()->getUnit();
$targetUnit = $query->getTargetUnit();
$supportedConverters = $this->converters->filter(function (Converter $converter) use ($query) {
return true === $converter->isSupported($query->getValue()->getUnit(), $query->getTargetUnit());
});

foreach ($this->converters as $converter) {
if (true === $converter->isSupported($valueUnit, $targetUnit)) {
return $converter;
}
if (true === $supportedConverters->isEmpty()) {
throw new NotSupportedConversionException(
$query->getValue()->getUnit(),
$query->getTargetUnit()
);
}

throw new NotSupportedConversionException($valueUnit, $targetUnit);
return $supportedConverters->first();
}
}
8 changes: 6 additions & 2 deletions tests/ConvertManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UnitConverter;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
use UnitConverter\Converter\LengthConverter;
use UnitConverter\Converter\WeightConverter;
Expand All @@ -20,10 +22,12 @@ class ConvertManagerTest extends TestCase
*/
public function testConvertReturnConvertedValue(string $rawQuery)
{
$convertManager = new ConvertManager([
$converters = new ArrayCollection([
new LengthConverter(),
new WeightConverter()
], new QueryResolver());
]);

$convertManager = new ConvertManager($converters, new QueryResolver());

$convertedValue = $convertManager->convert($rawQuery);

Expand Down

0 comments on commit 5a1ee64

Please sign in to comment.