1<?php 2 3namespace plugin\struct\types; 4 5use plugin\struct\meta\ValidationException; 6 7class Url extends Text { 8 9 protected $config = array( 10 'autoscheme' => 'https', 11 'prefix' => '', 12 'postfix' => '', 13 ); 14 15 /** 16 * The final string should be an URL 17 * 18 * @param string $value 19 */ 20 public function validate($value) { 21 $url = $this->buildURL($value); 22 23 $schemes = getSchemes(); 24 $regex = '^(' . join('|', $schemes) . '):\/\/.+'; 25 if(!preg_match("/$regex/i", $url)) { 26 throw new ValidationException('Url invalid', $url); 27 } 28 } 29 30 /** 31 * @param string $value 32 * @param \Doku_Renderer $R 33 * @param string $mode 34 * @return bool 35 */ 36 public function renderValue($value, \Doku_Renderer $R, $mode) { 37 $url = $this->buildURL($value); 38 $R->externallink($url); 39 return true; 40 } 41 42 /** 43 * Creates the full URL and applies the autoscheme if needed 44 * 45 * @param string $value 46 * @return string 47 */ 48 protected function buildURL($value) { 49 $url = $this->config['prefix'] . trim($value) . $this->config['postfix']; 50 51 if(!preg_match('/\w+:\/\//', $url)) { 52 $url = $this->config['autoscheme'] . '://' . $url; 53 } 54 55 return $url; 56 } 57 58} 59