|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2018 Ross Mitchell |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of RossMitchell/UpdateCloudFlare. |
|
8
|
|
|
* |
|
9
|
|
|
* RossMitchell/UpdateCloudFlare is free software: you can redistribute |
|
10
|
|
|
* it and/or modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace RossMitchell\UpdateCloudFlare\Helpers; |
|
24
|
|
|
|
|
25
|
|
|
use Symfony\Component\Console\Exception\LogicException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Hydrator |
|
29
|
|
|
* @package RossMitchell\UpdateCloudFlare\Helpers |
|
30
|
|
|
*/ |
|
31
|
|
|
class Hydrator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @param object $class |
|
35
|
|
|
* @param \stdClass $data |
|
36
|
|
|
* @param string $node |
|
37
|
|
|
* @param bool $required |
|
38
|
|
|
* |
|
39
|
|
|
* @throws LogicException |
|
40
|
|
|
*/ |
|
41
|
124 |
|
public function setProperty($class, \stdClass $data, string $node, $required = true): void |
|
42
|
|
|
{ |
|
43
|
124 |
|
if (!\property_exists($data, $node)) { |
|
44
|
39 |
|
if ($required === true) { |
|
45
|
38 |
|
throw new LogicException("$node does not exist in the result"); |
|
46
|
|
|
} |
|
47
|
1 |
|
$data->$node = null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
119 |
|
$settor = 'set' . str_replace('_', '', ucwords($node, '_')); |
|
51
|
|
|
|
|
52
|
119 |
|
if (!\method_exists($class, $settor)) { |
|
53
|
1 |
|
throw new LogicException("method $settor does not exist in class " . \get_class($class)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
118 |
|
$class->$settor($data->$node); |
|
57
|
118 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|