Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List all suported units #98

Closed
proualexandre opened this issue Jun 2, 2018 · 10 comments
Closed

List all suported units #98

proualexandre opened this issue Jun 2, 2018 · 10 comments

Comments

@proualexandre
Copy link
Contributor

proualexandre commented Jun 2, 2018

Does a method exist to list all the units currently supported by the librarie ? If not, i think implement it will be a good improuvment. I know the listMesurements method of the UnitRegistry class but it seems to list only the kind of unit, like length or speed, but not the unit itself.

@proualexandre
Copy link
Contributor Author

I haven't seen the listUnits while submiting my previous post. However, we need to manually register all the unit, is there a way to instiante the registry with all the supported units preregistred ?

@proualexandre
Copy link
Contributor Author

I have writed a class to solved my issue. Maybe it will be a good point of adding it in the library.

`class ConversionService {
/** @var UnitRegistry */
private $fullRegistry;

public function __construct()
{
    $this->fullRegistry = new UnitRegistry($this->getAllUnitsArray());
}

private function getAllUnitsArray() {
return [
# Area
new Acre(),
new Hectare(),
new SquareCentimetre(),
new SquareFoot(),
new SquareKilometre(),
new SquareMetre(),
new SquareMile(),
new SquareMillimetre(),

        # Energy
        new Calorie(),
        new FootPound(),
        new Joule(),
        new Kilojoule(),
        new KilowattHour(),
        new Megaelectronvolt(),
        new Megajoule(),
        new MegawattHour(),
        new NewtonMetre(),
        new WattHour(),

        # Length
        new AstronomicalUnit(),
        new Centimetre(),
        new Decimetre(),
        new Foot(),
        new Hand(),
        new Inch(),
        new Kilometre(),
        new Lightyear(),
        new Metre(),
        new Micrometre(),
        new Mile(),
        new Millimetre(),
        new Nanometre(),
        new Parsec(),
        new Picometre(),
        new Yard(),

        # Mass
        new Gram(),
        new Kilogram(),
        new LongTon(),
        new Milligram(),
        new Newton(),
        new Ounce(),
        new Pound(),
        new ShortTon(),
        new Stone(),
        new Tonne(),

        # Plane Angle
        new Degree(),
        new Radian(),

        # Pressure
        new Atmosphere(),
        new Bar(),
        new Kilopascal(),
        new Megapascal(),
        new Millibar(),
        new Pascal(),
        new PoundForcePerSquareInch(),
        new Torr(),

        # Speed
        new KilometrePerHour(),
        new MetrePerSecond(),
        new MilePerHour(),

        # Temperature
        new Celsius(),
        new Fahrenheit(),
        new Kelvin(),

        # Time
        new Day(),
        new Hour(),
        new Microsecond(),
        new Millisecond(),
        new Minute(),
        new Month(),
        new Nanosecond(),
        new Second(),
        new Week(),
        new Year(),

        # Volume
        new CubicMetre(),
        new Gallon(),
        new Litre(),
        new Millilitre(),
        new Pint()
    ];
}

public function listAllUnits ($measurement = null) {
    return $this->fullRegistry->listUnits($measurement);
}

}`

@jordanbrauer
Copy link
Owner

jordanbrauer commented Jun 6, 2018

@proualexandre Would love to have this feature implemented! Perhaps not as a new class, though.

I haven't seen the listUnits while submiting my previous post. However, we need to manually register all the unit, is there a way to instiante the registry with all the supported units preregistred ?

Not yet, there isn't. This can also be added as a feature. 👍

@jordanbrauer
Copy link
Owner

jordanbrauer commented Jun 7, 2018

@proualexandre After some more thinking about your suggestions, I think that a really simple implementiion of the builder pattern would be the best solution. Since you've already got a start on your concept, would you like me to leave this up to you for a pull request?

I know that you originally closed the issue, but I will give it the 4 days I took to reply, for you to reply, before carrying on. 🙂


Here is some scratchpad pseudo-y code as an example of how it might/could look, to help explain what I mean above.

# A new public static method could be added to the UnitConverter class. It will
# return a new instance of a ConverterBuilder (this class would have to be
# implemented)
$builder = UnitConverter::createBuilder();

$converter = $builder
    # Calculator Production Step Methods
    ->addSimpleCalculator() # constructs with a simple calculator
    ->addBinaryCalculator() # constructs with a binary calculator

    # Unit Registry Production Step Methods
    ->addDefaultRegistry() # constructs with the entire set of built-in units
    ->addRegistryWith([]) # constructs with a set of supplied units

    # Final call to the build method that will return the actual UnitConverter
    # instance
    ->build();

@proualexandre
Copy link
Contributor Author

I'm really happy of your interrest about my suggestion. In my own implementation, I have writed a specific class because it contains also the specific logic of my application but , I am agree with you, for an official integration in the library a specific class is not required.

@jordanbrauer
Copy link
Owner

@proualexandre Just so I can label this issue properly – are you going to work on a branch & submit a PR, or is this a feature request?

@proualexandre
Copy link
Contributor Author

Yes, I will submit a pull request.

@proualexandre
Copy link
Contributor Author

@jordanbrauer The implementation of the ConverterBuilder and of the fully loaded registry is ready on my local but I have somme rights issue to push on the branch fully-loaded-converter. How can I proceed to submit a pull request ?

@jordanbrauer
Copy link
Owner

@proualexandre Ah! You will have to fork the project and create your own branch, there. Here is link to the steps to create a PR to a project on GitHub

@proualexandre
Copy link
Contributor Author

I submit the pull request right now. I will be happy if you let me know your though about my implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants