* @param int $uid
* @param array $config
*/
- public function load(int $uid, array $config)
+ public function load($uid, array $config)
{
$categories = array_keys($config);
*
* @return null|string The value of the config entry or null if not set
*/
- public function get(int $uid, string $cat, string $key = null)
+ public function get($uid, string $cat, string $key = null)
{
if (isset($this->config[$uid][$cat][$key])) {
return $this->config[$uid][$cat][$key];
*
* @return bool Set successful
*/
- public function set(int $uid, string $cat, string $key, $value)
+ public function set($uid, string $cat, string $key, $value)
{
if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
$this->config[$uid] = [];
*
* @return bool true, if deleted
*/
- public function delete(int $uid, string $cat, string $key)
+ public function delete($uid, string $cat, string $key)
{
if (isset($this->config[$uid][$cat][$key])) {
unset($this->config[$uid][$cat][$key]);
* {@inheritDoc}
*
*/
- public function load(int $uid, string $cat = 'config')
+ public function load($uid, string $cat = 'config')
{
- // If not connected, do nothing
- if (!$this->configModel->isConnected()) {
+ // If not connected or no uid, do nothing
+ if (!is_int($uid) || !$this->configModel->isConnected()) {
return;
}
/**
* {@inheritDoc}
*/
- public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
+ public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false)
{
+ if (!is_int($uid)) {
+ return $default_value;
+ }
+
// if the value isn't loaded or refresh is needed, load it to the cache
if ($this->configModel->isConnected() &&
(empty($this->db_loaded[$uid][$cat][$key]) ||
/**
* {@inheritDoc}
*/
- public function set(int $uid, string $cat, string $key, $value)
+ public function set($uid, string $cat, string $key, $value)
{
+ if (!is_int($uid)) {
+ return false;
+ }
+
// set the cache first
$cached = $this->configCache->set($uid, $cat, $key, $value);
/**
* {@inheritDoc}
*/
- public function delete(int $uid, string $cat, string $key)
+ public function delete($uid, string $cat, string $key)
{
+ if (!is_int($uid)) {
+ return false;
+ }
+
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
if (isset($this->db_loaded[$uid][$cat][$key])) {
* @see PConfigCache )
*
*/
- abstract public function load(int $uid, string $cat = 'config');
+ abstract public function load($uid, string $cat = 'config');
/**
* Get a particular user's config variable given the category name
*
* @return mixed Stored value or null if it does not exist
*/
- abstract public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
+ abstract public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false);
/**
* Sets a configuration value for a user
*
* @return bool Operation success
*/
- abstract public function set(int $uid, string $cat, string $key, $value);
+ abstract public function set($uid, string $cat, string $key, $value);
/**
* Deletes the given key from the users's configuration.
*
* @return bool
*/
- abstract public function delete(int $uid, string $cat, string $key);
+ abstract public function delete($uid, string $cat, string $key);
}
* This loads all config values everytime load is called
*
*/
- public function load(int $uid, string $cat = 'config')
+ public function load($uid, string $cat = 'config')
{
- // Don't load the whole configuration twice
- if (!empty($this->config_loaded[$uid])) {
+ // Don't load the whole configuration twice or with invalid uid
+ if (!is_int($uid) || !empty($this->config_loaded[$uid])) {
return;
}
/**
* {@inheritDoc}
*/
- public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
+ public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false)
{
+ if (!is_int($uid)) {
+ return $default_value;
+ }
+
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
} elseif ($refresh) {
/**
* {@inheritDoc}
*/
- public function set(int $uid, string $cat, string $key, $value)
+ public function set($uid, string $cat, string $key, $value)
{
+ if (!is_int($uid)) {
+ return false;
+ }
+
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
}
/**
* {@inheritDoc}
*/
- public function delete(int $uid, string $cat, string $key)
+ public function delete($uid, string $cat, string $key)
{
+ if (!is_int($uid)) {
+ return false;
+ }
+
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
}
$this->assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']);
$this->assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']);
}
+
+ /**
+ * Test when using an invalid UID
+ * @todo check it the clean way before using the config class
+ */
+ public function testInvalidUid()
+ {
+ // bad UID!
+ $uid = null;
+
+ $this->testedConfig = $this->getInstance();
+
+ $this->assertNull($this->testedConfig->get($uid, 'cat1', 'cat2'));
+ $this->assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!'));
+
+ $this->assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
+ $this->assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1'));
+ }
}