diff --git a/src/Wuunder/Model/Model.php b/src/Wuunder/Model/Model.php index 06e84d7..0ec3817 100644 --- a/src/Wuunder/Model/Model.php +++ b/src/Wuunder/Model/Model.php @@ -32,7 +32,8 @@ public function __call($method, $args) return null; } - protected function setKeys($keys) { + protected function setKeys($keys) + { $formattedKeys = array(); foreach ($keys as $k => $v) { if (is_array($v)) { @@ -44,7 +45,8 @@ protected function setKeys($keys) { $this->keys = $formattedKeys; } - private function formatInnerKeys($keys) { + private function formatInnerKeys($keys) + { $formattedKeys = array(); foreach ($keys as $k => $v) { if (is_array($v)) { @@ -56,14 +58,18 @@ private function formatInnerKeys($keys) { return $formattedKeys; } - protected function importData($data) { + protected function importData($data, $keysToTranslate = array()) + { $data = json_decode($data); $validatedData = array(); foreach ($data as $key => $value) { if (array_key_exists($key, $this->keys)) { if (is_array($value)) { - $validatedData[$key] = $this->loopInnerData($value, $this->keys[$key]); + $validatedData[$key] = $this->loopInnerData($value, $this->keys[$key], $keysToTranslate); } else { + if (in_array($key, $keysToTranslate)) { + $value = $this->helper->translate($value); + } $validatedData[$key] = $value; } } else { @@ -73,15 +79,19 @@ protected function importData($data) { $this->data = $validatedData; } - private function loopInnerData($data, $keysMap) { + private function loopInnerData($data, $keysMap, $keysToTranslate) + { $validatedData = array(); foreach ($data as $key => $value) { if (array_key_exists($key, $keysMap) || is_object($value)) { if (is_object($value)) { - array_push($validatedData, $this->loopInnerData($value, $this->_isAssoc($keysMap) ? $keysMap[$key] : $keysMap[0])); + array_push($validatedData, $this->loopInnerData($value, $this->_isAssoc($keysMap) ? $keysMap[$key] : $keysMap[0], $keysToTranslate)); } elseif (is_array($value)) { - $validatedData[$key] = $this->loopInnerData($value, $keysMap[$key]); + $validatedData[$key] = $this->loopInnerData($value, $keysMap[$key], $keysToTranslate); } else { + if (in_array($key, $keysToTranslate)) { + $value = $this->helper->translate($value); + } $validatedData[$key] = $value; } } else { @@ -107,7 +117,8 @@ private function _underscore($name) return $result; } - public function jsonSerialize() { + public function jsonSerialize() + { return $this->data; } } \ No newline at end of file diff --git a/src/Wuunder/Model/ParcelshopModel.php b/src/Wuunder/Model/ParcelshopModel.php index 3ac41f3..5e1d59d 100644 --- a/src/Wuunder/Model/ParcelshopModel.php +++ b/src/Wuunder/Model/ParcelshopModel.php @@ -41,7 +41,7 @@ public function __construct($data) parent::__construct(); $this->setKeys(self::$modelStructure); - $this->importData($data); + $this->importData($data, array("weekday")); } final static function getStructure() { diff --git a/src/Wuunder/Model/ParcelshopsModel.php b/src/Wuunder/Model/ParcelshopsModel.php index 4f5c7e1..e759777 100644 --- a/src/Wuunder/Model/ParcelshopsModel.php +++ b/src/Wuunder/Model/ParcelshopsModel.php @@ -24,6 +24,6 @@ public function __construct($data) ) )); - $this->importData($data); + $this->importData($data, array("weekday")); } } \ No newline at end of file diff --git a/src/Wuunder/Util/Helper.php b/src/Wuunder/Util/Helper.php index 3693b33..268f1eb 100644 --- a/src/Wuunder/Util/Helper.php +++ b/src/Wuunder/Util/Helper.php @@ -56,6 +56,7 @@ private function __construct() */ public function setTranslationLang($lang) { + global $translationData; $translationData = array(); $file = realpath(dirname(dirname(__FILE__)) . "/etc/lang/en-" . strtolower($lang) . ".csv"); if (file_exists($file)) { @@ -73,6 +74,24 @@ public function setTranslationLang($lang) public function translate($val) { -// if + global $translationData; + + if (!in_array(strtolower($val), $translationData)) { + $translatedVal = $translationData[strtolower($val)]; + if ($this->_startsWithUpper($val)) { + $translatedVal = ucfirst($translatedVal); + } + return $translatedVal; + } + + return $val; } + + private function _startsWithUpper($str) + { + $chr = mb_substr($str, 0, 1, "UTF-8"); + return mb_strtolower($chr, "UTF-8") != $chr; + } + + } diff --git a/tests/test.php b/tests/test.php index 815d1b5..0379cbc 100644 --- a/tests/test.php +++ b/tests/test.php @@ -16,7 +16,7 @@ include("../src/Wuunder/Connector.php"); $connector = new Wuunder\Connector("YVc7rKdM6e6Q_HQK81NCt7SM0LT0TtQB"); -$connector->setLanguage("NL");exit; +$connector->setLanguage("NL"); $parcelshopRequest = $connector->getParcelshopById(); @@ -35,7 +35,7 @@ print("ParcelshopConfig not complete"); } -print("----------\r\n"); +print("----------\r\n"); exit; $parcelshopsRequest = $connector->getParcelshopsByAddress();