xref: /plugin/combo/ComboStrap/Slug.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1<?php
2
3
4use ComboStrap\DokuPath;
5use ComboStrap\MetaManagerForm;
6use ComboStrap\Metadata;
7use ComboStrap\MetadataWikiPath;
8use ComboStrap\PageTitle;
9use ComboStrap\ResourceCombo;
10
11class Slug extends MetadataWikiPath
12{
13
14    public const PROPERTY_NAME = "slug";
15
16    public static function createForPage(ResourceCombo $resource)
17    {
18        return (new Slug())
19            ->setResource($resource);
20    }
21
22    public function getCanonical(): string
23    {
24        return self::PROPERTY_NAME;
25    }
26
27
28    public static function toSlugPath($string): ?string
29    {
30        if (empty($string)) return null;
31        // Reserved word to space
32        $slugWithoutReservedWord = str_replace(DokuPath::getReservedWords(), " ", $string);
33        // Doubles spaces to space
34        $slugWithoutDoubleSpace = preg_replace("/\s{2,}/", " ", $slugWithoutReservedWord);
35        // Trim space
36        $slugTrimmed = trim($slugWithoutDoubleSpace);
37        // No Space around the path part
38        $slugParts = explode(DokuPath::PATH_SEPARATOR, $slugTrimmed);
39        $slugParts = array_map(function ($e) {
40            return trim($e);
41        }, $slugParts);
42        $slugWithoutSpaceAroundParts = implode(DokuPath::PATH_SEPARATOR, $slugParts);
43        // Space to separator
44        $slugWithoutSpace = str_replace(" ", DokuPath::SLUG_SEPARATOR, $slugWithoutSpaceAroundParts);
45        // No double separator
46        $slugWithoutDoubleSeparator = preg_replace("/" . DokuPath::SLUG_SEPARATOR . "{2,}/", DokuPath::SLUG_SEPARATOR, $slugWithoutSpace);
47        // Root
48        DokuPath::addRootSeparatorIfNotPresent($slugWithoutDoubleSeparator);
49        // Lower case
50        return strtolower($slugWithoutDoubleSeparator);
51    }
52
53    public function getTab(): string
54    {
55        return MetaManagerForm::TAB_REDIRECTION_VALUE;
56    }
57
58    public function getDescription(): string
59    {
60        return "The slug is used in the url of the page (if chosen)";
61    }
62
63    public function getLabel(): string
64    {
65        return "Slug Path";
66    }
67
68    public function setFromStoreValue($value): Metadata
69    {
70        return $this->buildFromStoreValue($value);
71    }
72
73    public function setValue($value): Metadata
74    {
75        return $this->buildFromStoreValue($value);
76    }
77
78    public function buildFromStoreValue($value): Metadata
79    {
80        return parent::buildFromStoreValue(self::toSlugPath($value));
81    }
82
83
84    static public function getName(): string
85    {
86        return self::PROPERTY_NAME;
87    }
88
89    public function getPersistenceType(): string
90    {
91        return Metadata::PERSISTENT_METADATA;
92    }
93
94    public function getMutable(): bool
95    {
96        return true;
97    }
98
99    public function getDefaultValue(): ?string
100    {
101        $title = PageTitle::createForPage($this->getResource())
102            ->getValueOrDefault();
103        if ($title === null) {
104            return null;
105        }
106        return self::toSlugPath($title);
107    }
108}
109