From 5a1ee6455eda901a2f531242205c6992691d8cea Mon Sep 17 00:00:00 2001 From: apiotrowski Date: Tue, 13 Nov 2018 22:54:15 +0100 Subject: [PATCH] :w Refactor ConvertManager, change constructor, replace array converters into Collection converters --- README.md | 2 +- composer.json | 3 +- composer.lock | 74 ++++++++++++++++++++++++++++++++++-- src/ConvertManager.php | 22 ++++++----- tests/ConvertManagerTest.php | 8 +++- 5 files changed, 92 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f8670d1..533c5b5 100644 --- a/README.md +++ b/README.md @@ -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'); ``` diff --git a/composer.json b/composer.json index 990f9f2..32fe45d 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ }, "require": { "php": "~7.1", - "ext-bcmath": "*" + "ext-bcmath": "*", + "doctrine/collections": "^1.5" }, "require-dev": { "phpunit/phpunit": "~7.4" diff --git a/composer.lock b/composer.lock index 831727f..4424e8c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,76 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d35916956ddd1752a03509425a6f7446", - "packages": [], + "content-hash": "4049ce06faf4a2fe2827d33399e1d03d", + "packages": [ + { + "name": "doctrine/collections", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/collections.git", + "reference": "a01ee38fcd999f34d9bfbcee59dbda5105449cbf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/collections/zipball/a01ee38fcd999f34d9bfbcee59dbda5105449cbf", + "reference": "a01ee38fcd999f34d9bfbcee59dbda5105449cbf", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "doctrine/coding-standard": "~0.1@dev", + "phpunit/phpunit": "^5.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Collections\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Collections Abstraction library", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "array", + "collections", + "iterator" + ], + "time": "2017-07-22T10:37:32+00:00" + } + ], "packages-dev": [ { "name": "doctrine/instantiator", @@ -1422,7 +1490,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "~7.0", + "php": "~7.1", "ext-bcmath": "*" }, "platform-dev": [] diff --git a/src/ConvertManager.php b/src/ConvertManager.php index f5ea3f0..7f481ac 100644 --- a/src/ConvertManager.php +++ b/src/ConvertManager.php @@ -2,6 +2,7 @@ namespace UnitConverter; +use Doctrine\Common\Collections\Collection; use UnitConverter\Converter\Converter; use UnitConverter\Exception\NotSupportedConversionException; use UnitConverter\Exception\NotSupportedUnitException; @@ -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; @@ -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); @@ -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(); } } diff --git a/tests/ConvertManagerTest.php b/tests/ConvertManagerTest.php index 81916ed..27dcd0e 100644 --- a/tests/ConvertManagerTest.php +++ b/tests/ConvertManagerTest.php @@ -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; @@ -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);