Passed
Branch master (2b4380)
by Wilder
02:46 queued 01:22
created

Schema::postalAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 14
rs 10
1
<?php
2
3
4
namespace WilderAmorim\StructuredData;
5
6
/**
7
 * Class Schema
8
 * @package WilderAmorim\StructuredData
9
 */
10
abstract class Schema
11
{
12
    /** @var */
13
    protected $person;
14
15
    /** @var */
16
    protected $organization;
17
18
    /** @var */
19
    protected $imageObject;
20
21
    /** @var string */
22
    protected $webPageName;
23
24
    /**  @var string */
25
    protected $webPageUrl;
26
27
    /** @var array */
28
    protected $webPageSameAs;
29
30
    /**
31
     * @see http://schema.org
32
     */
33
    const CONTEXT = 'http://schema.org';
34
35
    /**
36
     * @see https://schema.org/WebPage
37
     */
38
    const TYPE = 'WebPage';
39
40
    /**
41
     * Schema constructor.
42
     */
43
    public function __construct()
44
    {
45
        //
46
    }
47
48
    /**
49
     * @see https://schema.org/Person
50
     *
51
     * @param string $name
52
     * @param string $image
53
     * @param array|null $sameAs
54
     * @return object
55
     */
56
    protected function person(string $name, string $image, ?array $sameAs = null): object
57
    {
58
        $this->person = (object)[
59
            '@type' => 'Person',
60
            'name' => $name,
61
            'image' => $image,
62
            'sameAs' => $sameAs
63
        ];
64
        return $this->person;
65
    }
66
67
    /**
68
     * @see https://schema.org/Organization
69
     *
70
     * @param string $logo
71
     * @param string $image
72
     * @return Schema
73
     */
74
    protected function organization(
75
        string $logo,
76
        string $image,
77
        ?array $address = null
78
    ): object {
79
        $logo = $this->imageObject($logo, null, null);
80
        $image = $this->imageObject($image, null, null);
81
        $this->organization = [
82
            '@type' => 'Organization',
83
            'name' => $this->webPageName,
84
            'url' => $this->webPageUrl,
85
            'logo' => $logo,
86
            'image' => $image,
87
            'sameAs' => ($this->webPageSameAs ?? null)
88
        ];
89
90
        if (!is_null($address)) {
91
            $this->organization["address"] = $this->postalAddress(
92
                $address["addressLocality"],
93
                $address["addressRegion"],
94
                $address["postalCode"],
95
                $address["streetAddress"]);
96
        }
97
98
        return (object)$this->organization;
99
    }
100
101
    protected function postalAddress(
102
        string $addressLocality,
103
        string $addressRegion,
104
        string $postalCode,
105
        string $streetAddress
106
    ): array {
107
        $postalAddress = [
108
            "@type" => "PostalAddress",
109
            "addressLocality" => $addressLocality,
110
            "addressRegion" => $addressRegion,
111
            "postalCode" => $postalCode,
112
            "streetAddress" => $streetAddress
113
        ];
114
        return $postalAddress;
115
    }
116
117
    /**
118
     * @see https://schema.org/ImageObject
119
     *
120
     * @param string $url
121
     * @param int|null $width
122
     * @param int|null $height
123
     * @return array
124
     */
125
    protected function imageObject(string $url, ?int $width = 1280, ?int $height = 720): array
126
    {
127
        $this->imageObject = [
128
            '@type' => 'ImageObject',
129
            'url' => $url,
130
            'width' => $width,
131
            'height' => $height,
132
            'caption' => $this->webPageName
133
        ];
134
        return $this->imageObject;
135
136
    }
137
138
    /**
139
     * @param array $schema
140
     * @return string
141
     */
142
    protected function json(array $schema): string
143
    {
144
        return json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
145
    }
146
147
    /**
148
     * @param string $date
149
     * @return string
150
     */
151
    protected function setDate(string $date): string
152
    {
153
        $date = (strpos($date, ' ') ? explode(' ', $date)[0] : $date);
154
155
        $date = str_replace('/', '-', $date);
156
        $date = explode('-', $date);
157
158
        if (strlen($date[0]) == 4) {
159
            $date = "{$date[0]}-{$date[1]}-$date[2]";
160
        }
161
162
        if (strlen($date[2]) == 4) {
163
            $date = "{$date[2]}-{$date[1]}-$date[0]";
164
        }
165
166
        return $date;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $date could return the type string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
167
    }
168
}