xref: /plugin/combo/ComboStrap/Brand.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4namespace ComboStrap;
5
6
7class Brand
8{
9
10    const NEWSLETTER_BRAND_NAME = "newsletter";
11    const EMAIL_BRAND_NAME = "email";
12
13    public const BRAND_ABBREVIATIONS_MAPPING = [
14        "hn" => "hackernews",
15        "mail" => "email",
16        "wp" => "wikipedia"
17    ];
18    /**
19     * The brand of the current application/website
20     */
21    public const CURRENT_BRAND = "current";
22
23
24    private $secondaryColor;
25    private $brandUrl;
26
27    /**
28     * @var array
29     */
30    public static $brandDictionary;
31    /**
32     * @var bool
33     */
34    private $unknown = false;
35    /**
36     * @var mixed
37     */
38    private $brandDict;
39
40    /**
41     * Brand constructor.
42     * @param string $name
43     * @throws ExceptionCombo
44     */
45    public function __construct(string $name)
46    {
47        $this->name = strtolower($name);
48        if (isset(self::BRAND_ABBREVIATIONS_MAPPING[$this->name])) {
49            $this->name = self::BRAND_ABBREVIATIONS_MAPPING[$this->name];
50        }
51
52        /**
53         * Get the brands
54         */
55        Brand::$brandDictionary = Brand::getBrandDictionary();
56
57
58        /**
59         * Build the data for the brand
60         */
61        $this->brandDict = Brand::$brandDictionary[$this->name];
62        switch ($this->name) {
63            case self::CURRENT_BRAND:
64                $this->brandUrl = Site::getBaseUrl();
65                $secondaryColor = Site::getSecondaryColor();
66                if ($secondaryColor !== null) {
67                    // the predicates on the secondary value is to avoid a loop with the the function below
68                    $this->secondaryColor = $secondaryColor->toCssValue();
69                }
70                break;
71            default:
72                if ($this->brandDict !== null) {
73                    $this->secondaryColor = $this->brandDict["colors"]["secondary"];
74                    $this->brandUrl = $this->brandDict["url"];
75                    return;
76                }
77                $this->unknown = true;
78                break;
79        }
80
81    }
82
83    /**
84     * @return string[]
85     * @throws ExceptionCombo
86     */
87    public static function getAllKnownBrandNames(): array
88    {
89
90        $brandsDict = self::getBrandNamesFromDictionary();
91        $brandsAbbreviations = array_keys(self::BRAND_ABBREVIATIONS_MAPPING);
92        return array_merge(
93            $brandsDict,
94            $brandsAbbreviations,
95            [self::CURRENT_BRAND]
96        );
97    }
98
99    /**
100     * @throws ExceptionCombo
101     */
102    public static function getBrandNamesFromDictionary(): array
103    {
104        $brandDictionary = self::getBrandDictionary();
105        return array_keys($brandDictionary);
106    }
107
108    /**
109     * @throws ExceptionCombo
110     */
111    public static function getBrandDictionary(): array
112    {
113        if (Brand::$brandDictionary === null) {
114            Brand::$brandDictionary = Dictionary::getFrom("brands");
115        }
116        return Brand::$brandDictionary;
117    }
118
119
120    /**
121     * @var string
122     * The name of the brand,
123     * for company, we follow the naming of
124     * https://github.com/ellisonleao/sharer.js
125     */
126    private $name;
127
128    /**
129     * @throws ExceptionCombo
130     */
131    public static function create(string $brandName): Brand
132    {
133        return new Brand($brandName);
134    }
135
136    /**
137     * If the brand name is unknown (ie custom)
138     * @return bool
139     */
140    public function isUnknown(): bool
141    {
142        return $this->unknown;
143
144    }
145
146    public function getName(): string
147    {
148        return $this->name;
149    }
150
151    public function __toString()
152    {
153        if ($this->name === Brand::CURRENT_BRAND) {
154            return $this->name . " (" . Site::getTitle() . ")";
155        }
156        return $this->name;
157    }
158
159    /**
160     * Shared/Follow Url template
161     * the endpoint template url (for sharing and following)
162     * @var string $type - the type of button
163     */
164    public function getWebUrlTemplate(string $type): ?string
165    {
166        if (isset($this->brandDict[$type])) {
167            return $this->brandDict[$type]["web"];
168        }
169        return null;
170    }
171
172    /**
173     * Brand button title
174     * @return string
175     * @var string $type - the button type
176     */
177    public function getTitle(string $type): ?string
178    {
179        if ($this->name === self::CURRENT_BRAND) {
180            return Site::getTitle();
181        }
182        if ($this->brandDict !== null) {
183            if (isset($this->brandDict[$type])) {
184                return $this->brandDict[$type]["popup"];
185            }
186        }
187        return null;
188
189    }
190
191    public function getPrimaryColor(): ?string
192    {
193
194        if ($this->brandDict !== null) {
195            $primaryColor = $this->brandDict["colors"]["primary"];
196            if ($primaryColor !== null) {
197                return $primaryColor;
198            }
199        }
200
201        // Unknown or current brand / unknown color
202        $primaryColor = Site::getPrimaryColor();
203        if ($primaryColor !== null) {
204            return $primaryColor;
205        }
206
207        return null;
208
209    }
210
211    public function getSecondaryColor(): ?string
212    {
213        return $this->secondaryColor;
214    }
215
216    /**
217     * @param string $type - the button type
218     * @return string|null
219     */
220    public function getIconName(string $type): ?string
221    {
222
223        switch ($this->name) {
224            case self::CURRENT_BRAND:
225                $image = Site::getLogoAsSvgImage();
226                if ($image !== null) {
227                    $path = $image->getPath();
228                    if ($path instanceof DokuPath) {
229                        /**
230                         * End with svg, not seen as an external icon
231                         */
232                        return $path->getDokuwikiId();
233                    }
234                }
235                break;
236            default:
237                if (isset($this->brandDict["icons"])) {
238                    return $this->brandDict["icons"][$type];
239                }
240                break;
241        }
242
243        return null;
244    }
245
246    public function getBrandUrl(): ?string
247    {
248        return $this->brandUrl;
249    }
250
251    /**
252     */
253    public function supportButtonType(string $type): bool
254    {
255        switch ($type) {
256            case BrandButton::TYPE_BUTTON_SHARE:
257            case BrandButton::TYPE_BUTTON_FOLLOW:
258                if ($this->getWebUrlTemplate($type) !== null) {
259                    return true;
260                }
261                return false;
262            default:
263            case BrandButton::TYPE_BUTTON_BRAND:
264                return true;
265        }
266    }
267
268
269}
270