/** * SPL Countable function * Called automatically when attribute is used in a 'count()' function call * Caches results when there are no query changes * * @return int * @todo - this is broken */ public function count() { return count($this->execute()); $that = $this; // New scope with closure to get only PUBLIC properties of object instance (can't include cache property) $cacheParams = function () use($that) { // This trick doesn't seem to work by itself in PHP 5.4... $props = get_object_vars($that); // Depends on protected/private properties starting with underscore ('_') $publics = array_filter(array_keys($props), function ($key) { return strpos($key, '_') !== 0; }); return array_intersect_key($props, array_flip($publics)); }; $cacheKey = sha1(var_export($cacheParams(), true)) . "_count"; $cacheResult = isset($this->cache[$cacheKey]) ? $this->cache[$cacheKey] : false; // Check cache if ($cacheResult) { $result = $cacheResult; } else { // Execute query $result = $this->mapper->getDi()->get($this->mapper->getAdapterName())->count($this); // Set cache $this->cache[$cacheKey] = $result; } return is_numeric($result) ? $result : 0; }