1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Meta\Api\Metadata;
6use ComboStrap\Meta\Api\MetadataText;
7
8class Slug extends MetadataText
9{
10
11    public const PROPERTY_NAME = "slug";
12
13
14    public static function createForPage(ResourceCombo $resource)
15    {
16        return (new Slug())
17            ->setResource($resource);
18    }
19
20    static public function getCanonical(): string
21    {
22        return self::PROPERTY_NAME;
23    }
24
25
26    /**
27     * The goal is to get only words that can be interpreted
28     * We could also encode it
29     * @param $string
30     * @return string
31     * @throws ExceptionNull
32     */
33    public static function toSlugPath($string): string
34    {
35        if (empty($string)) throw new ExceptionNull("The slug value should not be empty");
36        $excludedCharacters = array_merge(WikiPath::getReservedWords(), StringUtility::SEPARATORS_CHARACTERS);
37        $excludedCharacters[] = WikiPath::SLUG_SEPARATOR;
38        $parts = explode(WikiPath::NAMESPACE_SEPARATOR_DOUBLE_POINT, $string);
39        $parts = array_map(function ($e) use ($excludedCharacters) {
40            $wordsPart = StringUtility::getWords(
41                $e,
42                $excludedCharacters
43            );
44            // Implode and Lower case
45            return strtolower(implode(WikiPath::SLUG_SEPARATOR, $wordsPart));
46        }, $parts);
47
48        $slug = implode(WikiPath::NAMESPACE_SEPARATOR_DOUBLE_POINT, $parts);
49        // Space to separator
50        //$slugWithoutSpace = str_replace(" ", DokuPath::SLUG_SEPARATOR, $slugWithoutSpaceAroundParts);
51        // No double separator
52        //$slugWithoutDoubleSeparator = preg_replace("/" . DokuPath::SLUG_SEPARATOR . "{2,}/", DokuPath::SLUG_SEPARATOR, $slugWithoutSpace);
53        WikiPath::addRootSeparatorIfNotPresent($slug);
54        return $slug;
55    }
56
57    static public function getTab(): string
58    {
59        return MetaManagerForm::TAB_REDIRECTION_VALUE;
60    }
61
62    static public function getDescription(): string
63    {
64        return "The slug is used in the url of the page (if chosen)";
65    }
66
67    static public function getLabel(): string
68    {
69        return "Slug Path";
70    }
71
72    public function setFromStoreValue($value): Metadata
73    {
74        return $this->setFromStoreValueWithoutException($value);
75    }
76
77    public function setValue($value): Metadata
78    {
79        return $this->setFromStoreValueWithoutException($value);
80    }
81
82    public function setFromStoreValueWithoutException($value): Metadata
83    {
84        try {
85            $slug = self::toSlugPath($value);
86        } catch (ExceptionNull $e) {
87            $slug = null;
88        }
89        return parent::setFromStoreValueWithoutException($slug);
90    }
91
92
93    static public function getName(): string
94    {
95        return self::PROPERTY_NAME;
96    }
97
98    static public function getPersistenceType(): string
99    {
100        return Metadata::PERSISTENT_METADATA;
101    }
102
103    static public function isMutable(): bool
104    {
105        return true;
106    }
107
108    /**
109     * @return string
110     */
111    public function getDefaultValue(): string
112    {
113        $title = PageTitle::createForMarkup($this->getResource())->getValueOrDefault();
114        try {
115            return self::toSlugPath($title);
116        } catch (ExceptionNull $e) {
117            throw new \RuntimeException("The default title of the page (" . $this->getResource() . ") should not be empty.");
118        }
119
120    }
121
122    static public function isOnForm(): bool
123    {
124        return true;
125    }
126}
127