From a9c5778232079a09000c519a414563a1e04e112d Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 28 Jul 2016 09:05:20 +0200 Subject: Split Calculatable in trait and interface --- classes/Calculatable.php | 63 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 15 deletions(-) (limited to 'classes/Calculatable.php') diff --git a/classes/Calculatable.php b/classes/Calculatable.php index 5d60704..76f89eb 100644 --- a/classes/Calculatable.php +++ b/classes/Calculatable.php @@ -22,35 +22,68 @@ * along with this program. If not, see . */ -define('CALCULATABLE_SUBTOTAL', 1); -define('CALCULATABLE_VAT', 2); -define('CALCULATABLE_TOTAL', 3); - /** - * The calculatable trait, to be used by something of which subtotal, VAT and - * total can be calculated + * The calculatable interface, to be used by something of which subtotal, VAT + * and total can be calculated */ -trait Calculatable { +interface Calculatable { + const SUBTOTAL = 1; + const VAT = 2; + const TOTAL = 3; + /** * Calculate the subtotal * * @return float The subtotal */ - abstract protected function calculateSubtotal(); + function calculateSubtotal(); /** * Calculate the VAT * * @return float The VAT */ - abstract protected function calculateVAT(); + function calculateVAT(); + + /** + * Calculate the total + * + * @return float The total + */ + function calculateTotal(); + + /** + * Calculate and possibly format + * + * Subtotal: the sum of the prices of the assignments excl. VAT + * VAT: the sum of all the VAT from all the assignments + * Total: the sum of subtotal and total + * + * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL + * @param int $round How many decimals to round the result on + * @param bool $format Whether to format the number nicely (for output) or not (for calculations) + * + * @throws PDOException If something went wrong with the database + * + * @return float|bool The calculated value rounded to $round decimals, or false on incorrect input + */ + function calculate($what = Calculatable::TOTAL, $round = 2, $format = true); +} + +/** + * The calculatable trait, to be used by something of which subtotal, VAT and + * total can be calculated + */ +trait StandardCalculatable { + abstract public function calculateSubtotal(); + abstract public function calculateVAT(); /** * Calculate the total * * @return float The total */ - protected function calculateTotal() { + public function calculateTotal() { return $this->calculateSubtotal() + $this->calculateVAT(); } @@ -61,7 +94,7 @@ trait Calculatable { * VAT: the sum of all the VAT from all the assignments * Total: the sum of subtotal and total * - * @param int $what Any of CALCULATABLE_SUBTOTAL, CALCULATABLE_VAT and CALCULATABLE_TOTAL + * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL * @param int $round How many decimals to round the result on * @param bool $format Whether to format the number nicely (for output) or not (for calculations) * @@ -69,16 +102,16 @@ trait Calculatable { * * @return float|bool The calculated value rounded to $round decimals, or false on incorrect input */ - public function calculate($what = CALCULATABLE_TOTAL, $round = 2, $format = true) { + public function calculate($what = Calculatable::TOTAL, $round = 2, $format = true) { $return = 0; switch ($what) { - case CALCULATABLE_SUBTOTAL: + case Calculatable::SUBTOTAL: $return = $this->calculateSubtotal(); break; - case CALCULATABLE_VAT: + case Calculatable::VAT: $return = $this->calculateVAT(); break; - case CALCULATABLE_TOTAL: + case Calculatable::TOTAL: $return = $this->calculateTotal(); break; default: -- cgit v1.2.3