Skip to content

Commit c9d7eb1

Browse files
committed
Merge branch 'master' of github.com:madebybob/php-number
2 parents 200ad40 + ea8947f commit c9d7eb1

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

.github/workflows/php-cs-fixer.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ jobs:
88

99
steps:
1010
- name: Checkout code
11-
uses: actions/checkout@v2
11+
uses: actions/checkout@v3
1212
with:
1313
ref: ${{ github.head_ref }}
1414

1515
- name: Run composer install
1616
run: composer install -n --prefer-dist
1717

1818
- name: Run PHP CS Fixer
19-
run: composer format
19+
uses: docker://oskarstark/php-cs-fixer-ga
20+
with:
21+
args: --config=.php-cs-fixer.dist.php --allow-risky=yes
2022

2123
- name: Commit changes
2224
uses: stefanzweifel/git-auto-commit-action@v4

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
coverage: none
3333

3434
- name: Install dependencies
35-
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
35+
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
3636

3737
- name: Execute tests
3838
run: vendor/bin/phpunit

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ to the desired needs of your business.
4040
- [Modulus](#modulus)
4141
- [State & Comparison](#state--comparison)
4242
- [Absolute & opposite values](#absolute--opposite-values)
43+
- [Limiting values](#limiting-values)
4344
- [Rounding](#rounding)
4445
- [Immutable & Chaining](#immutable--chaining)
4546
- [Extensibility](#extensibility)
@@ -191,6 +192,28 @@ $absolute = $number->opposite();
191192
$abs = $number->opp();
192193
```
193194

195+
### Limiting values
196+
To make sure the current number is not higher or lower than expected:
197+
198+
```php
199+
$number = new Number('200');
200+
201+
// $result will be 250
202+
$result = $number->min('250');
203+
204+
// $result will be 100
205+
$result = $number->max('100');
206+
```
207+
208+
To use a min and max 'clamp' at the same time:
209+
210+
```php
211+
$number = new Number('200');
212+
213+
// $result will be 150
214+
$result = $number->clamp('100', '150');
215+
```
216+
194217
### Rounding
195218
To round the current number instance, the following methods are available:
196219

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
"format": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes"
4444
},
4545
"config": {
46-
"sort-packages": true
46+
"sort-packages": true,
47+
"allow-plugins": {
48+
"phpro/grumphp": true,
49+
"ocramius/package-versions": true
50+
}
4751
},
4852
"minimum-stability": "dev",
4953
"prefer-stable": true

src/AbstractNumber.php

+48
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,54 @@ public function opp(): self
298298
return $this->opposite();
299299
}
300300

301+
/**
302+
* Prevent the current value to be less than the given value.
303+
*
304+
* @param AbstractNumber|string|float|int $value
305+
*/
306+
public function min($value = null): self
307+
{
308+
$value = $this->getNumberFromInput($value);
309+
310+
if ($this->isLessThan($value)) {
311+
return $this->init((string) $value);
312+
}
313+
314+
return $this;
315+
}
316+
317+
/**
318+
* Prevent the current value to be more than the given value.
319+
*
320+
* @param AbstractNumber|string|float|int $value
321+
*/
322+
public function max($value = null): self
323+
{
324+
$value = $this->getNumberFromInput($value);
325+
326+
if ($this->isGreaterThan($value)) {
327+
return $this->init((string) $value);
328+
}
329+
330+
return $this;
331+
}
332+
333+
/**
334+
* Put a clamp on the current value.
335+
*
336+
* @param AbstractNumber|string|float|int $min
337+
* @param AbstractNumber|string|float|int $max
338+
*/
339+
public function clamp($min, $max): self
340+
{
341+
$result = $this;
342+
343+
$result = $result->min($min);
344+
$result = $result->max($max);
345+
346+
return $this->init((string) $result);
347+
}
348+
301349
/**
302350
* Return boolean if the current value is a positive number.
303351
*/

tests/NumberTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,34 @@ public function testOpposite()
488488
$this->assertEquals('-0.3000', Number::create('0.3000')->opposite()->toString());
489489
}
490490

491+
public function testMin()
492+
{
493+
$number = new Number('200');
494+
495+
$this->assertEquals('200.000', $number->min('200'));
496+
$this->assertEquals('200.000', $number->min('150'));
497+
$this->assertEquals('250.000', $number->min('250'));
498+
}
499+
500+
public function testMax()
501+
{
502+
$number = new Number('200');
503+
504+
$this->assertEquals('200.000', $number->max('200'));
505+
$this->assertEquals('150.000', $number->max('150'));
506+
$this->assertEquals('200.000', $number->max('250'));
507+
}
508+
509+
public function testClamp()
510+
{
511+
$number = new Number('200');
512+
513+
$this->assertEquals('200.000', $number->clamp('100', '200'));
514+
$this->assertEquals('200.000', $number->clamp('100', '300'));
515+
$this->assertEquals('100.000', $number->clamp('50', '100'));
516+
$this->assertEquals('250.000', $number->clamp('250', '300'));
517+
}
518+
491519
public function testIsPositive(): void
492520
{
493521
$this->assertTrue((new Number('200'))->isPositive());

0 commit comments

Comments
 (0)