1<?php 2 3 4namespace ComboStrap\Meta\Field; 5 6 7use ComboStrap\ExceptionCompile; 8use ComboStrap\MarkupPath; 9use ComboStrap\Meta\Api\Metadata; 10use ComboStrap\Meta\Api\MetadataText; 11use ComboStrap\MetaManagerForm; 12use ComboStrap\Site; 13use ComboStrap\StringUtility; 14 15class Region extends MetadataText 16{ 17 18 19 public const PROPERTY_NAME = "region"; 20 public const OLD_REGION_PROPERTY = "country"; 21 public const CONF_SITE_LANGUAGE_REGION = "siteLanguageRegion"; 22 23 public static function createForPage(MarkupPath $page) 24 { 25 return (new Region()) 26 ->setResource($page); 27 } 28 29 static public function getTab(): ?string 30 { 31 return MetaManagerForm::TAB_LANGUAGE_VALUE; 32 } 33 34 /** 35 * @throws ExceptionCompile 36 */ 37 public function setFromStoreValue($value): Metadata 38 { 39 40 $this->validityCheck($value); 41 return parent::setFromStoreValue($value); 42 43 } 44 45 /** 46 * @param string|null $value 47 * @return Metadata 48 * @throws ExceptionCompile 49 */ 50 public function setValue($value): Metadata 51 { 52 $this->validityCheck($value); 53 return parent::setValue($value); 54 } 55 56 57 static public function getDescription(): string 58 { 59 return "The region of the language"; 60 } 61 62 static public function getLabel(): string 63 { 64 return "Region"; 65 } 66 67 static public function getName(): string 68 { 69 return self::PROPERTY_NAME; 70 } 71 72 static public function getPersistenceType(): string 73 { 74 return Metadata::PERSISTENT_METADATA; 75 } 76 77 static public function isMutable(): bool 78 { 79 return true; 80 } 81 82 public function getDefaultValue() 83 { 84 return Site::getLanguageRegion(); 85 } 86 87 /** 88 * @throws ExceptionCompile 89 */ 90 private function validityCheck($value) 91 { 92 if ($value === "" || $value === null) { 93 return; 94 } 95 if (!StringUtility::match($value, "^[a-zA-Z]{2}$")) { 96 throw new ExceptionCompile("The region value ($value) for the page ({$this->getResource()}) does not have two letters (ISO 3166 alpha-2 region code)", $this->getCanonical()); 97 } 98 } 99 100 static public function getCanonical(): string 101 { 102 return "region"; 103 } 104 105 106 static public function isOnForm(): bool 107 { 108 return true; 109 } 110 111} 112