1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OBRSDK\Entidades\Abstratos; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of AEntidadePreenchimento |
13
|
|
|
* |
14
|
|
|
* @author Antonio |
15
|
|
|
*/ |
16
|
|
|
abstract class AEntidadePreenchimento { |
17
|
|
|
|
18
|
|
|
public function setAtributos(array $atributos) { |
19
|
|
|
foreach ($atributos as $atributoNome => $valor) { |
20
|
|
|
if (property_exists($this, $atributoNome)) { |
21
|
|
|
$this->preencherPropriedade($atributoNome, $valor); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function preencherPropriedade($propriedade, $valor) { |
27
|
|
|
if (is_object($this->$propriedade) || is_array($this->$propriedade)) { |
28
|
|
|
$this->$propriedade = $this->tratarValorPropriedade($propriedade, $valor); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->$propriedade = $valor; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function tratarValorPropriedade($propriedade, $valor) { |
36
|
|
|
if (is_array($this->$propriedade)) { |
37
|
|
|
$return = []; |
38
|
|
|
foreach ($valor as $vv) { |
39
|
|
|
$return[] = $this->getObject($propriedade, $vv); |
40
|
|
|
} |
41
|
|
|
return $return; |
42
|
|
|
} else { |
43
|
|
|
return $this->getObject($propriedade, $valor); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getObject($objectNome, $valor) { |
48
|
|
|
$namespaceClass = '\OBRSDK\Entidades\\' . $this->underscoreParaPascalCase($objectNome); |
49
|
|
|
|
50
|
|
|
if (!class_exists($namespaceClass)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$instance = new $namespaceClass; |
55
|
|
|
$instance->setAtributos($valor); |
56
|
|
|
return $instance; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function pascalCaseParaUnderscore($texto) { |
60
|
|
|
return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $texto)), '_'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function underscoreParaPascalCase($texto) { |
64
|
|
|
return trim(lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $texto))))); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|