Improve::facebook()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.6111
cc 5
nc 3
nop 2
1
<?php
2
3
namespace SimpSyst\Improve;
4
5
/**
6
 * Class SimpSyst Improve
7
 *
8
 * @author Diego Matos <https://github.com/diegoamatos>
9
 * @package SimpSyst\Improve
10
 */
11
class Improve extends MetaTags
12
{
13
    /**
14
     * @param string $title
15
     * @param string $description
16
     * @param string $url
17
     * @param string $image
18
     * @param bool $follow
19
     * @return Improve
20
     */
21
    public function improver(
22
        string $title,
23
        string $description,
24
        string $url,
25
        string $image,
26
        bool $follow = true
27
    ): Improve {
28
        $this->data($title, $description, $url, $image);
29
30
        $title = $this->filter($title);
31
        $description = $this->filter($description);
32
33
        $this->buildTag("title", $title);
34
        $this->buildMeta("name", ["description" => $description]);
35
        $this->buildMeta("name", ["robots" => ($follow ? "index, follow" : "noindex, nofollow")]);
36
        $this->buildLink("canonical", $url);
37
38
        foreach ($this->tags as $meta => $prefix) {
39
            $this->buildMeta($meta, [
40
                "{$prefix}:title" => $title,
41
                "{$prefix}:description" => $description,
42
                "{$prefix}:url" => $url,
43
                "{$prefix}:image" => $image
44
            ]);
45
        }
46
47
        $this->buildMeta("itemprop", [
48
            "name" => $title,
49
            "description" => $description,
50
            "url" => $url,
51
            "image" => $image
52
        ]);
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $fbPage
59
     * @param string|null $fbAuthor
60
     * @return Improve
61
     */
62
    public function publisher(string $fbPage, string $fbAuthor = null): Improve
63
    {
64
        $this->buildMeta("property", [
65
            "article:publisher" => "https://www.facebook.com/{$fbPage}"
66
        ]);
67
68
        if ($fbAuthor) {
69
            $this->buildMeta("property", [
70
                "article:author" => "https://www.facebook.com/{$fbAuthor}"
71
            ]);
72
        }
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $siteName
79
     * @param string $locale
80
     * @param string $schema
81
     * @return Improve
82
     */
83
    public function openGraph(string $siteName, string $locale = "pt_BR", string $schema = "article"): Improve
84
    {
85
        $prefix = "og";
86
        $siteName = $this->filter($siteName);
87
88
        $this->buildMeta("property", [
89
            "{$prefix}:type" => $schema,
90
            "{$prefix}:site_name" => $siteName,
91
            "{$prefix}:locale" => $locale
92
        ]);
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $creator
99
     * @param string $site
100
     * @param string $domain
101
     * @param string|null $card
102
     * @return Improve
103
     */
104
    public function twitterCard(string $creator, string $site, string $domain, string $card = null): Improve
105
    {
106
        $prefix = "twitter";
107
        $card = ($card ?? "summary_large_image");
108
109
        $this->buildMeta("name", [
110
            "{$prefix}:card" => $card,
111
            "{$prefix}:site" => $site,
112
            "{$prefix}:creator" => $creator,
113
            "{$prefix}:domain" => $domain
114
        ]);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Você deve usar UM ou OUTRO, se for usar $appid deixe o $admins em null.
121
     * Mas se for usar $admins, então deixe o $appid em null.
122
     * @param string|null $appId
123
     * @param array|null $admins
124
     * @return Improve
125
     */
126
    public function facebook(string $appId = null, array $admins = null): Improve
127
    {
128
        if ($appId) {
129
            $fb = $this->meta->addChild("meta");
130
            $fb->addAttribute("property", "fb:app_id");
131
            $fb->addAttribute("content", $appId);
132
            return $this;
133
        }
134
135
        if (!empty($admins) && is_array($admins)) {
136
            foreach ($admins as $admin) {
137
                $fb = $this->meta->addChild("meta");
138
                $fb->addAttribute("property", "fb:admins");
139
                $fb->addAttribute("content", $admin);
140
            }
141
        }
142
143
        return $this;
144
    }
145
}