ソースを参照

Add progress helper calculate() method.

mscherer 6 年 前
コミット
b3c4429e36

+ 10 - 1
docs/Helper/Progress.md

@@ -44,10 +44,19 @@ This can be used if you want to customize the usage.
 
 ### calculatePercentage()
 
+This method is responsible for the main percentage calculation.
+It can be also used standalone.
+```php 
+$percentage = $this->Progress->calculatePercentage($total, $is);
+echo $this->Number->toPercentage($percentage, 0, ['multiply' => true]);
+```
+
+### roundPercentage()
+
 This method is responsible for the above min/max handling.
 It can be also used standalone.
 ```php 
-$percentage = $this->Progress->calculatePercentage($value);
+$percentage = $this->Progress->roundPercentage($value);
 echo $this->Number->toPercentage($percentage, 0, ['multiply' => true]);
 ```
 For value `0.49` it outputs: `49%`, for value `0.0001` it outputs `1%`.

+ 14 - 3
src/View/Helper/ProgressHelper.php

@@ -51,7 +51,7 @@ class ProgressHelper extends Helper {
 		$bar = $this->draw($value, $length);
 
 		$attributes += [
-			'title' => Number::toPercentage($this->calculatePercentage($value), 0, ['multiply' => true]),
+			'title' => Number::toPercentage($this->roundPercentage($value), 0, ['multiply' => true]),
 		];
 
 		return $this->Html->tag('span', $bar, $attributes);
@@ -89,10 +89,21 @@ class ProgressHelper extends Helper {
 	}
 
 	/**
+	 * @param float|int $total
+	 * @param float|int $is
+	 * @return float
+	 */
+	public function calculatePercentage($total, $is) {
+		$percentage = $total ? $is / $total : 0.0;
+
+		return $this->roundPercentage($percentage);
+	}
+
+	/**
 	 * @param float $percentage
 	 * @return float
 	 */
-	public function calculatePercentage($percentage) {
+	public function roundPercentage($percentage) {
 		$percentageRounded = round($percentage, 2);
 		if ($percentageRounded === 0.00 && $percentage > 0.0) {
 			$percentage = 0.01;
@@ -101,7 +112,7 @@ class ProgressHelper extends Helper {
 			$percentage = 0.99;
 		}
 
-		return $percentage;
+		return (float)$percentage;
 	}
 
 	/**

+ 22 - 2
tests/TestCase/View/Helper/ProgressHelperTest.php

@@ -72,10 +72,30 @@ class ProgressHelperTest extends TestCase {
 	 * @return void
 	 */
 	public function testCalculatePercentage() {
-		$result = $this->progressHelper->calculatePercentage(0.001);
+		$result = $this->progressHelper->calculatePercentage(0, 0);
+		$this->assertSame(0.00, $result);
+
+		$result = $this->progressHelper->calculatePercentage(0.0, 0.0);
+		$this->assertSame(0.00, $result);
+
+		$result = $this->progressHelper->calculatePercentage(997, 1);
+		$this->assertSame(0.01, $result);
+
+		$result = $this->progressHelper->calculatePercentage(997, 996);
+		$this->assertSame(0.99, $result);
+
+		$result = $this->progressHelper->calculatePercentage(997, 997);
+		$this->assertSame(1.00, $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testRoundPercentage() {
+		$result = $this->progressHelper->roundPercentage(0.001);
 		$this->assertSame(0.01, $result);
 
-		$result = $this->progressHelper->calculatePercentage(0.999);
+		$result = $this->progressHelper->roundPercentage(0.999);
 		$this->assertSame(0.99, $result);
 	}