Completed
Push — master ( 88eef6...81252c )
by Joschi
02:44
created

GenericAuthor::unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.3149
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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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 1
    public function __construct($name, $email = null, $url = null)
76
    {
77 1
        $this->_name = $name;
78 1
        $this->_email = $email;
79 1
        $this->_url = $url;
80 1
    }
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 1
    public static function unserialize($str)
90
    {
91
        // If the author serialization is invalid
92 1
        if (!preg_match("%^([^\<]+)(?:\s\<([^\>]+)\>)?(?:\s\(([^\)]+)\))?$%", $str, $author)) {
93
            throw new InvalidArgumentException(
94
                sprintf('Invalid generic author "%s"', $str),
95
                InvalidArgumentException::INVALID_GENERIC_AUTHOR
96
            );
97
        }
98
99 1
        $author = array_pad($author, 4, null);
100 1
        return new static($author[1], $author[2], $author[3]);
101
    }
102
103
    /**
104
     * Return a signature uniquely representing this author
105
     *
106
     * @return string Author signature
107
     */
108 1
    public function getSignature()
109
    {
110 1
        return sha1($this->serialize());
111
    }
112
113
    /**
114
     * Serialize the property
115
     *
116
     * @return mixed Property serialization
117
     */
118 1
    public function serialize()
119
    {
120 1
        $parts = [$this->_name];
121
122 1
        if (strlen($this->_email)) {
123
            $parts[] = '<'.$this->_email.'>';
124
        }
125
126 1
        if (strlen($this->_url)) {
127
            $parts[] = '('.$this->_url.')';
128
        }
129
130 1
        return implode(' ', $parts);
131
    }
132
}
133