Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

GenericPlaceMapperConfig   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerializeConfig() 0 3 1
A jsonDeserialize() 0 9 3
A setConfig() 0 4 1
A has() 0 3 1
A get() 0 3 1
A config() 0 3 1
A jsonSerialize() 0 5 1
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage GeoDispersion
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2021, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
12
declare(strict_types=1);
13
14
namespace MyArtJaub\Webtrees\Common\GeoDispersion\Config;
15
16
use MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface;
17
18
/**
19
 * Generic Place Mapper configuration.
20
 * This configuration is based on an associate array holding the configuration.
21
 */
22
class GenericPlaceMapperConfig implements PlaceMapperConfigInterface
23
{
24
    private array $config = [];
25
    
26
    /**
27
     * Get the generic mapper's config
28
     * 
29
     * @return array
30
     */
31
    public function config(): array
32
    {
33
        return $this->config;
34
    }
35
    
36
    /**
37
     * Set the generic mapper's config
38
     * 
39
     * @param array $config
40
     * @return self
41
     */
42
    public function setConfig(array $config): self
43
    {
44
        $this->config = $config;
45
        return $this;
46
    }
47
    
48
    /**
49
     * {@inheritDoc}
50
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::get()
51
     */
52
    public function get(string $key, $default = null)
53
    {
54
        return $this->config[$key] ?? $default;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::has()
60
     */
61
    public function has(string $key): bool
62
    {
63
        return key_exists($key, $this->config);
64
    }
65
    
66
    /**
67
     * {@inheritDoc}
68
     * @see \JsonSerializable::jsonSerialize()
69
     */
70
    public function jsonSerialize()
71
    {
72
        return [
73
            'class'     =>  get_class($this),
74
            'config'    =>  $this->jsonSerializeConfig()
75
        ];
76
    }
77
    
78
    /**
79
     * Returns a representation of the mapper config compatible with Json serialisation
80
     * 
81
     * @return mixed
82
     */
83
    public function jsonSerializeConfig()
84
    {
85
        return $this->config;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::jsonDeserialize()
91
     */
92
    public function jsonDeserialize($config): self
93
    {
94
        if(is_string($config)) {
95
            return $this->jsonDeserialize(json_decode($config));
96
        }
97
        if(is_array($config)) {
98
            return $this->setConfig($config);
99
        }
100
        return $this;
101
    }
102
103
104
}