Giter Club home page Giter Club logo

convertor's Introduction

Convertor

An easy to use PHP unit conversion library.

Full documentation & demos can be found at: http://olifolkerd.github.io/convertor

Convertor

An easy to use PHP unit conversion library.

Converter allows you to convert any unit to any other compatible unit type.

It has no external dependencies, simply include the library in your project and you're away!

Convertor can handle a wide range of unit types including:

  • Length
  • Area
  • Volume
  • Weight
  • Speed
  • Rotation
  • Temperature
  • Pressure
  • Time
  • Energy/Power

If you need aditional unit types, then it is easy to add your own.

Setup

Setting up Convertor could not be simpler by using Composer. Add the following to your composer.json file:

"repositories": [
	...
	{
		"type": "vcs",
		"url": "https://github.com/olifolkerd/convertor"
	}
	...
],
"require": {
	...
	"olifolkerd/convertor": "dev-master",
	...
}

Simple Example

Once you have included the Converter.php library, creating conversions is as simple as creating an instance of the Convertor with the value to convert from, then specifying the new units

$simpleConvertor = new Convertor(10, "m");
$simpleConvertor->to("ft"); //returns converted value

10 Meters = 32.808398950131 Feet

Define your own Units

Convertor now supports using different files that contain the unit conversions by specifying either the path to the file containing the unit array or the filename of the file in src/configdirectly:

//using the default file in `src/Config/Units.php`:
$c=new Convertor(100,"mps");
//using another file somewhere in the project:
$c=new Convertor(100,"mps",'/path/to/my/own/Units.php');
//using the name of the file in conf:
$c=new Convertor(100,"mps",'BaseUnits.php');
//define own units inline
$arr = [
    "m" => array("base" => "m", "conversion" => 1),
    "km" => array("base" => "m", "conversion" => 1000),
];

$c = new Convertor(1, 'm', $arr);

Currently two Unit files are available - one containing the owner's notation and the other one a more formal notation. Differences in notation:

Variant km² kg/m² FileName
owner 'km2' - BaseUnits.php
formal 'km**2' 'kg m**-2' Units.php

Additionally the Units.php file contains area-density definitions.

Resources

  • PHP-Skeleton as a template for the autoloading structure: github

convertor's People

Contributors

barton-webwings avatar curtisdhi avatar frontendcoffee avatar fwidm avatar marcobax avatar olifolkerd avatar scotthavens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

convertor's Issues

Meter cubic conversion error

"m3"=>array("base"=>"l", "conversion"=>1), //meters cubed

A liter is a cubic decimeter, and most certainly not a cubic meter

Add conversion tests

There are already several tests for various kinds of versions.

There are however, a few tests missing:

  • Conversion of area
  • Conversion of volume
  • Conversion of power

This was already mentioned in a todo in the code, I just moved it to here.

Array to string conversion

Hello

this is a simple code trying to convert s to day / hour / min
Ps :I'm using php 7

<?php
include("Convertor.php");

$simpleConvertor = new Convertor(1209867, "s");
var_dump($simpleConvertor->to(array('day', 'hr', 'min')));

this is the result:

array(3) {
  ["day"]=>
  float(14.003090277778)
  ["hr"]=>
  float(336.07416666667)
  ["min"]=>
  float(40328.9)
}

would be good to get the correct convert seconds to day(if possible ) / hours / min

like :

14 days 4 minutes 27 seconds

Thanks

Quality Assurance (and CI)

It's nice that the package already contains a test suite.

It would be nice if it also had:

  • PHP-CS-Fixer
  • PHPStan

And maybe most importantly:

  • CI (for instance: Github Actions)

I would be happy to create a PR for this.

How to display/handle error validation on empty units?

$simpleConvertor->to("ft"); //returns converted value

If the user put wrong unit or leave the unit empty, eg: $simpleConvertor->to("fttttt"); display this "you have entered wrong or empty unit" or redirect to 404 page. How to do this?

Im getting so many error logs:

[05-Aug-2017 23:06:58 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'Unit Not Set' in /public_html/Convertor.php:1020
Stack trace:
#0 /public_html/xxx.php(3): Convertor->to('')
#1 /public_html/xxxx.php(176): include('/xxx...')
#2 {main}
thrown in /public_html/Convertor.php on line 1020

Heavy modifications in Convertor Fork & PR

Hi, recently I've made Pull Request to bring convertor into the composer-compatible form. Turns out i needed to change quite some things to fit it to my application.

Features:

  • Empty constructor is now possible (to call from and to later on)
  • Units are now exchangeable by loading from different files
  • Added more exceptions to differentiate between problems
  • Added another Units file with other notations (km^2 => km2, kg/m^2 => kg m-2)
  • Added PHPUnit tests

So I've thought i should ask before submitting a PR in case it is too bloaty for you. My fork can be found here if you want to take a look first.

Wasn't too sure about PR etiquette on github if you heavily modify another project :D.

Let me know what you think.

Format the files with PSR

To get more compatible with other frameworks, i would suggest, that the file/files are formatted with psr.

Why Post this?

Tell me more about your engineering level, so we can lol.

Support composer

I think, you should provide a more modern way to include this lib.

Time Conversion

Big errors in your time conversion. There are 60 seconds in a minute. Not 30.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.