1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Scrawler package. |
4
|
|
|
* |
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Scrawler\Arca\Traits\Model; |
12
|
|
|
|
13
|
|
|
use Scrawler\Arca\Model; |
14
|
|
|
use Doctrine\DBAL\Types\Type; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Setter trait to provide setter methods to the model. |
19
|
|
|
*/ |
20
|
|
|
trait Setter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Set all properties of model via array. |
24
|
|
|
* |
25
|
|
|
* @param array<mixed> $properties |
26
|
|
|
*/ |
27
|
|
|
public function setProperties(array $properties): Model |
28
|
|
|
{ |
29
|
|
|
foreach ($properties as $key => $value) { |
30
|
|
|
$this->set($key, $value); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set all properties of model loaded via database. |
38
|
|
|
* |
39
|
|
|
* @param array<mixed> $properties |
40
|
|
|
*/ |
41
|
|
|
public function setLoadedProperties(array $properties): Model |
42
|
|
|
{ |
43
|
|
|
$this->__properties['self'] = $properties; |
|
|
|
|
44
|
|
|
$this->setLoadedAllProperties($properties); |
45
|
|
|
foreach ($properties as $key => $value) { |
46
|
|
|
$this->__properties['type'][$key] = $this->tableManager->getTable($this->table)->getColumn($key)->getComment(); |
47
|
|
|
} |
48
|
|
|
$this->__meta['id'] = $properties['id']; |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $this; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* get php value from database value |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
private function setLoadedAllProperties($properties): void{ |
58
|
|
|
foreach ($properties as $key => $value) { |
59
|
|
|
$type = Type::getType($this->tableManager->getTable($this->table)->getColumn($key)->getComment()); |
60
|
|
|
$this->__properties['all'][$key] = $type->convertToPHPValue($value, $this->connection->getDatabasePlatform()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* call when model is loaded from database. |
66
|
|
|
*/ |
67
|
|
|
public function setLoaded(): Model |
68
|
|
|
{ |
69
|
|
|
$this->__meta['is_loaded'] = true; |
|
|
|
|
70
|
|
|
$this->__meta['id_error'] = false; |
71
|
|
|
|
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|