|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Umbrella\Ya\RetornoBoleto\Cnab\Cnab240\Segmento; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Stringy\Stringy; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Classe que identifica o tipo de arquivo de retorno sendo carregado e instancia a classe |
|
10
|
|
|
* específica para leitura do mesmo. |
|
11
|
|
|
* @author Ítalo Lelis de Vietro <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractSegmento implements SegmentoInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
const DECIMAL_POINTS = 2; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Formata uma string, contendo uma data sem o separador, no formato DDMMAA. |
|
20
|
|
|
* @param string $date String contendo a data no formato DDMMAA. |
|
21
|
|
|
* @param string $format |
|
22
|
|
|
* @return DateTime |
|
23
|
|
|
*/ |
|
24
|
|
|
public function createDate($date, $format = "mdy") |
|
25
|
|
|
{ |
|
26
|
|
|
if (empty($date)) { |
|
27
|
|
|
return ""; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return DateTime::createFromFormat($format, $date); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Formata uma string, contendo uma data sem o separador, no formato DDMMAA HHIISS. |
|
35
|
|
|
* @param string $dateTimeString String contendo a data no formato DDMMAA. |
|
36
|
|
|
* @return DateTime |
|
37
|
|
|
*/ |
|
38
|
|
|
public function createDateTime($dateTimeString, $format = "mdy His") |
|
39
|
|
|
{ |
|
40
|
|
|
if (empty($dateTimeString)) { |
|
41
|
|
|
return ""; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return DateTime::createFromFormat($format, $dateTimeString); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Converte uma stringy para um float, de acordo com a quantidade de casas decimais passadas |
|
49
|
|
|
* @param Stringy $string |
|
50
|
|
|
* @param int $decimalPoints |
|
51
|
|
|
* @return float |
|
52
|
|
|
*/ |
|
53
|
|
|
public function convertToFloat(Stringy $string, $decimalPoints = self::DECIMAL_POINTS) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!is_int($decimalPoints)) { |
|
56
|
|
|
$decimalPoints = self::DECIMAL_POINTS; |
|
57
|
|
|
} |
|
58
|
|
|
return (float) preg_replace('#(\d*)(\d{' . $decimalPoints . '})$#', '$1.$2', $string->__toString()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function convertToInt(Stringy $string) |
|
62
|
|
|
{ |
|
63
|
|
|
return (int)$string->__toString(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.