1 <?php
2
3 namespace Balance\Posting;
4
5 use InvalidArgumentException;
6
7 /**
8 * Verificador de Lançamentos Balanceados
9 */
10 class Checker
11 {
12 const CREDIT = 'CREDIT';
13 const DEBIT = 'DEBIT';
14
15 /**
16 * Diferença Encontrada
17 * @type float
18 */
19 protected $difference = 0.0;
20
21 /**
22 * Adiciona um Valor ao Verificador
23 *
24 * @param string $type Tipo de Valor (Crédito ou Débito)
25 * @param float $value Valor para Adição
26 * @return Checker Próprio Objeto para Encadeamento
27 */
28 public function addValue($type, $value)
29 {
30 // Validação
31 if (! $value) {
32 // Valor Vazio
33 throw new InvalidArgumentException('Empty Value');
34 }
35 // Pesquisa
36 switch ($type) {
37 case self::CREDIT:
38 // Configuração
39 $this->difference = (float) bcadd($this->difference, $value, 2);
40 break;
41 case self::DEBIT:
42 // Configuração
43 $this->difference = (float) bcsub($this->difference, $value, 2);
44 break;
45 default:
46 throw new InvalidArgumentException("Invalid Type: '$type'");
47 }
48 // Encadeamento
49 return $this;
50 }
51
52 /**
53 * Verificador Válido?
54 *
55 * Efetua a verificação se todos os valores adicionados estão balanceados entre crédito e débito, retornando verdade
56 * se a diferença encontrada é igual a zero.
57 *
58 * @return bool Confirmação Solicitada
59 */
60 public function isValid()
61 {
62 return $this->difference === 0.0;
63 }
64
65 /**
66 * Apresenta a Diferença Encontrada
67 *
68 * @return float Valor Solicitado
69 */
70 public function getDifference()
71 {
72 return $this->difference;
73 }
74 }
75