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 17 const SEPARATORS_CHARACTERS = [".", "(", ")", ","]; 18 19 public static function createForPage(ResourceCombo $resource) 20 { 21 return (new Slug()) 22 ->setResource($resource); 23 } 24 25 public function getCanonical(): string 26 { 27 return self::PROPERTY_NAME; 28 } 29 30 31 /** 32 * The goal is to get only words that can be interpreted 33 * We could also encode it 34 * @param $string 35 * @return string|null 36 */ 37 public static function toSlugPath($string): ?string 38 { 39 if (empty($string)) return null; 40 // Reserved word to space 41 $slugWithoutReservedWord = str_replace(DokuPath::getReservedWords(), " ", $string); 42 // Delete points, comma, parenthesis 43 $slugWithoutSeparator = str_replace(self::SEPARATORS_CHARACTERS, " ", $slugWithoutReservedWord); 44 // Doubles spaces to space 45 $slugWithoutDoubleSpace = preg_replace("/\s{2,}/", " ", $slugWithoutSeparator); 46 // Trim space 47 $slugTrimmed = trim($slugWithoutDoubleSpace); 48 // No Space around the path part 49 $slugParts = explode(DokuPath::PATH_SEPARATOR, $slugTrimmed); 50 $slugParts = array_map(function ($e) { 51 return trim($e); 52 }, $slugParts); 53 $slugWithoutSpaceAroundParts = implode(DokuPath::PATH_SEPARATOR, $slugParts); 54 // Space to separator 55 $slugWithoutSpace = str_replace(" ", DokuPath::SLUG_SEPARATOR, $slugWithoutSpaceAroundParts); 56 // No double separator 57 $slugWithoutDoubleSeparator = preg_replace("/" . DokuPath::SLUG_SEPARATOR . "{2,}/", DokuPath::SLUG_SEPARATOR, $slugWithoutSpace); 58 // Root 59 DokuPath::addRootSeparatorIfNotPresent($slugWithoutDoubleSeparator); 60 // Lower case 61 return strtolower($slugWithoutDoubleSeparator); 62 } 63 64 public function getTab(): string 65 { 66 return MetaManagerForm::TAB_REDIRECTION_VALUE; 67 } 68 69 public function getDescription(): string 70 { 71 return "The slug is used in the url of the page (if chosen)"; 72 } 73 74 public function getLabel(): string 75 { 76 return "Slug Path"; 77 } 78 79 public function setFromStoreValue($value): Metadata 80 { 81 return $this->buildFromStoreValue($value); 82 } 83 84 public function setValue($value): Metadata 85 { 86 return $this->buildFromStoreValue($value); 87 } 88 89 public function buildFromStoreValue($value): Metadata 90 { 91 return parent::buildFromStoreValue(self::toSlugPath($value)); 92 } 93 94 95 static public function getName(): string 96 { 97 return self::PROPERTY_NAME; 98 } 99 100 public function getPersistenceType(): string 101 { 102 return Metadata::PERSISTENT_METADATA; 103 } 104 105 public function getMutable(): bool 106 { 107 return true; 108 } 109 110 public function getDefaultValue(): ?string 111 { 112 $title = PageTitle::createForPage($this->getResource()) 113 ->getValueOrDefault(); 114 if ($title === null) { 115 return null; 116 } 117 return self::toSlugPath($title); 118 } 119} 120