1<?php 2 3 4namespace ComboStrap; 5 6 7use action_plugin_combo_metaprocessing; 8use ComboStrap\Meta\Api\MetadataText; 9use ComboStrap\Meta\Store\MetadataDokuWikiStore; 10 11class ResourceName extends MetadataText 12{ 13 14 15 public const PROPERTY_NAME = "name"; 16 17 public static function createForResource(ResourceCombo $resource): ResourceName 18 { 19 return (new ResourceName()) 20 ->setResource($resource); 21 } 22 23 /** 24 * Return a name from a path 25 * @param Path $path 26 * @return string 27 */ 28 public static function getFromPath(Path $path): string 29 { 30 try { 31 $name = $path->getLastNameWithoutExtension(); 32 } catch (ExceptionNotFound $e) { 33 try { 34 $name = $path->getUrl()->getHost(); 35 } catch (ExceptionNotFound $e) { 36 return "Unknown"; 37 } 38 } 39 $words = preg_split("/\s/", preg_replace("/[-_]/", " ", $name)); 40 $wordsUc = []; 41 foreach ($words as $word) { 42 $wordsUc[] = ucfirst($word); 43 } 44 return implode(" ", $wordsUc); 45 46 } 47 48 public static function getTab(): string 49 { 50 return MetaManagerForm::TAB_PAGE_VALUE; 51 } 52 53 public static function getDescription(): string 54 { 55 56 return "A name is the shortest description. It should be at maximum a couple of words long. It's used in navigational components or as a default in link."; 57 58 } 59 60 public static function getLabel(): string 61 { 62 return "The name of a page"; 63 } 64 65 static public function getName(): string 66 { 67 return self::PROPERTY_NAME; 68 } 69 70 public static function getPersistenceType(): string 71 { 72 return MetadataDokuWikiStore::PERSISTENT_DOKUWIKI_KEY; 73 } 74 75 public static function isMutable(): bool 76 { 77 return true; 78 } 79 80 /** 81 * @return string 82 */ 83 public function getDefaultValue(): string 84 { 85 86 $resourceCombo = $this->getResource(); 87 88 /** 89 * If this is a home page, the default 90 * is the parent path name 91 */ 92 $path = $resourceCombo->getPathObject(); 93 if ($resourceCombo instanceof MarkupPath) { 94 95 96 if ($resourceCombo->isIndexPage() && !$resourceCombo->isRootHomePage()) { 97 98 try { 99 $path = $path->getParent(); 100 } catch (ExceptionNotFound $e) { 101 // no parent path 102 // should not happen because even the home page (:start) has 103 // a parent (ie :) 104 return Site::getIndexPageName(); 105 } 106 } 107 } 108 109 return self::getFromPath($path); 110 111 112 } 113 114 public static function getCanonical(): string 115 { 116 return static::getName(); 117 } 118 119 /** 120 * @return string 121 */ 122 public function getValueOrDefault(): string 123 { 124 try { 125 return $this->getValue(); 126 } catch (ExceptionNotFound $e) { 127 return $this->getDefaultValue(); 128 } 129 } 130 131 public static function isOnForm(): bool 132 { 133 return true; 134 } 135} 136