Unitz is a way to address easy conversions among various measurable units. A utility that helps convert a unit to all types and agnostic to what type of unit it was originally set as.
composer require brewerwall/unitz
// Create a new Gravity Object
$gravity = new Gravity(plato: 12);
// Gets the Plato of our Gravity
$plato = $gravity->getPlato();
// Gets the Specific Gravity of our Gravity
$specificGravity = $gravity->getSpecificGravity();
// Gets the Brix of our Gravity
$brix = $gravity->getBrix();
// Gets our Preferred Unit of measure based on our preferences
$plato = $gravity->getValue();
You can inject the UnitzService class into your application. Setting the user's preferences as an argument in the
constructor
will allow you to use the getValue()
method to get the user's preferred unit of measure.
// Instantiate a new UnitzService in a Service Provider Pattern
$unitService = new UnitzService(preferences: ['Temperature' => 'Celsius']);
// Dependency injection of UnitzService within the application
$temperature = $unitService->makeTemperature(fahrenheit: 72);
// Output of getValue() based on the user's preferences
$temperature->getValue(); // 22.222222222222
// Output of getValue() based on the user's preferences with rounding
$temperature->getValue(1); // 22.2
When setting a user's preference in the UnitService, you no longer need to specify what type of unit the user is
inputting by using the userValue
argument in the constructor of the Unit. If the user's value needs to change,
the setValue()
method will also accomplish the same idea.
// Create a new Gravity Object
$gravity = new Gravity(userValue: 12, preferences: ['Gravity' => 'Brix']);
// Gets the Brix of our Gravity
$brix = $gravity->getBrix(); // 12
// Gets our Preferred Unit of measure based on our preferences
$plato = $gravity->getValue(); // 12
// Instantiate a new UnitzService in a Service Provider Pattern
$unitService = new UnitzService(preferences: ['Temperature' => 'Fahrenheit']);
// Dependency injection of UnitzService within the application and a user submitted form value
$temperature = $unitService->makeTemperature(userValue: 72);
// Output of getValue() based on the user's preferences
$temperature->getValue(); // 72
// Output of getFahrenheit() will return the same as getValue() since it's the user's preference
$temperature->getFahrenheit(); // 72
// Updating the user's temperature value will have the same effect.
$temperature->setValue(76);
// Values update as needed
$temperature->getValue(); // 76
$temperature->getFahrenheit(); // 76
Unit | Types |
---|---|
Gravity | Plato, SpecificGravity, Brix |
Pressure | Psi, Bar |
Temperature | Celsius, Fahrenheit |
Volume | Ounce, Gallon, Barrel, Milliliter, Liter, Hectoliter |
Weight | Ounce, Pound, Gram, Kilogram |
Color | Srm, Ebc, Lovibond |
Time | Millisecond, Second, Minute, Hour, Day, Week, Month, Year |
Distillate | Proof, Alcohol Percent |
Length | Inch, Foot, Yard, Mile, Millimeter, Centimeter, Meter, Kilometer |
By default, all units have a getValue()
method that returns the users preference of unit type. There is a default
preference set, but can be overridden when instantiating a new unit.
[
'Gravity' => 'Plato',
'Temperature' => 'Fahrenheit',
'Volume' => 'Gallon',
'Pressure' => 'Psi',
'Weight' => 'Pound',
'Color' => 'Srm',
'Time' => 'Minute',
'Distillate' => 'Proof',
'Length' => 'Foot',
];
// Create a new Weight Object
$weight = new Weight(kilogram: 7.5, preferences: ['Weight' => 'Kilogram']);
// Returns Kilogram since that is the overridden preference
$kilogram = $weight->getValue();
In each type's get method, there is the option to pass in a precision of rounding. This also includes the getValue()
method that all units share.
$weight = new Weight(kilogram: 7.5629145);
$kilogram = $weight->getKilogram(3); // $kilogram = 7.563
A way of representing a rate of change between two units. When accessing values of rates, all will follow the naming
pattern of $rate->get{Unit}Per{Unit}
for the respective units. Example using Flow: $flow->getGallonPerHour()
This class represent the amount of Volume flowed over a period of Time.
$flow = new Flow(new Volume(gallon: 5), new Time(hour: 1));
$flow->getGallonPerHour(); // 5
$flow->getGallonPerMinute(); // 0.083333333333333
This class represent the amount of Volume boiled over a period of Time.
$boil = new Boil(new Volume(gallon: 5), new Time(hour: 1));
$boil->getGallonPerHour(); // 5
$boil->getGallonPerMinute(); // 0.083333333333333
This class represent the amount of Length traveled over a period of Time.
$speed = new Speed(new Length(foot: 5), new Time(minute: 1));
$speed->getFeetPerMinute(); // 5
$boil->getFeetPerHour(); // 300
A library of calculations that can be used with various Unitz classes.
This class will calculate Area related calculations.
This method will calculate the area of a rectangle based on the length and width.
Area::rectangle(Length $length, Length $width): Length
Length $length
- Length of the rectangleLength $width
- Width of the rectangle
Length
- Area of the rectangle
This method will calculate the area of a square based on the length of one side.
Area::square(Length $side): Length
Length $side
- Length of one side of the square
Length
- Area of the square
This method will calculate the area of a circle based on the radius.
Area::circle(Length $radius): Length
Length $radius
- Radius of the circle
Length
- Area of the circle
This method will calculate the area of an ellipse based on the major and minor axis.
Area::ellipse(Length $majorAxisRadius, Length $minorAxisRadius): Length
Length $majorAxisRadius
- Major axis radius of the ellipseLength $minorAxisRadius
- Minor axis radius of the ellipse
Length
- Area of the ellipse
This method will calculate the area of a triangle based on the base and height.
Area::triangle(Length $base, Length $height): Length
Length $base
- Base of the triangleLength $height
- Height of the triangle
Length
- Area of the triangle
This method will calculate the area of an equilateral triangle based on the length of one side.
Area::equilateralTriangle(Length $side): Length
Length $side
- Length of one side of the equilateral triangle
Length
- Area of the equilateral triangle
This method will calculate the area of a trapezoid based on the length of the two bases and the height.
Area::trapezoid(Length $base1, Length $base2, Length $height): Length
Length $base1
- Length of the first base of the trapezoidLength $base2
- Length of the second base of the trapezoidLength $height
- Height of the trapezoid
Length
- Area of the trapezoid
This method will calculate the area of a regular pentagon based on the length of one side.
Area::regularPentagon(Length $side): Length
Length $side
- Length of one side of the regular pentagon
Length
- Area of the regular pentagon
This method will calculate the area of a regular hexagon based on the length of one side.
Area::regularHexagon(Length $side): Length
Length $side
- Length of one side of the regular hexagon
Length
- Area of the regular hexagon
This method will calculate the area of a regular heptagon based on the length of one side.
Area::regularHeptagon(Length $side): Length
Length $side
- Length of one side of the regular heptagon
Length
- Area of the regular heptagon
This method will calculate the area of a regular octagon based on the length of one side.
Area::regularOctagon(Length $side): Length
Length $side
- Length of one side of the regular octagon
Length
- Area of the regular octagon
This method will calculate the area of a regular nonagon based on the length of one side.
Area::regularNonagon(Length $side): Length
Length $side
- Length of one side of the regular nonagon
Length
- Area of the regular nonagon
This method will calculate the area of a regular decagon based on the length of one side.
Area::regularDecagon(Length $side): Length
Length $side
- Length of one side of the regular decagon
Length
- Area of the regular decagon
This class will calculate Beer related calculations.
Alcohol By Volume (ABV) is the percent of alcohol content in the beer based on the original gravity, final gravity and formula version. Source of equation is at Brewer's Friend.
Beer::alcoholByVolume(Gravity $originalGravity, Gravity $finalGravity, string $formulaVersion = Beer::ABV_ALTERNATE_FORMULA): float
Gravity $originalGravity
- Original Gravity of the beerGravity $finalGravity
- Final Gravity of the beerstring $formulaVersion
- Formula ABV calculation:Beer::ABV_STANDARD_FORMULA
orBeer::ABV_ALTERNATE_FORMULA
float
- Alcohol By Volume (ABV) Value
Alcohol By Weight (ABW) is weighing the amount of alcohol in a fixed volume of liquid and comparing it to the weight of pure water based on the original gravity and final gravity.
Beer::alcoholByWeight(Gravity $originalGravity, Gravity $finalGravity): float
Gravity $originalGravity
- Original Gravity of the beerGravity $finalGravity
- Final Gravity of the beer
float
- Alcohol By Weight (ABW) Value
Standard Reference Method (Srm) is the method for color assessment of wort or beer as published in the recommended methods of the American Society of Brewing Chemists
Beer::standardReferenceMethod(Weight $weight, Color $color, Volume $volume): Color
Weight $weight
- Weight of the grainColor $color
- Color of the grainVolume $volume
- Volume of the water
Unitz/Color
- Color (Color) Value
Malt Color Unit (MCU) is an equation that helps determine what color a beer would be.
Beer::maltColorUnit(Weight $weight, Color $color, Volume $volume): float
Weight $weight
- Weight of the grainColor $color
- Color of the grainVolume $volume
- Volume of the water
float
- Malt Color Unit (MCU) Value
International Bitterness Units (IBU) is the bitterness of the beer based on the alpha acid of the hops, weight of the hops, time in the boil, gravity of the wort, and volume of the wort.
Based off Palmer's Calculation
Beer::internationalBitternessUnits(float $alphaAcid, Weight $weight, Time $time, Gravity $gravity, Volume $volume)
float $alphaAcid
- Alpha Acid of the hopsWeight $weight
- Weight of the hopsTime $time
- Time in the boilGravity $gravity
- Gravity of the wortVolume $volume
- Volume of the wort
float
- International Bitterness Units (IBU) Value
Alpha Acid Units (AAU) is the potential bitterness of the hops based on the alpha acid and weight.
Beer::alphaAcidUnit(float $alphaAcid, Weight $weight): float
float $alphaAcid
- Alpha Acid of the hopsWeight $weight
- Weight of the hops
float
- Alpha Acid Units (AAU) Value
This is a hop utilization factor based on the Tinseth formula derived by Glenn Tinseth.
Beer::hopUtilization(Time $time, Gravity $gravity)
Time $time
- Time in the boilGravity $gravity
- Gravity of the wort
float
- Hop Utilization Value
Determines the number of calories in a finished beer based on the original gravity, final gravity and the volume of the beer consumed.
Beer::calories(Gravity $originalGravity, Gravity $finalGravity, Volume $volume)
Gravity $originalGravity
- Original Gravity of the beerGravity $finalGravity
- Final Gravity of the beerVolume $volume
- Volume of the beer consumed
float
- Calories
Real Extract (RE) is a precise calculation concerning the gravity of beer. Source of equation is Craft Beer & Brewing
Beer::realExtract(Gravity $originalGravity, Gravity $finalGravity)
Gravity $originalGravity
- Original Gravity of the beerGravity $finalGravity
- Final Gravity of the beer
float
- Real Extract
Apparent Degree of Fermentation (ADF) is a measure of the amount of sugar that has been converted to alcohol and carbon dioxide by yeast during fermentation
Beer::apparentDegreeOfFermentation(Gravity $originalGravity, Gravity $finalGravity)
Gravity $originalGravity
- Original Gravity of the beerGravity $finalGravity
- Final Gravity of the beer
float
- Apparent Degree of Fermentation
Gravity Correction based on Temperature of Sample and Hydrometer Calibration. Source Brewers Friend
Beer::gravityCorrection(Gravity $gravity, Temperature $temperature, Temperature $calibrationTemperature)
Gravity $gravity
- Gravity of the SampleTemperature $temperature
- Temperature of the sampleTemperature $calibrationTemperature
- Temperature hydrometer is calibrated to
Gravity
- Corrected Gravity of Sample
This class will calculate Spirit related calculations.
Dilute Down To Desired Proof is a calculation to determine how much water to add to a spirit to get to a desired proof.
Spirit::diluteDownToDesiredProof(Proof $currentProof, Proof $desiredProof, Volume $currentVolume): Volume
Proof $currentProof
- Current Proof of the spiritProof $desiredProof
- Desired Proof of the spiritVolume $currentVolume
- Current Volume of the spirit
Volume
- Volume of water to add to the spirit
Distilled Alcohol Volume is a calculation to determine the volume of alcohol distilled depending on the wash abv and still efficiency.
Spirit::distilledAlcoholVolume(Volume $volume, Distillate $wash, float $stillEfficiencyPercent): Volume
Volume $volume
- Volume of the washDistillate $wash
- Distillate of the washfloat $stillEfficiencyPercent
- Still efficiency percentage
Volume
- Volume of the distilled alcohol
Distilled Remaining Water Volume is a calculation to determine the volume of water remaining after distilling a spirit.
Spirit::distilledRemainingWaterVolume(Volume $volume, Distillate $wash, float $stillEfficiencyPercent): Volume
Volume $volume
- Volume of the washDistillate $wash
- Distillate of the washfloat $stillEfficiencyPercent
- Still efficiency percentage
Volume
- Volume of the remaining water
This class will calculate Water related calculations.
Parts Per Million (PPM) is a calculation to determine the amount of a substance in a solution.
Water::partsPerMillion(Weight $substance, Volume $volume): float
Weight $substance
- Weight of the substanceVolume $volume
- Volume of the water
float
- Parts Per Million (PPM) Value
Boil Off Volume determines the Volume boiled off based on Boil Rate and Time.
Water::boilOffVolume(Boil $boilRate, Time $time): Volume
Boil $boilRate
- Boil Rate of your systemTime $time
- Time of the boil
Volume
- Volume that has been boiled off
Post Boil Volume determines the Volume solution remaining after a Pre Boil Volume, Boil Rate and Time are given.
Water::boilOffVolume(Volume postBoilVolume, Boil $boilRate, Time $time): Volume
Volume $preBoilVolume
- Volume of solution before it's boiledBoil $boilRate
- Boil Rate of your systemTime $time
- Time of the boil
Volume
- Volume that remains from the boil