/** * Tests saving data. */ public function testSaveData() { $object = $this->saveObject(); $this->assertSame($object, $this->cache->get(self::CACHE_KEY)); // Saving using one way ObjectCache::set(self::CACHE_KEY, $object); $this->assertSame($object, $this->cache->get(self::CACHE_KEY)); // Saving using the other way $this->cache->{self::CACHE_KEY} = $object; $this->assertSame($object, $this->cache->get(self::CACHE_KEY)); $this->cache->clear(); }
/** * 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 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')); }