예제 #1
0
    public function testDecrementItemsReturnsEmptyArrayIfNonWritable()
    {
        $this->_storage->setItem('key', 10);
        $this->_options->setWritable(false);

        $this->assertSame(array(), $this->_storage->decrementItems(array('key' => 5)));
        $this->assertEquals(10, $this->_storage->getItem('key'));
    }
예제 #2
0
    /**
     * Internal function for adding translation data
     *
     * This may be a new language or additional data for an existing language
     * If the options 'clear' is true, then the translation data for the specified
     * language is replaced and added otherwise
     *
     * @see    Zend_Locale
     * @param  array|Traversable $options Translation data to add
     * @throws \Zend\Translator\Exception\InvalidArgumentException
     * @return \Zend\Translator\Adapter\AbstractAdapter Provides fluent interface
     */
    private function _addTranslationData($options = array())
    {
        if ($options instanceof Traversable) {
            $options = ArrayUtils::iteratorToArray($options);
        } else if (func_num_args() > 1) {
            $args = func_get_args();
            $options['content'] = array_shift($args);

            if (!empty($args)) {
                $options['locale'] = array_shift($args);
            }

            if (!empty($args)) {
                $options += array_shift($args);
            }
        }

        if (($options['content'] instanceof Translator\Translator) || ($options['content'] instanceof AbstractAdapter)) {
            $options['usetranslateadapter'] = true;
            $content = $options['content'];
            if (empty($options['locale']) || ($options['locale'] == 'auto')) {
                $locales = $content->getList();
            } else {
                $locales = array(1 => $options['locale']);
            }

            foreach ($locales as $locale) {
                $options['locale']  = $locale;
                $options['content'] = $content->getMessages($locale);
                $this->_addTranslationData($options);
            }

            return $this;
        }

        try {
            $options['locale'] = Locale\Locale::findLocale($options['locale']);
        } catch (Locale\Exception\ExceptionInterface $e) {
            throw new Exception\InvalidArgumentException("The given Language '{$options['locale']}' does not exist", 0, $e);
        }

        if ($options['clear'] || !isset($this->_translate[$options['locale']])) {
            $this->_translate[$options['locale']] = array();
        }

        $read = true;
        if (isset(self::$_cache)) {
            $id = 'Zend_Translator_' . md5(serialize($options['content'])) . '_' . $this->toString();
            $temp = self::$_cache->getItem($id);
            if ($temp) {
                $read = false;
            }
        }

        if ($options['reload']) {
            $read = true;
        }

        if ($read) {
            if (!empty($options['usetranslateadapter'])) {
                $temp = array($options['locale'] => $options['content']);
            } else {
                $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options);
            }
        }

        if (empty($temp)) {
            $temp = array();
        }

        $keys = array_keys($temp);
        foreach($keys as $key) {
            if (!isset($this->_translate[$key])) {
                $this->_translate[$key] = array();
            }

            if (array_key_exists($key, $temp) && is_array($temp[$key])) {
                $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
            }
        }

        if ($this->_automatic === true) {
            $find    = new Locale\Locale($options['locale']);
            $browser = $find->getEnvironment() + $find->getBrowser();
            arsort($browser);
            foreach($browser as $language => $quality) {
                if (isset($this->_translate[$language])) {
                    $this->_options['locale'] = $language;
                    break;
                }
            }
        }

        if (($read) and (isset(self::$_cache))) {
            $id = 'Zend_Translator_' . md5(serialize($options['content'])) . '_' . $this->toString();
            $this->saveCache($temp, $id);
        }

        return $this;
    }
예제 #3
0
 /**
  * Returns the page item cache.
  *
  * @return array
  */
 public function getPageItemCache()
 {
     $data = array();
     if ($this->_cacheEnabled()) {
         $cacheIds = self::$_cache->find(CacheAdapter::MATCH_TAGS_OR, array(
             'tags' => array($this->_getCacheInternalId()),
         ));
         $cacheIds = array();
         while (($item = self::$_cache->fetch()) !== false) {
             $cacheIds[] = $item['key'];
         }
         foreach ($cacheIds as $id) {
             if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) {
                 $data[$page[1]] = self::$_cache->getItem($this->_getCacheId($page[1]));
             }
         }
     }
     return $data;
 }