Registry::setUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm;
7
8
use Slince\Crm\Exception\InvalidArgumentException;
9
10
class Registry
11
{
12
    /**
13
     * Registry name
14
     * @var string
15
     */
16
    protected $name;
17
18
    /**
19
     * Registry url
20
     * @var string
21
     */
22
    protected $url;
23
24
    /**
25
     * Registry homepage
26
     * @var string
27
     */
28
    protected $homepage;
29
30
    /**
31
     * Registry author
32
     * @var string
33
     */
34
    protected $author;
35
36
    public function __construct($name, $url, $homepage = null, $author = null)
37
    {
38
        $this->name = $name;
39
        $this->url = $url;
40
        $this->homepage = $homepage;
41
        $this->author = $author;
42
    }
43
44
    /**
45
     * @param string $name
46
     */
47
    public function setName($name)
48
    {
49
        $this->name = $name;
50
    }
51
52
    /**
53
     * @param string $url
54
     */
55
    public function setUrl($url)
56
    {
57
        $this->url = $url;
58
    }
59
60
    /**
61
     * @param string $homepage
62
     */
63
    public function setHomepage($homepage)
64
    {
65
        $this->homepage = $homepage;
66
    }
67
68
    /**
69
     * @param string $author
70
     */
71
    public function setAuthor($author)
72
    {
73
        $this->author = $author;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getName()
80
    {
81
        return $this->name;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getUrl()
88
    {
89
        return $this->url;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getHomepage()
96
    {
97
        return $this->homepage;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getAuthor()
104
    {
105
        return $this->author;
106
    }
107
108
    /**
109
     * convert to array
110
     * @return array
111
     */
112
    public function toArray()
113
    {
114
        return [
115
            'name' => $this->name,
116
            'url' => $this->url,
117
            'homepage' => $this->homepage,
118
            'author' => $this->author,
119
        ];
120
    }
121
122
    /**
123
     * factory method
124
     * @param $registryData
125
     * @throws InvalidArgumentException
126
     * @return static
127
     */
128
    public static function create($registryData)
129
    {
130
        if (empty($registryData['name']) || empty($registryData['url'])) {
131
            throw new InvalidArgumentException("Registry data must contain key [name] and [url]");
132
        }
133
        $homepage = isset($registryData['homepage']) ? $registryData['homepage'] : '';
134
        $author = isset($registryData['author']) ? $registryData['author'] : '';
135
        return new static($registryData['name'], $registryData['url'], $homepage, $author);
136
    }
137
}
138