1<?php 2 3 4namespace ComboStrap\Meta\Api; 5 6use ComboStrap\DataType; 7use ComboStrap\ExceptionBadArgument; 8use ComboStrap\ExceptionNotFound; 9use ComboStrap\LogUtility; 10use ComboStrap\WikiPath; 11 12/** 13 * Class MetadataWikiPath 14 * @package ComboStrap 15 * A wiki path value where the separator is a {@link WikiPath::NAMESPACE_SEPARATOR_DOUBLE_POINT} 16 * 17 18 */ 19abstract class MetadataWikiPath extends Metadata 20{ 21 22 /** 23 * @return string - the drive from where the path should be created (ie the poth string only is stored) 24 */ 25 public abstract static function getDrive(): string; 26 27 28 /** 29 * @return string 30 * 31 * We don't extend text because the default wiki path 32 * can be an image that is not a simple path but an image 33 * in the resources 34 * 35 * We store still the image path in the store as text 36 */ 37 public static function getDataType(): string 38 { 39 return DataType::TEXT_TYPE_VALUE; 40 } 41 42 43 /** 44 * @var WikiPath 45 */ 46 protected WikiPath $value; 47 48 /** 49 * @param WikiPath|string|null $value 50 * @return Metadata 51 */ 52 public function setValue($value): Metadata 53 { 54 if ($value === null) { 55 return $this; 56 } 57 58 if ($value instanceof WikiPath) { 59 $this->value = $value; 60 return $this; 61 } 62 63 if ($value === "" || $value === ":") { 64 // form send empty string 65 // for the root `:`, non canonical 66 return $this; 67 } 68 $value = WikiPath::toValidAbsolutePath($value); 69 $this->value = WikiPath::createWikiPath($value, $this->getDrive()); 70 return $this; 71 } 72 73 74 public function toStoreValue() 75 { 76 try { 77 /** 78 * {@link self::getValue()} because 79 * it may be overwritten by derived metadata 80 */ 81 $actualPath = $this->getValue(); 82 } catch (ExceptionNotFound $e) { 83 return null; 84 } 85 /** 86 * The absolute id with the extension if not a wiki file 87 */ 88 return $actualPath->toAbsoluteId(); 89 } 90 91 public function toStoreDefaultValue() 92 { 93 try { 94 $defaultValue = $this->getDefaultValue(); 95 if (!($defaultValue instanceof WikiPath)) { 96 LogUtility::internalError("The value ($defaultValue) is not a wiki path"); 97 return $defaultValue; 98 } 99 return $defaultValue->toAbsoluteId(); 100 } catch (ExceptionNotFound $e) { 101 return null; 102 } 103 } 104 105 106 public function setFromStoreValueWithoutException($value): Metadata 107 { 108 if ($value === null || trim($value) === "") { 109 return $this; 110 } 111 112 if ($value instanceof WikiPath) { 113 $this->value = $value; 114 return $this; 115 } 116 117 $value = WikiPath::toValidAbsolutePath($value); 118 119 $drive = $this->getDrive(); 120 if ($drive === WikiPath::MARKUP_DRIVE) { 121 /** 122 * For a Markup drive, a file path should have an extension 123 * What fucked up is fucked up 124 */ 125 try { 126 $this->value = WikiPath::createMarkupPathFromPath($value); 127 } catch (ExceptionBadArgument $e) { 128 LogUtility::internalError("This is not a relative path, we should not get this error"); 129 $this->value = WikiPath::createFromPath($value, $drive); 130 } 131 } else { 132 $this->value = WikiPath::createFromPath($value, $drive); 133 } 134 return $this; 135 136 } 137 138 139 /** 140 * @return WikiPath 141 * @throws ExceptionNotFound 142 */ 143 public function getValue(): WikiPath 144 { 145 $this->buildCheck(); 146 if (isset($this->value)) { 147 return $this->value; 148 } 149 throw new ExceptionNotFound("No value found"); 150 } 151 152 public function valueIsNotNull(): bool 153 { 154 return isset($this->value); 155 } 156 157 158 public function getDefaultValue() 159 { 160 throw new ExceptionNotFound("No default value"); 161 } 162 163} 164