1<?php 2 3 4namespace ComboStrap\Meta\Api; 5 6use ComboStrap\DataType; 7use ComboStrap\ExceptionBadArgument; 8use ComboStrap\ExceptionCompile; 9use ComboStrap\ExceptionNotFound; 10use ComboStrap\LogUtility; 11 12/** 13 * Implement all default method for a text metadata (ie small string without any paragraph such as name, title, ...) 14 * Class MetadataText 15 * @package ComboStrap 16 */ 17abstract class MetadataText extends Metadata 18{ 19 20 /** 21 * @var string|null 22 */ 23 protected $value; 24 25 26 public static function getDataType(): string 27 { 28 return DataType::TEXT_TYPE_VALUE; 29 } 30 31 public function getValue(): string 32 { 33 $this->buildCheck(); 34 if ($this->value === null) { 35 throw new ExceptionNotFound("The value was not found for the metadata ($this)"); 36 } 37 return $this->value; 38 } 39 40 public function valueIsNotNull(): bool 41 { 42 return $this->value !== null; 43 } 44 45 46 /** 47 * @param null|string $value 48 * @return $this 49 * @throws ExceptionBadArgument 50 */ 51 public function setValue($value): Metadata 52 { 53 if ($value !== null && !is_string($value)) { 54 throw new ExceptionBadArgument("The value of the metadata ($this) is not a string", $this->getCanonical()); 55 } 56 $value = trim($value); 57 if ($value === "") { 58 /** 59 * TODO: move this into the function {@link MetadataText::setFromStoreValueWithoutException()} ?? 60 * form don't return null only empty string 61 * equivalent to null 62 */ 63 return $this; 64 } 65 $possibleValues = $this->getPossibleValues(); 66 if ($possibleValues !== null) { 67 if (!in_array($value, $possibleValues)) { 68 throw new ExceptionBadArgument("The value ($value) for the metadata ({$this->getName()}) is not one of the possible following values: " . implode(", ", $possibleValues) . "."); 69 } 70 } 71 $this->value = $value; 72 return $this; 73 74 } 75 76 /** 77 * @throws ExceptionCompile 78 */ 79 public function setFromStoreValue($value): Metadata 80 { 81 return $this->setValue($value); 82 } 83 84 public function setFromStoreValueWithoutException($value): Metadata 85 { 86 if ($value === null || $value === "") { 87 $this->value = null; 88 return $this; 89 } 90 if (!is_string($value)) { 91 LogUtility::msg("This value of a text metadata is not a string. " . var_export($value, true)); 92 return $this; 93 } 94 $this->value = $value; 95 return $this; 96 } 97 98 public function getDefaultValue() 99 { 100 return null; 101 } 102 103 104} 105