/** * Saves the data member into the Mongo collection */ public function save() { if (!$this->_isPersistable) { throw new ZFE_Model_Mongo_Exception("Can not save a non-persistable instance"); } $collection = static::getCollection(); $data = $this->_data; if (isset($this->_id)) { $data = array_merge(array('_id' => $this->_id), $data); } // Remove from model if value is null foreach ($data as $key => $val) { if (is_null($val)) { unset($data[$key]); } } $collection->save($data); $this->_id = $data['_id']; unset($data['_id']); $this->_data = $data; // Remove from cache after saving $class = get_called_class(); unset(self::$_cache[$class][(string) $this->getIdentifier()]); $objectId = (string) $this->_id; if (isset(static::$_refCache[$objectId])) { unset(static::$_refCache[$objectId]); } // Run on*Updated functions on changed fields and clear it foreach ($this->_changedFields as $fld => $oldValues) { $fn = ZFE_Util_String::toCamelCase("on-" . $fld . "-updated"); if (method_exists($this, $fn)) { $this->{$fn}($oldValues); } } $this->_changedFields = array(); }
/** * The magic getter. It checks for the specific getter function, * and whether the given key is a translated entry. */ public function __get($key) { // Check if a user-defined getter method exists, // and use that immediately $getter = "_" . ZFE_Util_String::toCamelCase("get_" . strtolower($key)); if (method_exists($this, $getter)) { return $this->{$getter}(); } // Check the simple cases, and return these immediately if (!isset($this->_data[$key])) { return null; } if (!in_array($key, static::$translations)) { return $this->_data[$key]; } // The key is a translatable data entry, so we try figuring out // the language to use $lang = $this->_lang; if (!isset($this->_data[$key][$lang])) { $languages = array_keys($this->_data[$key]); $lang = $languages[0]; } return $this->_data[$key][$lang]; }
public function testChopWordsPunctuation() { $str = "This is a string. It has punctuation. No ellipsis should be added after punctuation."; $expected = "This is a string. It has punctuation."; $this->assertEquals($expected, ZFE_Util_String::chopWords($str, 37)); }
public function toString($indent = null) { // Return early if no minifier has been set up if (false === $this->_minifier || !$this->_minifier->doBundle()) { return parent::toString($indent); } ZFE_Util_Stopwatch::trigger(__METHOD__); $container = $this->getContainer(); $container->ksort(); $items = $container->getArrayCopy(); // Collect CSS files $compressable = array(); foreach ($items as $key => $item) { if ($item->rel == 'stylesheet' && $item->type == 'text/css' && !$item->conditionalStylesheet) { $file = basename($item->href); if (in_array($file, $this->doNotBundle)) { continue; } if (ZFE_Util_String::startsWith($item->href, "http://")) { continue; } if (ZFE_Util_String::startsWith($item->href, "//")) { continue; } if (!isset($compressable[$item->media])) { $compressable[$item->media] = array(); } $compressable[$item->media][] = $item; unset($items[$key]); } } $container->exchangeArray($items); // Collect current data of the CSS files $hashes = array(); $mtimes = array(); foreach ($compressable as $media => $items) { $hash = ''; $_mtimes = array(); foreach ($items as $item) { $file = $_SERVER['DOCUMENT_ROOT'] . $item->href; if (Zend_Loader::isReadable($file)) { $hash .= ':' . $item->href; $_mtimes[] = filemtime($file); } } $hashes[$media] = $hash; $mtimes[$media] = $_mtimes; } // Check if the original CSS files have been updated since the // last minification foreach ($hashes as $media => $hash) { $regenerate = true; $filename = sha1($media . $hash) . ".css"; $cachedir = $this->_minifier->getCacheDir(); $path = $_SERVER['DOCUMENT_ROOT'] . $cachedir; // check directory exists. if not, create it if (!file_exists($path)) { mkdir($path, 0775, true); } if (file_exists($path . "/" . $filename)) { $mtime = filemtime($path . "/" . $filename); $regenerate = array_reduce($mtimes[$media], function ($u, $v) use($mtime) { return $u || $v > $mtime; }, false); } // If any CSS file in this media group has been updated since the last // minification, collect the content again, and store it in the cached version if ($regenerate) { $cssContent = ''; foreach ($compressable[$media] as $item) { $file = $_SERVER['DOCUMENT_ROOT'] . $item->href; if (Zend_Loader::isReadable($file)) { $cssContent .= file_get_contents($file) . PHP_EOL; } } $cssContent = $this->_minifier->minify($cssContent); file_put_contents($path . "/" . $filename, $cssContent); } $this->appendStylesheet($cachedir . "/" . $filename, $media); } ZFE_Util_Stopwatch::trigger(__METHOD__); return parent::toString($indent); }