Passed
Push — master ( 9b4e67...25f483 )
by Caen
02:57 queued 12s
created

Author::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * The Post Author Object Model.
9
 */
10
class Author implements \Stringable
11
{
12
    /**
13
     * The username of the author.
14
     * This is the key used to find authors in the config.
15
     *
16
     * @var string
17
     */
18
    public string $username;
19
20
    /**
21
     * The display name of the author.
22
     *
23
     * @var string|null
24
     */
25
    public ?string $name = null;
26
27
    /**
28
     * The author's website URI.
29
     *
30
     * Could for example, be a Twitter page, website,
31
     * or a hyperlink to more posts by the author.
32
     *
33
     * @var string|null
34
     */
35
    public ?string $website = null;
36
37
    /**
38
     * Construct a new Author object.
39
     *
40
     * Parameters are supplied through an array to make it
41
     * easy to load data from Markdown post front matter.
42
     *
43
     * @param  string  $username
44
     * @param  array|null  $data
45
     */
46
    public function __construct(string $username, ?array $data = [])
47
    {
48
        $this->username = $username;
49
        if (isset($data['name'])) {
50
            $this->name = $data['name'];
51
        }
52
        if (isset($data['website'])) {
53
            $this->website = $data['website'];
54
        }
55
    }
56
57
    public function __toString(): string
58
    {
59
        return $this->getName();
60
    }
61
62
    /**
63
     * Get the author's preferred name.
64
     *
65
     * @see \Hyde\Framework\Testing\Unit\AuthorGetNameTest
66
     *
67
     * @return string
68
     */
69
    public function getName(): string
70
    {
71
        return $this->name ?? $this->username;
72
    }
73
74
    public static function create(string $username, ?string $name = null, ?string $website = null): static
75
    {
76
        return new static($username, [
77
            'name' => $name,
78
            'website'=> $website,
79
        ]);
80
    }
81
82
    public static function all(): Collection
83
    {
84
        return new Collection(config('authors', []));
85
    }
86
87
    public static function get(string $username): static
88
    {
89
        return static::all()->firstWhere('username', $username)
90
            ?? static::create($username);
91
    }
92
}
93