File tree 4 files changed +31
-9
lines changed
4 files changed +31
-9
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,12 @@ $size->add('2G')
60
60
echo $size->asAuto(); // '10.00 GB'
61
61
```
62
62
63
+ Negative values are supported. In the case below, 1.2 megabytes are subtracted:
64
+
65
+ ``` php
66
+ $size->add('-1.2mb');
67
+ ```
68
+
63
69
You may also use ` add() ` and ` subtract() ` with an array of values:
64
70
65
71
``` php
Original file line number Diff line number Diff line change @@ -107,20 +107,14 @@ private function addSize($size)
107
107
}
108
108
109
109
/**
110
- * Subtract from this filesize, stopping at 0 bytes .
110
+ * Subtract from this filesize.
111
111
*
112
112
* @param string|int $size Such as '100 MB'
113
113
* @return self
114
114
*/
115
115
private function subtractSize ($ size )
116
116
{
117
- $ bytesToSubtract = $ this ->sizeToBytes ($ size );
118
-
119
- if ($ bytesToSubtract <= $ this ->bytes ) {
120
- $ this ->bytes -= $ bytesToSubtract ;
121
- } else {
122
- $ this ->bytes = 0 ;
123
- }
117
+ $ this ->bytes -= $ this ->sizeToBytes ($ size );
124
118
125
119
return $ this ;
126
120
}
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ class SizeStringParser
18
18
* - 123 MB
19
19
* - 1 gigabytes
20
20
*/
21
- const SIZE_STRING_PATTERN = '/^([0-9\.]+)\s*?([A-Za-z]+)?$/ ' ;
21
+ const SIZE_STRING_PATTERN = '/^([0-9\.- ]+)\s*?([A-Za-z]+)?$/ ' ;
22
22
23
23
/**
24
24
* Parse a size string into its parts (value, unit).
Original file line number Diff line number Diff line change @@ -41,6 +41,17 @@ public function testAdd()
41
41
$ this ->assertSame ($ size ->as ('B ' ), 129128448 );
42
42
}
43
43
44
+ /**
45
+ * Test #add with a negative value.
46
+ */
47
+ public function testAddNegative ()
48
+ {
49
+ $ size = new FileSize ('10MB ' );
50
+ $ size ->add ('-20MB ' );
51
+
52
+ $ this ->assertSame ($ size ->as ('MB ' ), -10.0 );
53
+ }
54
+
44
55
/**
45
56
* Test #subtract.
46
57
*/
@@ -52,6 +63,17 @@ public function testSubtract()
52
63
$ this ->assertSame ($ size ->as ('B ' ), 128821248 );
53
64
}
54
65
66
+ /**
67
+ * Test #subtract with a negative value.
68
+ */
69
+ public function testSubtractNegative ()
70
+ {
71
+ $ size = new FileSize ('10MB ' );
72
+ $ size ->subtract ('-20MB ' );
73
+
74
+ $ this ->assertSame ($ size ->as ('MB ' ), 30.0 );
75
+ }
76
+
55
77
/**
56
78
* Test adding an array of items.
57
79
*/
You can’t perform that action at this time.
0 commit comments