/** * testMergeDiff method * * @return void */ public function testMergeDiff() { $first = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2')); $second = array('ModelTwo' => array('id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2')); $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first + $second); $result = Hash::mergeDiff($first, array()); $this->assertEquals($result, $first); $result = Hash::mergeDiff(array(), $first); $this->assertEquals($result, $first); $third = array('ModelOne' => array('id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3')); $result = Hash::mergeDiff($first, $third); $expected = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3')); $this->assertEquals($expected, $result); $first = array(0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')), 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))); $second = array(0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')), 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))); $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first); $third = array(0 => array('ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2'))); $result = Hash::mergeDiff($first, $third); $expected = array(0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'), 'ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')), 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))); $this->assertEquals($expected, $result); $result = Hash::mergeDiff($first, array()); $this->assertEquals($result, $first); $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first + $second); }
/** * testMergeDiff method * * @return void */ public function testMergeDiff() { $first = ['ModelOne' => ['id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2']]; $second = ['ModelTwo' => ['id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2']]; $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first + $second); $result = Hash::mergeDiff($first, []); $this->assertEquals($result, $first); $result = Hash::mergeDiff([], $first); $this->assertEquals($result, $first); $third = ['ModelOne' => ['id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3']]; $result = Hash::mergeDiff($first, $third); $expected = ['ModelOne' => ['id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3']]; $this->assertEquals($expected, $result); $first = [0 => ['ModelOne' => ['id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2']], 1 => ['ModelTwo' => ['id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2']]]; $second = [0 => ['ModelOne' => ['id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2']], 1 => ['ModelTwo' => ['id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2']]]; $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first); $third = [0 => ['ModelThree' => ['id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2']]]; $result = Hash::mergeDiff($first, $third); $expected = [0 => ['ModelOne' => ['id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'], 'ModelThree' => ['id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2']], 1 => ['ModelTwo' => ['id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2']]]; $this->assertEquals($expected, $result); $result = Hash::mergeDiff($first, []); $this->assertEquals($result, $first); $result = Hash::mergeDiff($first, $second); $this->assertEquals($result, $first + $second); }
/** * Binds the given domain to a file in the specified directory. * * @param string $domain Domain to bind * @return string Domain binded */ protected function _bindTextDomain($domain) { $this->_noLocale = true; $core = true; $merge = array(); $searchPaths = App::path('Locale'); $plugins = Plugin::loaded(); if (!empty($plugins)) { foreach ($plugins as $plugin) { $pluginDomain = Inflector::underscore($plugin); if ($pluginDomain === $domain) { $searchPaths[] = Plugin::path($plugin) . 'Locale/'; $searchPaths = array_reverse($searchPaths); break; } } } foreach ($searchPaths as $directory) { foreach ($this->l10n->languagePath as $lang) { $localeDef = $directory . $lang . DS . $this->category; if (is_file($localeDef)) { $definitions = static::loadLocaleDefinition($localeDef); if ($definitions !== false) { $this->_domains[$domain][$this->_lang][$this->category] = $definitions; $this->_noLocale = false; return $domain; } } if ($core) { $app = $directory . $lang . DS . $this->category . DS . 'core'; $translations = false; if (is_file($app . '.mo')) { $translations = static::loadMo($app . '.mo'); } if ($translations === false && is_file($app . '.po')) { $translations = static::loadPo($app . '.po'); } if ($translations !== false) { $this->_domains[$domain][$this->_lang][$this->category] = $translations; $merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category]; $this->_noLocale = false; $core = null; } } $file = $directory . $lang . DS . $this->category . DS . $domain; $translations = false; if (is_file($file . '.mo')) { $translations = static::loadMo($file . '.mo'); } if ($translations === false && is_file($file . '.po')) { $translations = static::loadPo($file . '.po'); } if ($translations !== false) { $this->_domains[$domain][$this->_lang][$this->category] = $translations; $this->_noLocale = false; break 2; } } } if (empty($this->_domains[$domain][$this->_lang][$this->category])) { $this->_domains[$domain][$this->_lang][$this->category] = array(); return $domain; } if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) { $head = $this->_domains[$domain][$this->_lang][$this->category][""]; foreach (explode("\n", $head) as $line) { $header = strtok($line, ':'); $line = trim(strtok("\n")); $this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line; } if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) { $switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]); $this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch; unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]); } $this->_domains = Hash::mergeDiff($this->_domains, $merge); if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) { unset($this->_domains[$domain][$this->_lang][$this->category][null]); } } return $domain; }