Completed
Push — master ( 81c425...a2bf82 )
by Joschi
02:41
created

GenericAuthor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Author;
38
39
use Apparat\Object\Domain\Contract\SerializablePropertyInterface;
40
41
/**
42
 * Generic author
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain
46
 */
47
class GenericAuthor implements AuthorInterface
48
{
49
    /**
50
     * Name
51
     *
52
     * @var string
53
     */
54
    private $name;
55
    /**
56
     * Email address
57
     *
58
     * @var string
59
     */
60
    private $email;
61
    /**
62
     * URL
63
     *
64
     * @var string
65
     */
66
    private $url;
67
68
    /**
69
     * Generic author constructor
70
     *
71
     * @param string $name Name
72
     * @param string $email Email address
73
     * @param string $url URL
74
     */
75 15
    public function __construct($name, $email = null, $url = null)
76
    {
77 15
        $this->name = $name;
78 15
        $this->email = $email;
79 15
        $this->url = $url;
80 15
    }
81
82
    /**
83
     * Unserialize the string representation of this property
84
     *
85
     * @param string $str Serialized property
86
     * @return SerializablePropertyInterface Property
87
     * @throws InvalidArgumentException If the generic author is invalid
88
     */
89 15
    public static function unserialize($str)
90
    {
91
        // If the author serialization is invalid
92 15
        if (!strlen(trim($str)) ||
93 15
            !preg_match("%^([^\<]+)?(?:\s+\<([^\>]+)\>)?(?:\s+\(([^\)]+)\))?$%", $str, $author)
94 15
        ) {
95
            throw new InvalidArgumentException(
96
                sprintf('Invalid generic author "%s"', $str),
97
                InvalidArgumentException::INVALID_GENERIC_AUTHOR
98
            );
99
        }
100
101 15
        $author = array_pad($author, 4, null);
102 15
        return new static($author[1], $author[2], $author[3]);
103
    }
104
105
    /**
106
     * Return a signature uniquely representing this author
107
     *
108
     * @return string Author signature
109
     */
110 14
    public function getSignature()
111
    {
112 14
        return sha1($this->serialize());
113
    }
114
115
    /**
116
     * Serialize the property
117
     *
118
     * @return mixed Property serialization
119
     */
120 15
    public function serialize()
121
    {
122 15
        $parts = [$this->name];
123
124 15
        if (strlen($this->email)) {
125 2
            $parts[] = '<'.$this->email.'>';
126 2
        }
127
128 15
        if (strlen($this->url)) {
129 2
            $parts[] = '('.$this->url.')';
130 2
        }
131
132 15
        return implode(' ', $parts);
133
    }
134
}
135