|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entities\PersonEntity; |
|
6
|
|
|
use Nette\Database\Context; |
|
7
|
|
|
use Nette\Reflection\ClassType; |
|
8
|
|
|
use App\Entities\IEntity; |
|
9
|
|
|
use \Exception; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Social Logins |
|
13
|
|
|
* |
|
14
|
|
|
* class for handling social logins |
|
15
|
|
|
*/ |
|
16
|
|
View Code Duplication |
class PersonModel extends BaseModel implements IModel |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $table = 'kk_persons'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Context $database |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Context $database) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->setDatabase($database); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return ActiveRow |
|
32
|
|
|
*/ |
|
33
|
|
|
public function all() |
|
34
|
|
|
{ |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int $id |
|
40
|
|
|
* @return PersonEntity |
|
41
|
|
|
*/ |
|
42
|
|
|
public function find($id): PersonEntity |
|
43
|
|
|
{ |
|
44
|
|
|
$block = parent::find($id); |
|
45
|
|
|
return $this->hydrate($block); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param IEntity $entity |
|
50
|
|
|
* @return bool|mixed |
|
51
|
|
|
* @throws Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
public function save(IEntity $entity) |
|
54
|
|
|
{ |
|
55
|
|
|
if ($entity->getId() === null) { |
|
56
|
|
|
$values = $entity->toArray(); |
|
57
|
|
|
|
|
58
|
|
|
$result = $this->create($values); |
|
59
|
|
|
//$result = $this->setIdentity($row, $row->id); |
|
60
|
|
|
} else { |
|
61
|
|
|
$values = $entity->toArray(); |
|
62
|
|
|
$result = $this->update($entity->getId(), $values); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $values |
|
70
|
|
|
* @return PersonEntity |
|
71
|
|
|
*/ |
|
72
|
|
|
public function hydrate($values): PersonEntity |
|
73
|
|
|
{ |
|
74
|
|
|
$entity = new PersonEntity(); |
|
75
|
|
|
//$this->setIdentity($entity, $values->id); |
|
76
|
|
|
|
|
77
|
|
|
// unset($values['id']); |
|
78
|
|
|
foreach ($values as $property => $value) { |
|
79
|
|
|
$entity->$property = $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $entity; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $item |
|
88
|
|
|
* @param $id |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
private function setIdentity($item, $id) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$ref = new ClassType($item); |
|
94
|
|
|
$idProperty = $ref->getProperty('data'); |
|
95
|
|
|
$idProperty->setAccessible(true); |
|
96
|
|
|
$idProperty->setValue($item, $id); |
|
97
|
|
|
|
|
98
|
|
|
return $item; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.