From cedf426a186e837df3acd1dcaf6ca3ad28f9dafb Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Tue, 1 Apr 2025 14:23:07 +0200 Subject: [PATCH 1/4] Update readme --- readme.md | 150 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 110 insertions(+), 40 deletions(-) diff --git a/readme.md b/readme.md index d4e9b87..896d1b2 100644 --- a/readme.md +++ b/readme.md @@ -1,60 +1,130 @@ -# Units in php +# Unit Conversion Library for PHP -## to do: +## Introduction -- [ ] Add more units -- [ ] Add conversion ratios +This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units. -```php -parse('m/s'); // Return class Unit +``` -// Setup basic units -RegistryBuilder::build($registry); +### Regular Conversion + +```php +use Vesper\UnitConversion\Converter; + +$converter = new Converter(); +$cm = $parser->parse('cm'); +$m = $parser->parse('m'); + +$result = $converter->convert($cm, $m, 100); // Converts 100 cm to meters +``` + +### Compound Unit Conversion + +```php +$ms = $parser->parse('m/s'); +$kmh = $parser->parse('km/h'); + +$result = $converter->convert($ms, $kmh, 10); // Converts 10 m/s to km/h +``` + +### Creating and Adding Custom Units + +```php +use Vesper\UnitConversion\RegistryBuilder; +use Vesper\UnitConversion\Dimension; -// Register a si unit including all the si prefixes. -// When specifying symbols, it will also add prefixed symbols. RegistryBuilder::registerSiUnit( $registry, - 'meter^2', // Full name of the unit - symbols: ['m^2'], // Symbols for the unit - ratio: 1 // The relation to the base unit + 'mile', + symbols: ['mi'], + ratio: 1609.34, // 1 mile = 1609.34 meters dimension: Dimension::LENGTH - power: 2 ); +``` -$parser = new Parser($registry); -$converter = new Converter(); +### Using in Laravel -// Simple units -$cm = $parser->parse('cm'); -$m = $parser->parse('m'); +To use this package in Laravel, bind the `Converter` and `Parser` classes in a service provider: -$converter->convert($cm, $m, 100); +```php +use Illuminate\Support\ServiceProvider; +use Vesper\UnitConversion\Converter; +use Vesper\UnitConversion\Parser; +use Vesper\UnitConversion\Registry; -// Compound units -$ms = $parser->parse('m/s'); -$kmh = $parser->parse('km/h'), +class UnitConversionServiceProvider extends ServiceProvider +{ + public function register() + { + $this->app->singleton(Registry::class, function () { + return new Registry(); + }); + + $this->app->singleton(Parser::class, function ($app) { + return new Parser($app->make(Registry::class)); + }); + + $this->app->singleton(Converter::class, function () { + return new Converter(); + }); + } +} +``` + +Then, you can use dependency injection in your controllers or services: + +```php +use Vesper\UnitConversion\Converter; +use Vesper\UnitConversion\Parser; + +class UnitController +{ + public function __construct( + protected Converter $converter, + protected Parser $parser + ) {} + + public function convert() + { + $from = $this->parser->parse('kg'); + $to = $this->parser->parse('g'); + $value = 5; + + return $this->converter->convert($from, $to, $value); // Converts 5 kg to grams + } +} +``` -$converter->convert($ms, $kmh); +## License -// Convert density -$kg = $parser->parse('kg'); -$m3 = $parser->parse('m^3'); -$kgm3 = $parser->parse('kg/m^3'); +This package is open-source and available under the MIT License. See the [LICENSE](./LICENSE) file for more details. -[$amount, $volumeUnit] = $converter->multiply(1, $m3, 19320, $kgm3) -$converter->convert( - $amount, - $volumeUnit, - $parser->parse('cm^3'), -) -``` \ No newline at end of file From 55860ae622929f9158e6f3bc8d9f6035721107c4 Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Wed, 2 Apr 2025 10:53:57 +0200 Subject: [PATCH 2/4] Fix readme --- readme.md | 102 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/readme.md b/readme.md index 896d1b2..bb50e12 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ use Vesper\UnitConversion\Registry; $registry = new Registry(); $parser = new Parser($registry); -$unit = $parser->parse('m/s'); // Return class Unit +$unit = $parser->parse('m/s'); // Returns an instance of Unit ``` ### Regular Conversion @@ -43,7 +43,7 @@ $converter = new Converter(); $cm = $parser->parse('cm'); $m = $parser->parse('m'); -$result = $converter->convert($cm, $m, 100); // Converts 100 cm to meters +$result = $converter->convert($cm, $m, 100); // Converts 100cm to 1m ``` ### Compound Unit Conversion @@ -52,54 +52,95 @@ $result = $converter->convert($cm, $m, 100); // Converts 100 cm to meters $ms = $parser->parse('m/s'); $kmh = $parser->parse('km/h'); -$result = $converter->convert($ms, $kmh, 10); // Converts 10 m/s to km/h +$result = $converter->convert($ms, $kmh, 10); // Converts 10m/s to 36km/h ``` ### Creating and Adding Custom Units +The `RegistryBuilder` class provides a set of predefined units and allows for easy extension. + +#### Using `RegistryBuilder::build()` + +The `build()` method initializes a `Registry` instance with common units like length, mass, time, temperature, force, etc. + ```php +use Vesper\UnitConversion\Registry; use Vesper\UnitConversion\RegistryBuilder; + +$registry = new Registry(); +RegistryBuilder::build($registry); +``` + +This method ensures that a wide range of units are available for use immediately. + +#### Registering a Custom Unit + +The `register()` method is used to add new units to the registry, and `alias()` allows you to specify alternative names. + +```php +use Vesper\UnitConversion\Registry; +use Vesper\UnitConversion\Unit; +use Vesper\UnitConversion\Dimension; +use Vesper\UnitConversion\UnitPart; + +$registry = new Registry(); + +$registry->register( + 'furlong', + new Unit(new UnitPart(1, Dimension::LENGTH, 1) +); +$registry->alias('furlong', ['fur']); +``` + +#### Using `registerSiUnit()` + +The `registerSiUnit()` method allows you to define SI units along with their prefixed versions (e.g., kilogram, milligram, etc.). + +```php +use Vesper\UnitConversion\RegistryBuilder; +use Vesper\UnitConversion\Unit; +use Vesper\UnitConversion\UnitPart; use Vesper\UnitConversion\Dimension; RegistryBuilder::registerSiUnit( $registry, - 'mile', - symbols: ['mi'], - ratio: 1609.34, // 1 mile = 1609.34 meters - dimension: Dimension::LENGTH + 'gram', // Base unit + ['g'], // Aliases + new Unit(new UnitPart(1, Dimension::MASS, 1)) ); ``` +This automatically registers `gram` and generates prefixed versions like `kilogram (kg)`, `milligram (mg)`, etc. + ### Using in Laravel -To use this package in Laravel, bind the `Converter` and `Parser` classes in a service provider: +This package provides a service provider, `ConversionServiceProvider`, which is auto-discovered by Laravel. This means you don’t need to manually register it. ```php -use Illuminate\Support\ServiceProvider; -use Vesper\UnitConversion\Converter; -use Vesper\UnitConversion\Parser; -use Vesper\UnitConversion\Registry; - -class UnitConversionServiceProvider extends ServiceProvider +public function register(): void { - public function register() - { - $this->app->singleton(Registry::class, function () { - return new Registry(); - }); - - $this->app->singleton(Parser::class, function ($app) { - return new Parser($app->make(Registry::class)); - }); - - $this->app->singleton(Converter::class, function () { - return new Converter(); - }); - } + $this->app->singleton(Converter::class, function () { + return new Converter(); + }); + + $this->app->singleton(Parser::class, function (Application $app) { + $registry = $app->make(Registry::class); + + return new Parser($registry); + }); + + $this->app->singleton(Registry::class, function () { + $registry = new Registry(); + + RegistryBuilder::build($registry); + + return $registry; + }); } ``` +Additionally, a facade `Converter` is provided for easy access. -Then, you can use dependency injection in your controllers or services: +#### Example Usage ```php use Vesper\UnitConversion\Converter; @@ -118,7 +159,7 @@ class UnitController $to = $this->parser->parse('g'); $value = 5; - return $this->converter->convert($from, $to, $value); // Converts 5 kg to grams + return $this->converter->convert($from, $to, $value); // Converts 5kg to 5000g } } ``` @@ -127,4 +168,3 @@ class UnitController This package is open-source and available under the MIT License. See the [LICENSE](./LICENSE) file for more details. - From f824824e57c83025dc437140a047df7b6564dc24 Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Thu, 3 Apr 2025 11:34:49 +0200 Subject: [PATCH 3/4] Update readme.md --- readme.md | 44 +++++++++++++++++++---------------------- src/RegistryBuilder.php | 2 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/readme.md b/readme.md index bb50e12..cf9b7a0 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,23 @@ This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units. +## Supported Conversions + +Our library supports a wide range of unit conversions, covering both fundamental and derived physical quantities. + +### πŸ“ **Length (Distance)** +### πŸ“ **Area** +### βš–οΈ **Mass** +### ⏳ **Time** +### 🌑 **Temperature** +### ⚑ **Energy** +### πŸ”₯ **Power** +### πŸ“Š **Pressure** +### πŸ“‘ **Frequency** +### πŸ‹οΈ **Force** +### πŸ”† **Luminous Intensity** +### πŸ§ͺ **Amount of Substance** +### πŸ₯€ **Volume** ## Why Use This Library? - **Simple string-based unit parsing** @@ -87,7 +104,7 @@ $registry = new Registry(); $registry->register( 'furlong', - new Unit(new UnitPart(1, Dimension::LENGTH, 1) + new Unit(new UnitPart(201.168, Dimension::LENGTH, 1) // 1 furlong = 201.168 meter ); $registry->alias('furlong', ['fur']); ``` @@ -106,7 +123,7 @@ RegistryBuilder::registerSiUnit( $registry, 'gram', // Base unit ['g'], // Aliases - new Unit(new UnitPart(1, Dimension::MASS, 1)) + new Unit(new UnitPart(0.001, Dimension::MASS, 1)) // 1 gram = 0.001 kilogram ); ``` @@ -115,29 +132,8 @@ This automatically registers `gram` and generates prefixed versions like `kilogr ### Using in Laravel This package provides a service provider, `ConversionServiceProvider`, which is auto-discovered by Laravel. This means you don’t need to manually register it. +`ConversionServiceProvider` include `Converter`, `Parser` and `Registry` classes. -```php -public function register(): void -{ - $this->app->singleton(Converter::class, function () { - return new Converter(); - }); - - $this->app->singleton(Parser::class, function (Application $app) { - $registry = $app->make(Registry::class); - - return new Parser($registry); - }); - - $this->app->singleton(Registry::class, function () { - $registry = new Registry(); - - RegistryBuilder::build($registry); - - return $registry; - }); -} -``` Additionally, a facade `Converter` is provided for easy access. #### Example Usage diff --git a/src/RegistryBuilder.php b/src/RegistryBuilder.php index a44760b..5bfddd4 100644 --- a/src/RegistryBuilder.php +++ b/src/RegistryBuilder.php @@ -144,7 +144,7 @@ protected static function initLength(Registry $registry): void $registry->alias('inch', ['in']); $registry->register('foot', new Unit(new UnitPart(0.3048, Dimension::LENGTH, 1))); - $registry->alias('foot', ['fe']); + $registry->alias('foot', ['ft']); $registry->register('yard', new Unit(new UnitPart(0.9144, Dimension::LENGTH, 1))); $registry->alias('yard', ['yd']); From d9e5c50bf93794f3dfe8ecad3a01a852a3ae787b Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Thu, 3 Apr 2025 11:45:45 +0200 Subject: [PATCH 4/4] Make list smaller --- readme.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index cf9b7a0..9998cdd 100644 --- a/readme.md +++ b/readme.md @@ -8,19 +8,19 @@ This package provides a powerful and flexible way to handle unit conversion in P Our library supports a wide range of unit conversions, covering both fundamental and derived physical quantities. -### πŸ“ **Length (Distance)** -### πŸ“ **Area** -### βš–οΈ **Mass** -### ⏳ **Time** -### 🌑 **Temperature** -### ⚑ **Energy** -### πŸ”₯ **Power** -### πŸ“Š **Pressure** -### πŸ“‘ **Frequency** -### πŸ‹οΈ **Force** -### πŸ”† **Luminous Intensity** -### πŸ§ͺ **Amount of Substance** -### πŸ₯€ **Volume** +- πŸ“ **Length (Distance)** +- πŸ“ **Area** +- βš–οΈ **Mass** +- ⏳ **Time** +- 🌑 **Temperature** +- ⚑ **Energy** +- πŸ”₯ **Power** +- πŸ“Š **Pressure** +- πŸ“‘ **Frequency** +- πŸ‹οΈ **Force** +- πŸ”† **Luminous Intensity** +- πŸ§ͺ **Amount of Substance** +- πŸ₯€ **Volume** ## Why Use This Library? - **Simple string-based unit parsing**