Git Product home page Git Product logo

units-of-measurement's Introduction

Units of Measurement

This project collects some units of measurement and encapsulates symbols and names in simple constant classes. Some behavior was added to handle and interact with it in a more comfortable way.

In this version, the following units were implemented:

  • Time (s, m, h, ...)
  • Information (B, KB, MB, ...)

Installation

Use following command to install this library:

composer require pleets/units-of-measurement

Usage

Displaying Units and Symbols

You can get the name of a unit of measurement as follows

use Pleets\Units\Symbols\TimeSymbol;
use Pleets\Units\Units\TimeUnit;
use Pleets\Units\Symbols\InformationSymbol;
use Pleets\Units\Units\InformationUnit;

// second
$symbol = TimeUnit::fromSymbol(TimeSymbol::SECOND);

// kilobyte
$symbol = InformationUnit::fromSymbol(InformationSymbol::KILOBYTE);

As the same way, you can get the symbol of a unit measurement as follows

use Pleets\Units\Symbols\TimeSymbol;
use Pleets\Units\Units\TimeUnit;
use Pleets\Units\Symbols\InformationSymbol;
use Pleets\Units\Units\InformationUnit;

// s
$symbol = TimeSymbol::fromUnit(TimeUnit::SECOND);

// KB
$symbol = InformationSymbol::fromUnit(InformationUnit::KILOBYTE);

Grouping Units and Symbols

You can group as units as you need in the following way.

use Pleets\Units\Units\TimeUnit;
use Pleets\Units\Units\Sets\TimeUnitSet;

$timeSet = new TimeUnitSet();
$timeSet->addUnit(TimeUnit::MINUTE);
$timeSet->addUnit(TimeUnit::HOUR);

// ['minute', 'hour']
$units = $timeSet->toArray();

As the same way, you can group as symbols as you need in the following way.

use Pleets\Units\Symbols\TimeSymbol;
use Pleets\Units\Symbols\Sets\TimeSymbolSet;

$timeSet = new TimeSymbolSet();
$timeSet->addSymbol(TimeSymbol::MINUTE);
$timeSet->addSymbol(TimeSymbol::HOUR);

// ['min', 'h']
$symbols = $timeSet->toArray();

Conversions

You can convert between units using the main class of each unit.

use Pleets\Units\Time;

$time = Time::fromUnit('minute', 5);

// 300s
$time->toMinutes()->toString();

// 300 seconds
$time->toString(false);

units-of-measurement's People

Contributors

darioriverat avatar

Watchers

 avatar  avatar

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.