1<?php 2 3namespace ComboStrap; 4 5use ComboStrap\Meta\Api\Metadata; 6use ComboStrap\Meta\Api\MetadataText; 7use ComboStrap\Meta\Api\MetadataWikiPath; 8 9class Slug extends MetadataText 10{ 11 12 public const PROPERTY_NAME = "slug"; 13 14 15 public static function createForPage(ResourceCombo $resource) 16 { 17 return (new Slug()) 18 ->setResource($resource); 19 } 20 21 static public function getCanonical(): string 22 { 23 return self::PROPERTY_NAME; 24 } 25 26 27 /** 28 * The goal is to get only words that can be interpreted 29 * We could also encode it 30 * @param $string 31 * @return string|null 32 */ 33 public static function toSlugPath($string): ?string 34 { 35 if (empty($string)) return null; 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 return parent::setFromStoreValueWithoutException(self::toSlugPath($value)); 85 } 86 87 88 static public function getName(): string 89 { 90 return self::PROPERTY_NAME; 91 } 92 93 static public function getPersistenceType(): string 94 { 95 return Metadata::PERSISTENT_METADATA; 96 } 97 98 static public function isMutable(): bool 99 { 100 return true; 101 } 102 103 /** 104 * @return string 105 */ 106 public function getDefaultValue(): string 107 { 108 $title = PageTitle::createForMarkup($this->getResource()) 109 ->getValueOrDefault(); 110 return self::toSlugPath($title); 111 } 112 113 static public function isOnForm(): bool 114 { 115 return true; 116 } 117} 118