Skip to content

Commit 02bcdd2

Browse files
committed
bugs & features
added lots of length,area and volume units inversed factor for relative units as thats more logical added tests for (relative) factors on custom units
1 parent 07fcef2 commit 02bcdd2

File tree

139 files changed

+1864
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+1864
-199
lines changed

CONTRIBUTING.md

+8-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
As this package is still very much a work in progress, any help adding more unit's and unit tests is really appreciated!
33

44
A couple of things are important when submitting a Pull Request:
5-
- Use the PSR-2 Coding Standard
5+
- Use PSR-2 Coding Standard
66
- Add tests
77
- Use the method of implementation as described below
88

@@ -25,23 +25,15 @@ imperial / usc systems.
2525
Now create a folder `Unit/XXX/` in which we will place all the unit classes for this unit type, after this proceed with adding a base unit
2626

2727
#### Adding a base unit
28-
We prefer the base unit to be a unit from the metric system. This way we can easily add all SI prefixed units by extending them from the base
29-
unit and only implement a `PhpUnitConversion\Prefix\Metric\<SI_Prefix>` interface. E.g. the implementation of `Mass\MilliGram` is:
30-
```php
31-
class MilliGram extends Gram implements Metric, Milli
32-
{}
33-
```
34-
You should not set a `FACTOR` on the `BASE_UNIT`, otherwise the above implementation will fail because the interface `Prefix\Metric\Milli`
35-
cannot overwrite an already defined class constant.
36-
37-
See `Gruntfile.js` for a quick way to add all prefixed classes to your unit.
28+
We prefer the base unit to be a unit from the SI and/or metric system. If your unit should use the SI prefixes, please use the `Gruntfile.js` for a
29+
quick way to add all prefixed classes to your unit.
3830

3931
#### Add an new unit to a unit type
4032
When adding a unit `YYY` to unit type `XXX` start by creating the file `Unit/XXX/YYY.php`.
4133

4234
If your unit has a linear correspondence to the base unit, just set the class constant `FACTOR` to its correct value.
43-
If your unit has an offset to the base unit, you can add a class constant `ADDITION`.
44-
(Conversion to the BASE_UNIT value is done by first applying the `FACTOR`, then adding the `ADDITION`)
35+
If your unit has an offset to the base unit, you can add a class constant `ADDITION_PRE` or `ADDITION_POST`.
36+
These correspond to the following formula: `BaseUnitValue = (ThisUnitValue + ADDITION_PRE) * FACTOR + ADDITION_POST`
4537

4638
Please also set the class constant `SYMBOL` and `LABEL`. When your unit does not have a `SYMBOL`, omit it or set it to an empty string. `LABEL` is often equal to your lower case classname.
4739

@@ -56,14 +48,14 @@ class AvoirdupoisPound extends Mass
5648
{
5749
use HasFactor;
5850

59-
const FACTOR = 453.59237; // FACTOR is relative to Mass\Gram, 453.6 Gram in a Pound
51+
const FACTOR = 453.59237; // FACTOR is relative to Mass\Gram, 1 Avoirdupois Pound is 453.6 Gram
6052
}
6153

6254
class Ounce extends Pound
6355
{
6456
use HasRelativeFactor;
6557

66-
const FACTOR = 16; // FACTOR is relative to Mass\Pound, 16 Ounce in a Pound
58+
const FACTOR = 1/16; // FACTOR is relative to Mass\Pound, 1 Ounce is 1/16 Pound (or 16 Ounce in a Pound)
6759
}
6860
```
6961

@@ -111,4 +103,4 @@ composer test
111103
or
112104
```
113105
vendor/bin/phpunit
114-
```
106+
```

Gruntfile.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ module.exports = function(grunt) {
3131
fileExt = '.php',
3232
baseUnits = {},
3333
units = [
34-
{ unit: 'Mass', symbol: 'g', label: 'gram', prefix: '' },
35-
{ unit: 'Length', symbol: 'm', label: 'meter', prefix: '' },
36-
{ unit: 'Area', symbol: 'm', label: 'meter', prefix: 'Square' },
37-
{ unit: 'Volume', symbol: 'm', label: 'meter', prefix: 'Cubic' },
34+
{ unit: 'Mass', base: 1, relative: 0, symbol: 'g', label: 'gram', prefix: '' },
35+
{ unit: 'Length', base: 1, relative: 0, symbol: 'm', label: 'meter', prefix: '' },
36+
{ unit: 'Area', base: 1, relative: 0, symbol: 'm', label: 'meter', prefix: 'Square' },
37+
{ unit: 'Volume', base: 1, relative: 0, symbol: 'm', label: 'meter', prefix: 'Cubic' },
38+
{ unit: 'Volume', base: 1, relative: 1, symbol: 'l', label: 'liter', prefix: '' },
3839
];
3940
phpTemplate = `<?php
4041
namespace PhpUnitConversion\\Unit\\<%= unit %>;
4142
4243
use PhpUnitConversion\\System\\Metric;
43-
44+
<%= extraIncludes %>
4445
class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPrefix %><%= baseUnit %> implements Metric
4546
{
46-
const FACTOR = <%= factor %>;
47+
<%= extraUses %> const FACTOR = <%= factor %>;
4748
4849
const SYMBOL = '<%= symbol %>';
4950
const LABEL = '<%= label %>';
@@ -52,20 +53,26 @@ class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPr
5253
var baseRegex = /BASE_UNIT\s*=\s*([^:]+)::/,
5354
replaceExt = new RegExp(fileExt);
5455

55-
//grunt.file.recurse(siPrefixes, function(abspath, rootdir, subdir, filename) {
5656
for (var u = 0; u < units.length; u++) {
5757
var baseUnitPrefix = units[u].prefix,
5858
baseUnit = units[u].label.replace(/\b./g, function(m){ return m.toUpperCase(); })
5959

6060
if(baseUnit) {
6161
for (var i = 0; i < siPrefixes.length; i++) {
6262
siPrefix = siPrefixes[i]
63-
var prefixSI = siPrefix.label.replace(/\b./g, function(m){ return m.toUpperCase(); }) /*filename.replace(replaceExt, '')*/,
63+
var prefixSI = siPrefix.label.replace(/\b./g, function(m){ return m.toUpperCase(); }),
6464
phpFileName = unitFolder + units[u].unit + '/' + baseUnitPrefix + prefixSI + baseUnit + fileExt,
65-
factor = '1E' + siPrefix.factor * (baseUnitPrefix === 'Cubic' ? 3 : (baseUnitPrefix === 'Square' ? 2 : 1)),
65+
factor = units[u].base + 'E' + siPrefix.factor * (baseUnitPrefix === 'Cubic' ? 3 : (baseUnitPrefix === 'Square' ? 2 : 1)),
6666
symbol = siPrefix.symbol + units[u].symbol + (baseUnitPrefix === 'Cubic' ? '3' : (baseUnitPrefix === 'Square' ? '2' : '')),
6767
label = (baseUnitPrefix ? baseUnitPrefix.toLowerCase() + ' ' : '') + siPrefix.label + units[u].label;
6868

69+
extraIncludes = ''
70+
extraUses = ''
71+
if (units[u].relative) {
72+
extraIncludes = 'use PhpUnitConversion\\Traits\\HasRelativeFactor;\n';
73+
extraUses = ' use HasRelativeFactor;\n\n';
74+
}
75+
6976
if(true||!grunt.file.exists(phpFileName)) {
7077
console.log(phpFileName);
7178
grunt.file.write(phpFileName,
@@ -74,6 +81,8 @@ class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPr
7481
factor: factor,
7582
symbol: symbol,
7683
label: label,
84+
extraIncludes: extraIncludes,
85+
extraUses: extraUses,
7786
prefixSI: prefixSI,
7887
baseUnit: baseUnit,
7988
baseUnitPrefix: baseUnitPrefix
@@ -83,6 +92,5 @@ class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPr
8392
}
8493
}
8594
}
86-
//});
8795
});
8896
};

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"myclabs/php-enum": "^1.5"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^6.0.7"
28+
"phpunit/phpunit": "^6.0.7",
29+
"squizlabs/php_codesniffer": "^3.1"
2930
},
3031
"autoload": {
3132
"psr-4": {
@@ -38,6 +39,7 @@
3839
}
3940
},
4041
"scripts": {
41-
"test": "vendor/bin/phpunit"
42+
"test": "vendor/bin/phpunit",
43+
"cs": "vendor/bin/phpcs src/*"
4244
}
4345
}

phpunit.xml.dist

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</whitelist>
2323
</filter>
2424

25-
<logging>
25+
<!-- logging>
2626
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
27-
</logging>
27+
</logging -->
2828
</phpunit>

src/System.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
interface System
55
{
66

7-
}
7+
}

src/System/Imperial.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
interface Imperial extends System
77
{
88

9-
}
9+
}

src/System/Metric.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
interface Metric extends System
77
{
88

9-
}
9+
}

src/System/USC.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
interface USC extends System
77
{
88

9-
}
9+
}

src/Traits/BaseUnit.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ protected function toBaseUnit()
1010
}
1111
return parent::toBaseUnit();
1212
}
13-
}
13+
}

src/Traits/HasFactor.php

+42-12
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,68 @@ public function getFactor()
88
return self::FACTOR;
99
}
1010

11-
public function getAddition()
11+
public function getAdditionPre()
1212
{
13-
if (defined('static::ADDITION')) {
14-
return self::ADDITION;
13+
if (defined('self::ADDITION_PRE')) {
14+
return self::ADDITION_PRE;
1515
}
1616

1717
return false;
1818
}
1919

20-
protected function fromBaseValue($value)
20+
public function getAdditionPost()
2121
{
22-
$value/= self::getFactor();
23-
24-
$addition = self::getAddition();
22+
if (defined('self::ADDITION_POST')) {
23+
return self::ADDITION_POST;
24+
}
25+
26+
return false;
27+
}
28+
29+
protected function fromBaseValue($baseValue)
30+
{
31+
$value = $baseValue;
32+
33+
$addition = self::getAdditionPost();
34+
if ($addition !== false) {
35+
$value-= $addition;
36+
}
37+
38+
$factor = self::getFactor();
39+
if ($factor !== false) {
40+
$value/= $factor;
41+
}
42+
43+
$addition = self::getAdditionPre();
2544
if ($addition !== false) {
2645
$value-= $addition;
2746
}
2847

2948
return $value;
3049
}
50+
3151

3252
protected function toBaseValue($value = null)
3353
{
34-
if($value === null) $value = $this->value;
54+
if ($value === null) {
55+
$value = $this->value;
56+
}
57+
58+
$addition = self::getAdditionPre();
59+
if ($addition !== false) {
60+
$value+= $addition;
61+
}
62+
63+
$factor = self::getFactor();
64+
if ($factor !== false) {
65+
$value*= $factor;
66+
}
3567

36-
$addition = self::getAddition();
68+
$addition = self::getAdditionPost();
3769
if ($addition !== false) {
3870
$value+= $addition;
3971
}
4072

41-
$value*= self::getFactor();
42-
4373
return $value;
4474
}
45-
}
75+
}

src/Traits/HasRelativeFactor.php

+48-18
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,69 @@ public function getFactor()
88
return self::FACTOR;
99
}
1010

11-
public function getAddition()
11+
public function getAdditionPre()
1212
{
13-
if (defined('static::ADDITION')) {
14-
return self::ADDITION;
13+
if (defined('self::ADDITION_PRE')) {
14+
return self::ADDITION_PRE;
15+
}
16+
17+
return false;
18+
}
19+
20+
public function getAdditionPost()
21+
{
22+
if (defined('self::ADDITION_POST')) {
23+
return self::ADDITION_POST;
1524
}
1625

1726
return false;
1827
}
1928

2029
protected function fromBaseValue($baseValue)
2130
{
22-
$value = parent::fromBaseValue($baseValue);
23-
$value*= self::getFactor();
24-
25-
$addition = self::getAddition();
31+
$value = $baseValue;
32+
33+
$addition = self::getAdditionPost();
2634
if ($addition !== false) {
2735
$value-= $addition;
2836
}
29-
30-
return $value;
37+
38+
$factor = self::getFactor();
39+
if ($factor !== false) {
40+
$value/= $factor;
41+
}
42+
43+
$addition = self::getAdditionPre();
44+
if ($addition !== false) {
45+
$value-= $addition;
46+
}
47+
48+
return parent::fromBaseValue($value);
3149
}
32-
50+
3351
protected function toBaseValue($value = null)
3452
{
35-
if($value === null) $value = $this->value;
36-
37-
$addition = self::getAddition();
53+
if ($value === null) {
54+
$value = $this->value;
55+
}
56+
57+
$value = parent::toBaseValue($value);
58+
59+
$addition = self::getAdditionPre();
60+
if ($addition !== false) {
61+
$value+= $addition;
62+
}
63+
64+
$factor = self::getFactor();
65+
if ($factor !== false) {
66+
$value*= $factor;
67+
}
68+
69+
$addition = self::getAdditionPost();
3870
if ($addition !== false) {
3971
$value+= $addition;
4072
}
41-
42-
$baseValue = $value / self::getFactor();
43-
44-
return parent::toBaseValue($baseValue);
73+
74+
return $value;
4575
}
46-
}
76+
}

0 commit comments

Comments
 (0)