@@ -138,13 +138,13 @@ discard block |
||
| 138 | 138 | // Get the namespace name |
| 139 | 139 | $namespace = $this->__default_namespace__; |
| 140 | 140 | $separatorPosition = strrpos($template, '::'); |
| 141 | - if($separatorPosition !== false) |
|
| 141 | + if ($separatorPosition !== false) |
|
| 142 | 142 | { |
| 143 | 143 | $namespace = substr($template, 0, $separatorPosition); |
| 144 | 144 | $template = substr($template, $separatorPosition + 2); |
| 145 | 145 | } |
| 146 | 146 | // Check if the namespace is defined |
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 147 | + if (!isset($this->__namespaces__[$namespace])) |
|
| 148 | 148 | { |
| 149 | 149 | return $template; |
| 150 | 150 | } |
@@ -165,13 +165,13 @@ discard block |
||
| 165 | 165 | { |
| 166 | 166 | // Get the template path |
| 167 | 167 | $templatePath = $this->__path(); |
| 168 | - if(!@is_readable($templatePath)) |
|
| 168 | + if (!@is_readable($templatePath)) |
|
| 169 | 169 | { |
| 170 | 170 | return ''; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Save the template properties. |
| 174 | - foreach($vars as $name => $value) |
|
| 174 | + foreach ($vars as $name => $value) |
|
| 175 | 175 | { |
| 176 | 176 | $this->__set((string)$name, $value); |
| 177 | 177 | } |
@@ -182,8 +182,7 @@ discard block |
||
| 182 | 182 | include $templatePath; |
| 183 | 183 | $content = ob_get_clean(); |
| 184 | 184 | |
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 185 | + return $this->__extends__ === null ? $content : // Render the extended template with the same properties. |
|
| 187 | 186 | $this->__extends__->__render($this->__properties__); |
| 188 | 187 | }; |
| 189 | 188 | |
@@ -30,54 +30,54 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Context |
| 32 | 32 | { |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * @var Context|null |
| 35 | 35 | */ |
| 36 | - private $__extends__ = null; |
|
| 36 | +private $__extends__ = null; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @var string |
| 40 | 40 | */ |
| 41 | - private $__block_name__; |
|
| 41 | +private $__block_name__; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * @var array |
| 45 | 45 | */ |
| 46 | - private $__properties__ = []; |
|
| 46 | +private $__properties__ = []; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * The constructor |
| 50 | 50 | * |
| 51 | 51 | * @param array $__namespaces__ |
| 52 | 52 | * @param string $__default_namespace__ |
| 53 | 53 | * @param string $__template__ |
| 54 | 54 | */ |
| 55 | - public function __construct(protected array $__namespaces__, |
|
| 56 | - protected string $__default_namespace__, protected string $__template__) |
|
| 57 | - {} |
|
| 55 | +public function __construct(protected array $__namespaces__, |
|
| 56 | +protected string $__default_namespace__, protected string $__template__) |
|
| 57 | +{} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @param string $name |
| 61 | 61 | * |
| 62 | 62 | * @return mixed |
| 63 | 63 | */ |
| 64 | - public function __get(string $name): mixed |
|
| 65 | - { |
|
| 66 | - return $this->__properties__[$name] ?? ''; |
|
| 67 | - } |
|
| 64 | +public function __get(string $name): mixed |
|
| 65 | +{ |
|
| 66 | +return $this->__properties__[$name] ?? ''; |
|
| 67 | +} |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * @param string $name |
| 71 | 71 | * @param mixed $value |
| 72 | 72 | * |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - public function __set(string $name, $value): void |
|
| 76 | - { |
|
| 77 | - $this->__properties__[$name] = $value; |
|
| 78 | - } |
|
| 75 | +public function __set(string $name, $value): void |
|
| 76 | +{ |
|
| 77 | +$this->__properties__[$name] = $value; |
|
| 78 | +} |
|
| 79 | 79 | |
| 80 | - /** |
|
| 80 | +/** |
|
| 81 | 81 | * Include a template |
| 82 | 82 | * |
| 83 | 83 | * @param string $template The name of template to be rendered |
@@ -85,110 +85,110 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return void |
| 87 | 87 | */ |
| 88 | - protected function include(string $template, array $vars = []): void |
|
| 89 | - { |
|
| 90 | - $context = new Context($this->__namespaces__, |
|
| 91 | - $this->__default_namespace__, $template); |
|
| 92 | - echo $context->__render($vars); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 88 | +protected function include(string $template, array $vars = []): void |
|
| 89 | +{ |
|
| 90 | +$context = new Context($this->__namespaces__, |
|
| 91 | +$this->__default_namespace__, $template); |
|
| 92 | +echo $context->__render($vars); |
|
| 93 | +} |
|
| 94 | + |
|
| 95 | +/** |
|
| 96 | 96 | * @param string $template The name of template to be rendered |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function extends(string $template): void |
|
| 101 | - { |
|
| 102 | - $this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | - $this->__default_namespace__, $template); |
|
| 104 | - } |
|
| 100 | +public function extends(string $template): void |
|
| 101 | +{ |
|
| 102 | +$this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | +$this->__default_namespace__, $template); |
|
| 104 | +} |
|
| 105 | 105 | |
| 106 | - /** |
|
| 106 | +/** |
|
| 107 | 107 | * Start a new block |
| 108 | 108 | * |
| 109 | 109 | * @param string $name |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - public function block(string $name): void |
|
| 114 | - { |
|
| 115 | - ob_start(); |
|
| 116 | - $this->__block_name__ = $name; |
|
| 117 | - } |
|
| 113 | +public function block(string $name): void |
|
| 114 | +{ |
|
| 115 | +ob_start(); |
|
| 116 | +$this->__block_name__ = $name; |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * End the current block |
| 121 | 121 | * |
| 122 | 122 | * @param Closure|null $filter |
| 123 | 123 | * |
| 124 | 124 | * @return void |
| 125 | 125 | */ |
| 126 | - public function endblock(?Closure $filter = null): void |
|
| 127 | - { |
|
| 128 | - $content = ob_get_clean(); |
|
| 129 | - $this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | - } |
|
| 126 | +public function endblock(?Closure $filter = null): void |
|
| 127 | +{ |
|
| 128 | +$content = ob_get_clean(); |
|
| 129 | +$this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | +} |
|
| 131 | 131 | |
| 132 | - /** |
|
| 132 | +/** |
|
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - private function __path(): string |
|
| 136 | - { |
|
| 137 | - $template = trim($this->__template__); |
|
| 138 | - // Get the namespace name |
|
| 139 | - $namespace = $this->__default_namespace__; |
|
| 140 | - $separatorPosition = strrpos($template, '::'); |
|
| 141 | - if($separatorPosition !== false) |
|
| 142 | - { |
|
| 143 | - $namespace = substr($template, 0, $separatorPosition); |
|
| 144 | - $template = substr($template, $separatorPosition + 2); |
|
| 145 | - } |
|
| 146 | - // Check if the namespace is defined |
|
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | - { |
|
| 149 | - return $template; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $namespace = $this->__namespaces__[$namespace]; |
|
| 153 | - // Get the template path |
|
| 154 | - return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 135 | +private function __path(): string |
|
| 136 | +{ |
|
| 137 | +$template = trim($this->__template__); |
|
| 138 | +// Get the namespace name |
|
| 139 | +$namespace = $this->__default_namespace__; |
|
| 140 | +$separatorPosition = strrpos($template, '::'); |
|
| 141 | +if($separatorPosition !== false) |
|
| 142 | +{ |
|
| 143 | +$namespace = substr($template, 0, $separatorPosition); |
|
| 144 | +$template = substr($template, $separatorPosition + 2); |
|
| 145 | +} |
|
| 146 | +// Check if the namespace is defined |
|
| 147 | +if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | +{ |
|
| 149 | +return $template; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +$namespace = $this->__namespaces__[$namespace]; |
|
| 153 | +// Get the template path |
|
| 154 | +return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | +} |
|
| 156 | + |
|
| 157 | +/** |
|
| 158 | 158 | * Render a template |
| 159 | 159 | * |
| 160 | 160 | * @param array $vars The template vars |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - public function __render(array $vars): string |
|
| 165 | - { |
|
| 166 | - // Get the template path |
|
| 167 | - $templatePath = $this->__path(); |
|
| 168 | - if(!@is_readable($templatePath)) |
|
| 169 | - { |
|
| 170 | - return ''; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Save the template properties. |
|
| 174 | - foreach($vars as $name => $value) |
|
| 175 | - { |
|
| 176 | - $this->__set((string)$name, $value); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Render the template |
|
| 180 | - $renderer = function() use($templatePath) { |
|
| 181 | - ob_start(); |
|
| 182 | - include $templatePath; |
|
| 183 | - $content = ob_get_clean(); |
|
| 184 | - |
|
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 187 | - $this->__extends__->__render($this->__properties__); |
|
| 188 | - }; |
|
| 189 | - |
|
| 190 | - // Call the closure in the context of this object. |
|
| 191 | - // So the keyword '$this' in the template will refer to this object. |
|
| 192 | - return call_user_func($renderer->bindTo($this)); |
|
| 193 | - } |
|
| 164 | +public function __render(array $vars): string |
|
| 165 | +{ |
|
| 166 | +// Get the template path |
|
| 167 | +$templatePath = $this->__path(); |
|
| 168 | +if(!@is_readable($templatePath)) |
|
| 169 | +{ |
|
| 170 | +return ''; |
|
| 171 | +} |
|
| 172 | + |
|
| 173 | +// Save the template properties. |
|
| 174 | +foreach($vars as $name => $value) |
|
| 175 | +{ |
|
| 176 | +$this->__set((string)$name, $value); |
|
| 177 | +} |
|
| 178 | + |
|
| 179 | +// Render the template |
|
| 180 | +$renderer = function() use($templatePath) { |
|
| 181 | +ob_start(); |
|
| 182 | +include $templatePath; |
|
| 183 | +$content = ob_get_clean(); |
|
| 184 | + |
|
| 185 | +return $this->__extends__ === null ? $content : |
|
| 186 | + // Render the extended template with the same properties. |
|
| 187 | + $this->__extends__->__render($this->__properties__); |
|
| 188 | +}; |
|
| 189 | + |
|
| 190 | +// Call the closure in the context of this object. |
|
| 191 | +// So the keyword '$this' in the template will refer to this object. |
|
| 192 | +return call_user_func($renderer->bindTo($this)); |
|
| 193 | +} |
|
| 194 | 194 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function setLocale(string $sLocale): void |
| 58 | 58 | { |
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 59 | + if (($sLocale = trim($sLocale))) |
|
| 60 | 60 | { |
| 61 | 61 | $this->sDefaultLocale = $sLocale; |
| 62 | 62 | } |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
| 75 | 75 | { |
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 76 | + foreach ($aTranslations as $sKey => $xTranslation) |
|
| 77 | 77 | { |
| 78 | 78 | $sKey = trim($sKey); |
| 79 | 79 | $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
| 80 | - if(is_array($xTranslation)) |
|
| 80 | + if (is_array($xTranslation)) |
|
| 81 | 81 | { |
| 82 | 82 | // Recursively read the translations in the array |
| 83 | 83 | $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
@@ -100,18 +100,18 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function loadTranslations(string $sFilePath, string $sLanguage): bool |
| 102 | 102 | { |
| 103 | - if(!file_exists($sFilePath)) |
|
| 103 | + if (!file_exists($sFilePath)) |
|
| 104 | 104 | { |
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | $aTranslations = require($sFilePath); |
| 108 | - if(!is_array($aTranslations)) |
|
| 108 | + if (!is_array($aTranslations)) |
|
| 109 | 109 | { |
| 110 | 110 | return false; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | // Load the translations |
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 114 | + if (!isset($this->aTranslations[$sLanguage])) |
|
| 115 | 115 | { |
| 116 | 116 | $this->aTranslations[$sLanguage] = []; |
| 117 | 117 | } |
@@ -133,16 +133,16 @@ discard block |
||
| 133 | 133 | public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
| 134 | 134 | { |
| 135 | 135 | $sText = trim($sText); |
| 136 | - if(empty($sLanguage)) |
|
| 136 | + if (empty($sLanguage)) |
|
| 137 | 137 | { |
| 138 | 138 | $sLanguage = $this->sDefaultLocale; |
| 139 | 139 | } |
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 140 | + if (!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | 141 | { |
| 142 | 142 | return $sText; |
| 143 | 143 | } |
| 144 | 144 | $sMessage = $this->aTranslations[$sLanguage][$sText]; |
| 145 | - if(!empty($aPlaceHolders)) |
|
| 145 | + if (!empty($aPlaceHolders)) |
|
| 146 | 146 | { |
| 147 | 147 | $aVars = array_map(function($sVar) { |
| 148 | 148 | return ':' . $sVar; |
@@ -162,16 +162,16 @@ discard block |
||
| 162 | 162 | */ |
| 163 | 163 | public function translations(string $sKey, string $sLanguage = ''): array |
| 164 | 164 | { |
| 165 | - if(empty($sLanguage)) |
|
| 165 | + if (empty($sLanguage)) |
|
| 166 | 166 | { |
| 167 | 167 | $sLanguage = $this->sDefaultLocale; |
| 168 | 168 | } |
| 169 | 169 | $aKeys = explode('.', $sKey); |
| 170 | 170 | |
| 171 | 171 | $aTranslations = $this->aRawTranslations[$sLanguage]; |
| 172 | - foreach($aKeys as $sKey) |
|
| 172 | + foreach ($aKeys as $sKey) |
|
| 173 | 173 | { |
| 174 | - if($sKey !== '') |
|
| 174 | + if ($sKey !== '') |
|
| 175 | 175 | { |
| 176 | 176 | $aTranslations = $aTranslations[$sKey] ?? []; |
| 177 | 177 | } |
@@ -81,8 +81,7 @@ |
||
| 81 | 81 | { |
| 82 | 82 | // Recursively read the translations in the array |
| 83 | 83 | $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
| 84 | - } |
|
| 85 | - else |
|
| 84 | + } else |
|
| 86 | 85 | { |
| 87 | 86 | // Save this translation |
| 88 | 87 | $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
@@ -26,43 +26,43 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class Translator |
| 28 | 28 | { |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The default locale |
| 31 | 31 | * |
| 32 | 32 | * @var string |
| 33 | 33 | */ |
| 34 | - protected $sDefaultLocale = 'en'; |
|
| 34 | +protected $sDefaultLocale = 'en'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * The translations in a flattened array |
| 38 | 38 | * |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - protected $aTranslations = []; |
|
| 41 | +protected $aTranslations = []; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * The translations as received as input |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $aRawTranslations = []; |
|
| 48 | +protected $aRawTranslations = []; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Set the default locale |
| 52 | 52 | * |
| 53 | 53 | * @param string $sLocale |
| 54 | 54 | * |
| 55 | 55 | * @return void |
| 56 | 56 | */ |
| 57 | - public function setLocale(string $sLocale): void |
|
| 58 | - { |
|
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 60 | - { |
|
| 61 | - $this->sDefaultLocale = $sLocale; |
|
| 62 | - } |
|
| 63 | - } |
|
| 57 | +public function setLocale(string $sLocale): void |
|
| 58 | +{ |
|
| 59 | +if(($sLocale = trim($sLocale))) |
|
| 60 | +{ |
|
| 61 | +$this->sDefaultLocale = $sLocale; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Recursively load translated strings from an array |
| 67 | 67 | * |
| 68 | 68 | * @param string $sLanguage The language of the translations |
@@ -71,26 +71,26 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return void |
| 73 | 73 | */ |
| 74 | - private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | - { |
|
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | - { |
|
| 78 | - $sKey = trim($sKey); |
|
| 79 | - $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | - if(is_array($xTranslation)) |
|
| 81 | - { |
|
| 82 | - // Recursively read the translations in the array |
|
| 83 | - $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | - } |
|
| 85 | - else |
|
| 86 | - { |
|
| 87 | - // Save this translation |
|
| 88 | - $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 74 | +private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | +{ |
|
| 76 | +foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | +{ |
|
| 78 | +$sKey = trim($sKey); |
|
| 79 | +$sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | +if(is_array($xTranslation)) |
|
| 81 | +{ |
|
| 82 | + // Recursively read the translations in the array |
|
| 83 | + $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | +} |
|
| 85 | +else |
|
| 86 | +{ |
|
| 87 | + // Save this translation |
|
| 88 | + $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | +} |
|
| 90 | +} |
|
| 91 | +} |
|
| 92 | 92 | |
| 93 | - /** |
|
| 93 | +/** |
|
| 94 | 94 | * Load translated strings from a file |
| 95 | 95 | * |
| 96 | 96 | * @param string $sFilePath The file full path |
@@ -98,30 +98,30 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | - { |
|
| 103 | - if(!file_exists($sFilePath)) |
|
| 104 | - { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - $aTranslations = require($sFilePath); |
|
| 108 | - if(!is_array($aTranslations)) |
|
| 109 | - { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 101 | +public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | +{ |
|
| 103 | +if(!file_exists($sFilePath)) |
|
| 104 | +{ |
|
| 105 | +return false; |
|
| 106 | +} |
|
| 107 | +$aTranslations = require($sFilePath); |
|
| 108 | +if(!is_array($aTranslations)) |
|
| 109 | +{ |
|
| 110 | +return false; |
|
| 111 | +} |
|
| 112 | 112 | |
| 113 | - // Load the translations |
|
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | - { |
|
| 116 | - $this->aTranslations[$sLanguage] = []; |
|
| 117 | - } |
|
| 118 | - $this->aRawTranslations[$sLanguage] = |
|
| 119 | - array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | - $this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 113 | +// Load the translations |
|
| 114 | +if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | +{ |
|
| 116 | +$this->aTranslations[$sLanguage] = []; |
|
| 117 | +} |
|
| 118 | +$this->aRawTranslations[$sLanguage] = |
|
| 119 | +array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | +$this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | +return true; |
|
| 122 | +} |
|
| 123 | 123 | |
| 124 | - /** |
|
| 124 | +/** |
|
| 125 | 125 | * Get a translated string |
| 126 | 126 | * |
| 127 | 127 | * @param string $sText The key of the translated string |
@@ -130,29 +130,29 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | - { |
|
| 135 | - $sText = trim($sText); |
|
| 136 | - if(empty($sLanguage)) |
|
| 137 | - { |
|
| 138 | - $sLanguage = $this->sDefaultLocale; |
|
| 139 | - } |
|
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | - { |
|
| 142 | - return $sText; |
|
| 143 | - } |
|
| 144 | - $sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | - if(!empty($aPlaceHolders)) |
|
| 146 | - { |
|
| 147 | - $aVars = array_map(function($sVar) { |
|
| 148 | - return ':' . $sVar; |
|
| 149 | - }, array_keys($aPlaceHolders)); |
|
| 150 | - $sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | - } |
|
| 152 | - return $sMessage; |
|
| 153 | - } |
|
| 133 | +public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | +{ |
|
| 135 | +$sText = trim($sText); |
|
| 136 | +if(empty($sLanguage)) |
|
| 137 | +{ |
|
| 138 | +$sLanguage = $this->sDefaultLocale; |
|
| 139 | +} |
|
| 140 | +if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | +{ |
|
| 142 | +return $sText; |
|
| 143 | +} |
|
| 144 | +$sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | +if(!empty($aPlaceHolders)) |
|
| 146 | +{ |
|
| 147 | +$aVars = array_map(function($sVar) { |
|
| 148 | + return ':' . $sVar; |
|
| 149 | +}, array_keys($aPlaceHolders)); |
|
| 150 | +$sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | +} |
|
| 152 | +return $sMessage; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Get all the translations under a given key |
| 157 | 157 | * |
| 158 | 158 | * @param string $sKey |
@@ -160,22 +160,22 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @return array |
| 162 | 162 | */ |
| 163 | - public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | - { |
|
| 165 | - if(empty($sLanguage)) |
|
| 166 | - { |
|
| 167 | - $sLanguage = $this->sDefaultLocale; |
|
| 168 | - } |
|
| 169 | - $aKeys = explode('.', $sKey); |
|
| 163 | +public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | +{ |
|
| 165 | +if(empty($sLanguage)) |
|
| 166 | +{ |
|
| 167 | +$sLanguage = $this->sDefaultLocale; |
|
| 168 | +} |
|
| 169 | +$aKeys = explode('.', $sKey); |
|
| 170 | 170 | |
| 171 | - $aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | - foreach($aKeys as $sKey) |
|
| 173 | - { |
|
| 174 | - if($sKey !== '') |
|
| 175 | - { |
|
| 176 | - $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - return $aTranslations; |
|
| 180 | - } |
|
| 171 | +$aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | +foreach($aKeys as $sKey) |
|
| 173 | +{ |
|
| 174 | +if($sKey !== '') |
|
| 175 | +{ |
|
| 176 | + $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | +} |
|
| 178 | +} |
|
| 179 | +return $aTranslations; |
|
| 180 | +} |
|
| 181 | 181 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | { |
| 38 | 38 | return file_get_contents($sPath); |
| 39 | 39 | } |
| 40 | - catch(Exception $e) |
|
| 40 | + catch (Exception $e) |
|
| 41 | 41 | { |
| 42 | 42 | return false; |
| 43 | 43 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | { |
| 57 | 57 | return Minifier::minify($sCode); |
| 58 | 58 | } |
| 59 | - catch(Exception $e) |
|
| 59 | + catch (Exception $e) |
|
| 60 | 60 | { |
| 61 | 61 | return false; |
| 62 | 62 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public function minify(string $sJsFile, string $sMinFile): bool |
| 74 | 74 | { |
| 75 | - if(($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 75 | + if (($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 76 | 76 | ($sMinCode = $this->minifyCode($sJsCode)) === false) |
| 77 | 77 | { |
| 78 | 78 | return false; |
@@ -36,8 +36,7 @@ discard block |
||
| 36 | 36 | try |
| 37 | 37 | { |
| 38 | 38 | return file_get_contents($sPath); |
| 39 | - } |
|
| 40 | - catch(Exception $e) |
|
| 39 | + } catch(Exception $e) |
|
| 41 | 40 | { |
| 42 | 41 | return false; |
| 43 | 42 | } |
@@ -55,8 +54,7 @@ discard block |
||
| 55 | 54 | try |
| 56 | 55 | { |
| 57 | 56 | return Minifier::minify($sCode); |
| 58 | - } |
|
| 59 | - catch(Exception $e) |
|
| 57 | + } catch(Exception $e) |
|
| 60 | 58 | { |
| 61 | 59 | return false; |
| 62 | 60 | } |
@@ -24,45 +24,45 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | class FileMinifier |
| 26 | 26 | { |
| 27 | - /** |
|
| 27 | +/** |
|
| 28 | 28 | * Read the file content |
| 29 | 29 | * |
| 30 | 30 | * @param string $sPath |
| 31 | 31 | * |
| 32 | 32 | * @return string|false |
| 33 | 33 | */ |
| 34 | - private function readFile(string $sPath): bool|string |
|
| 35 | - { |
|
| 36 | - try |
|
| 37 | - { |
|
| 38 | - return file_get_contents($sPath); |
|
| 39 | - } |
|
| 40 | - catch(Exception $e) |
|
| 41 | - { |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - } |
|
| 34 | +private function readFile(string $sPath): bool|string |
|
| 35 | +{ |
|
| 36 | +try |
|
| 37 | +{ |
|
| 38 | +return file_get_contents($sPath); |
|
| 39 | +} |
|
| 40 | +catch(Exception $e) |
|
| 41 | +{ |
|
| 42 | +return false; |
|
| 43 | +} |
|
| 44 | +} |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * Read the file content |
| 48 | 48 | * |
| 49 | 49 | * @param string $sCode |
| 50 | 50 | * |
| 51 | 51 | * @return string|false |
| 52 | 52 | */ |
| 53 | - private function minifyCode(string $sCode): bool|string |
|
| 54 | - { |
|
| 55 | - try |
|
| 56 | - { |
|
| 57 | - return Minifier::minify($sCode); |
|
| 58 | - } |
|
| 59 | - catch(Exception $e) |
|
| 60 | - { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - } |
|
| 53 | +private function minifyCode(string $sCode): bool|string |
|
| 54 | +{ |
|
| 55 | +try |
|
| 56 | +{ |
|
| 57 | +return Minifier::minify($sCode); |
|
| 58 | +} |
|
| 59 | +catch(Exception $e) |
|
| 60 | +{ |
|
| 61 | +return false; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Minify javascript code |
| 67 | 67 | * |
| 68 | 68 | * @param string $sJsFile The javascript file to be minified |
@@ -70,14 +70,14 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return bool |
| 72 | 72 | */ |
| 73 | - public function minify(string $sJsFile, string $sMinFile): bool |
|
| 74 | - { |
|
| 75 | - if(($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 76 | - ($sMinCode = $this->minifyCode($sJsCode)) === false) |
|
| 77 | - { |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - file_put_contents($sMinFile, $sMinCode); |
|
| 81 | - return is_file($sMinFile); |
|
| 82 | - } |
|
| 73 | +public function minify(string $sJsFile, string $sMinFile): bool |
|
| 74 | +{ |
|
| 75 | +if(($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 76 | +($sMinCode = $this->minifyCode($sJsCode)) === false) |
|
| 77 | +{ |
|
| 78 | +return false; |
|
| 79 | +} |
|
| 80 | +file_put_contents($sMinFile, $sMinCode); |
|
| 81 | +return is_file($sMinFile); |
|
| 82 | +} |
|
| 83 | 83 | } |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | |
| 36 | 36 | protected function setUp(): void |
| 37 | 37 | { |
| 38 | - $this->sConfigDir = __DIR__ . '/../config'; |
|
| 38 | + $this->sConfigDir = __DIR__ . '/../config'; |
|
| 39 | 39 | $this->xConfigSetter = new ConfigSetter(); |
| 40 | 40 | $this->xConfigReader = new ConfigReader($this->xConfigSetter); |
| 41 | 41 | $this->xConfig = $this->xConfigSetter->newConfig(['core' => ['language' => 'en']]); |
@@ -90,11 +90,11 @@ |
||
| 90 | 90 | $sKey = trim($sKey, ' .'); |
| 91 | 91 | $aKeys = Value::explodeName($sKey); |
| 92 | 92 | $aValues = $this->aValues; |
| 93 | - foreach($aKeys as $_sKey) |
|
| 93 | + foreach ($aKeys as $_sKey) |
|
| 94 | 94 | { |
| 95 | 95 | $aValues = $aValues[$_sKey] ?? []; |
| 96 | 96 | } |
| 97 | - if(!Value::containsOptions($aValues)) |
|
| 97 | + if (!Value::containsOptions($aValues)) |
|
| 98 | 98 | { |
| 99 | 99 | return []; |
| 100 | 100 | } |
@@ -34,13 +34,13 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | public static function containsOptions($xValue): bool |
| 36 | 36 | { |
| 37 | - if(!is_array($xValue) || count($xValue) === 0) |
|
| 37 | + if (!is_array($xValue) || count($xValue) === 0) |
|
| 38 | 38 | { |
| 39 | 39 | return false; |
| 40 | 40 | } |
| 41 | - foreach(array_keys($xValue) as $xKey) |
|
| 41 | + foreach (array_keys($xValue) as $xKey) |
|
| 42 | 42 | { |
| 43 | - if(!is_string($xKey)) |
|
| 43 | + if (!is_string($xKey)) |
|
| 44 | 44 | { |
| 45 | 45 | return false; |
| 46 | 46 | } |
@@ -35,13 +35,13 @@ |
||
| 35 | 35 | public static function read(string $sConfigFile): array |
| 36 | 36 | { |
| 37 | 37 | $sConfigFile = realpath($sConfigFile); |
| 38 | - if(!is_readable($sConfigFile)) |
|
| 38 | + if (!is_readable($sConfigFile)) |
|
| 39 | 39 | { |
| 40 | 40 | throw new FileAccess($sConfigFile); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | $aConfigOptions = include($sConfigFile); |
| 44 | - if(!is_array($aConfigOptions)) |
|
| 44 | + if (!is_array($aConfigOptions)) |
|
| 45 | 45 | { |
| 46 | 46 | throw new FileContent($sConfigFile); |
| 47 | 47 | } |
@@ -38,19 +38,19 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | public static function read(string $sConfigFile): array |
| 40 | 40 | { |
| 41 | - if(!extension_loaded('yaml')) |
|
| 41 | + if (!extension_loaded('yaml')) |
|
| 42 | 42 | { |
| 43 | 43 | throw new YamlExtension(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | $sConfigFile = realpath($sConfigFile); |
| 47 | - if(!is_readable($sConfigFile)) |
|
| 47 | + if (!is_readable($sConfigFile)) |
|
| 48 | 48 | { |
| 49 | 49 | throw new FileAccess($sConfigFile); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | $aConfigOptions = yaml_parse_file($sConfigFile); |
| 53 | - if(!is_array($aConfigOptions)) |
|
| 53 | + if (!is_array($aConfigOptions)) |
|
| 54 | 54 | { |
| 55 | 55 | throw new FileContent($sConfigFile); |
| 56 | 56 | } |
@@ -37,14 +37,14 @@ |
||
| 37 | 37 | public static function read(string $sConfigFile): array |
| 38 | 38 | { |
| 39 | 39 | $sConfigFile = realpath($sConfigFile); |
| 40 | - if(!is_readable($sConfigFile)) |
|
| 40 | + if (!is_readable($sConfigFile)) |
|
| 41 | 41 | { |
| 42 | 42 | throw new FileAccess($sConfigFile); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $sFileContent = file_get_contents($sConfigFile); |
| 46 | 46 | $aConfigOptions = json_decode($sFileContent, true); |
| 47 | - if(!is_array($aConfigOptions)) |
|
| 47 | + if (!is_array($aConfigOptions)) |
|
| 48 | 48 | { |
| 49 | 49 | throw new FileContent($sConfigFile); |
| 50 | 50 | } |