1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataText;
9
10class Locale extends MetadataText
11{
12
13    const PROPERTY_NAME = "locale";
14
15    private string $separator = "_";
16
17    public static function createForPage(MarkupPath $page, string $separator = "_"): Locale
18    {
19        return (new Locale())
20            ->setSeparator($separator)
21            ->setResource($page);
22    }
23
24    static public function getTab(): string
25    {
26        return MetaManagerForm::TAB_LANGUAGE_VALUE;
27    }
28
29    static public function getDescription(): string
30    {
31        return "The locale define the language and the formatting of numbers and time for the page. It's generated from the language and region metadata.";
32    }
33
34    static public function getLabel(): string
35    {
36        return "Locale";
37    }
38
39    /**
40     * @return string
41     */
42    public function getValue(): string
43    {
44
45        $page = $this->getResource();
46        if (!($page instanceof MarkupPath)) {
47            LogUtility::internalError("The locale is only implemented for page resources");
48            return $this->getDefaultValue();
49        }
50        $lang = $page->getLangOrDefault();
51        $country = $page->getRegionOrDefault();
52
53        return $lang . $this->separator . strtoupper($country);
54
55
56    }
57
58
59    public static function getName(): string
60    {
61        return self::PROPERTY_NAME;
62    }
63
64    static public function getPersistenceType(): string
65    {
66        return Metadata::DERIVED_METADATA;
67    }
68
69    static public function isMutable(): bool
70    {
71        return false;
72    }
73
74    /**
75     * @return string
76     */
77    public function getDefaultValue(): string
78    {
79        /**
80         * The value of {@link locale_get_default()} is with an underscore
81         * We follow this lead
82         */
83        return Site::getLocale($this->separator);
84    }
85
86    static public function getCanonical(): string
87    {
88        return "locale";
89    }
90
91    /**
92     * @return string
93     */
94    public function getValueOrDefault(): string
95    {
96        return $this->getValue();
97    }
98
99    public function setSeparator(string $separator): Locale
100    {
101        $this->separator = $separator;
102        return $this;
103    }
104
105
106    static public function isOnForm(): bool
107    {
108        return true;
109    }
110}
111