Example:
$key = 'User_Friends/' . $user->username();
return \Jyxo\Spl\ObjectCache::get($key) ?: \Jyxo\Spl\ObjectCache::set($key, new \User\Friends($this->context, $user));
/** * Static filtering. * * @param string $method Filter name * @param array $params Parameters; the first value gets filtered, the rest will be used as constructor parameters * @return mixed */ public static function __callStatic(string $method, array $params) { $factory = \Jyxo\Spl\ObjectCache::get(\Jyxo\Input\Factory::class) ?: \Jyxo\Spl\ObjectCache::set(\Jyxo\Input\Factory::class, new Factory()); $value = array_shift($params); $key = 'Jyxo\\Input\\Filter\\' . ucfirst($method) . ($params ? '/' . serialize($params) : ''); $filter = \Jyxo\Spl\ObjectCache::get($key) ?: \Jyxo\Spl\ObjectCache::set($key, $factory->getFilterByName($method, $params)); /* @var $filter \Jyxo\Input\FilterInterface */ return $filter->filter($value); }
/** * Static validation. * * @param string $method Validator name * @param array $params Parameters; the first value gets validated, the rest will be used as constructor parameters * @return boolean */ public static function __callStatic(string $method, array $params) { try { $factory = \Jyxo\Spl\ObjectCache::get(\Jyxo\Input\Factory::class) ?: \Jyxo\Spl\ObjectCache::set(\Jyxo\Input\Factory::class, new Factory()); $value = array_shift($params); $key = 'Jyxo\\Input\\Validator\\' . ucfirst($method) . ($params ? '/' . serialize($params) : ''); $validator = \Jyxo\Spl\ObjectCache::get($key) ?: \Jyxo\Spl\ObjectCache::set($key, $factory->getValidatorByName($method, $params)); } catch (\Exception $e) { $validator = $factory->getValidatorByName($method, $params); } return $validator->isValid($value); }
/** * Tests unset(). */ public function testUnset() { // Nothing is saved $this->assertFalse(isset($this->cache->{self::CACHE_KEY})); // Save and check $this->saveObject(); $this->assertTrue(isset($this->cache->{self::CACHE_KEY})); // Unset the object and check unset($this->cache->{self::CACHE_KEY}); $this->assertFalse(isset($this->cache->{self::CACHE_KEY})); // Save and check again $this->saveObject(); $this->assertTrue(isset($this->cache->{self::CACHE_KEY})); $this->cache->clear(); }
/** * Tests static call usage. */ public function testCallStatic() { static $value = 42; $this->assertTrue(Validator::isInt($value)); $this->assertTrue(Validator::lessThan($value, $value * 2)); $this->assertTrue(Validator::callback($value, function ($value, $lowerBound) { return is_numeric($value) && $value > $lowerBound; }, 41)); // Tests storing in cache - the first on in cached, the second one isn't // because it had additional parameters that had been added to the cache ID $this->assertNotNull(\Jyxo\Spl\ObjectCache::get('\\Jyxo\\Input\\Validator\\IsInt')); $this->assertNull(\Jyxo\Spl\ObjectCache::get('\\Jyxo\\Input\\Validator\\LessThan')); }