sunnysideup /
silverstripe-externalurlfield
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Sunnysideup\ExternalURLField; |
||
| 4 | |||
| 5 | use SilverStripe\Core\Config\Config; |
||
| 6 | use SilverStripe\ORM\FieldType\DBVarchar; |
||
| 7 | |||
| 8 | class ExternalURL extends DBVarchar |
||
| 9 | { |
||
| 10 | private static $casting = [ |
||
| 11 | 'Domain' => ExternalURL::class, |
||
| 12 | 'DomainShort' => 'Varchar', |
||
| 13 | 'URL' => ExternalURL::class, |
||
| 14 | 'Path' => 'Varchar', |
||
| 15 | ]; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * 2083 is the lowest common denominator when it comes to url lengths. |
||
| 19 | * however, 768 allows searching... |
||
| 20 | * |
||
| 21 | * @param null|mixed $name |
||
| 22 | * @param mixed $size |
||
| 23 | * @param mixed $options |
||
| 24 | */ |
||
| 25 | public function __construct($name = null, $size = 2083, $options = []) |
||
| 26 | { |
||
| 27 | parent::__construct($name, $size, $options); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Remove ugly parts of a url to make it nice. |
||
| 32 | */ |
||
| 33 | public function Nice(): string |
||
| 34 | { |
||
| 35 | if ($this->value) { |
||
| 36 | $parts = parse_url($this->URL()); |
||
| 37 | if ($parts) { |
||
|
0 ignored issues
–
show
|
|||
| 38 | $remove = ['scheme', 'user', 'pass', 'port', 'query', 'fragment']; |
||
| 39 | foreach ($remove as $part) { |
||
| 40 | unset($parts[$part]); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | return rtrim(http_build_url($parts), '/'); |
||
| 45 | } |
||
| 46 | |||
| 47 | return ''; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get just the domain of the url. |
||
| 52 | */ |
||
| 53 | public function Domain() |
||
| 54 | { |
||
| 55 | if ($this->value) { |
||
| 56 | return parse_url($this->URL(), PHP_URL_HOST); |
||
| 57 | } |
||
| 58 | |||
| 59 | return ''; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get just the domain of the url without the www subdomain and without https://. |
||
| 64 | */ |
||
| 65 | public function DomainShort() |
||
| 66 | { |
||
| 67 | if ($this->value) { |
||
| 68 | $parsedUrl = parse_url($this->URL()); |
||
| 69 | $host = $parsedUrl['host'] ?? ''; |
||
| 70 | // Remove 'www.' if present. |
||
| 71 | $domain = preg_replace('/^www\./', '', $host); |
||
| 72 | return $domain; |
||
| 73 | } |
||
| 74 | |||
| 75 | return ''; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Remove the www subdomain, if present. |
||
| 80 | */ |
||
| 81 | public function NoWWW() |
||
| 82 | { |
||
| 83 | //https://stackoverflow.com/questions/23349257/trim-a-full-string-instead-of-using-trim-with-characters |
||
| 84 | return $url = preg_replace('/^(www\.)*/', '', (string) $this->value); |
||
|
0 ignored issues
–
show
|
|||
| 85 | } |
||
| 86 | |||
| 87 | public function Path() |
||
| 88 | { |
||
| 89 | if ($this->value) { |
||
| 90 | return trim((string) parse_url((string) $this->URL(), PHP_URL_PATH), '/'); |
||
| 91 | } |
||
| 92 | |||
| 93 | return ''; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Scaffold the ExternalURLField for this ExternalURL. |
||
| 98 | * |
||
| 99 | * @param null|mixed $title |
||
| 100 | * @param null|mixed $params |
||
| 101 | */ |
||
| 102 | public function scaffoldFormField($title = null, $params = null) |
||
| 103 | { |
||
| 104 | $field = new ExternalURLField($this->name, $title); |
||
| 105 | $field->setMaxLength($this->getSize()); |
||
| 106 | |||
| 107 | return $field; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function forTemplate() |
||
| 111 | { |
||
| 112 | if ($this->value) { |
||
| 113 | return (string) $this->URL(); |
||
| 114 | } |
||
| 115 | |||
| 116 | return ''; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function saveInto($dataObject) |
||
| 120 | { |
||
| 121 | $url = (string) $this->value; |
||
| 122 | if ($url !== '' && $url !== '0') { |
||
| 123 | $config = Config::inst()->get(ExternalURLField::class, 'default_config'); |
||
| 124 | $defaults = $config['defaultparts']; |
||
| 125 | if (! preg_match('#^[a-zA-Z]+://#', $url)) { |
||
| 126 | $url = $defaults['scheme'] . '://' . $url; |
||
| 127 | } |
||
| 128 | |||
| 129 | $parts = parse_url($url); |
||
| 130 | if ($parts) { |
||
|
0 ignored issues
–
show
The expression
$parts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 131 | //can't parse url, abort |
||
| 132 | |||
| 133 | foreach (array_keys($parts) as $part) { |
||
| 134 | if (true === $config['removeparts'][$part]) { |
||
| 135 | unset($parts[$part]); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // this causes errors! |
||
| 140 | // $parts = array_filter($defaults, fn ($default) => ! isset($parts[$part])); |
||
| 141 | |||
| 142 | $this->value = rtrim(http_build_url($defaults, $parts), '/'); |
||
| 143 | } else { |
||
| 144 | $this->value = ''; |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | $this->value = ''; |
||
| 148 | } |
||
| 149 | parent::saveInto($dataObject); |
||
| 150 | } |
||
| 151 | } |
||
| 152 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.