|
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 Platine\Framework\Demo\Form\Param; |
|
10
|
|
|
|
|
11
|
|
|
use Platine\Orm\Entity; |
|
12
|
|
|
use Platine\Stdlib\Helper\Str; |
|
13
|
|
|
|
|
14
|
|
|
class BaseParam |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create new instance |
|
19
|
|
|
* @param array<string, mixed> $data |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(array $data = []) |
|
22
|
|
|
{ |
|
23
|
|
|
$params = array_merge($this->getDefault(), $data); |
|
24
|
|
|
$this->load($params); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Load the field data |
|
29
|
|
|
* @param array<string, mixed> $data |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function load(array $data): void |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($data as $name => $value) { |
|
35
|
|
|
$key = Str::camel($name, true); |
|
36
|
|
|
|
|
37
|
|
|
$setterMethod = 'set' . ucfirst($key); |
|
38
|
|
|
if (method_exists($this, $setterMethod)) { |
|
39
|
|
|
$this->{$setterMethod}($value); |
|
40
|
|
|
} elseif (property_exists($this, $key)) { |
|
41
|
|
|
$this->{$key} = $value; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function fromEntity(Entity $entity): self |
|
47
|
|
|
{ |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return the fields default values |
|
53
|
|
|
* @return array<string, mixed> |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getDefault(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return the value for the given property |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @return mixed|null |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __get($name) |
|
66
|
|
|
{ |
|
67
|
|
|
if (property_exists($this, $name)) { |
|
68
|
|
|
return $this->{$name}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|