Trait luya\traits\RegistryTrait

Implemented byluya\admin\models\Config, luya\cms\models\Config
Available since version1.0.0
Source Code https://github.com/luyadev/luya/blob/master/core/traits/RegistryTrait.php

Registry Trait.

The RegistryTrait helps to handle set(), get(), has() and remove() operations for a key value based storage.

Can be attached to ActiveRecords with a name and value property where name is an unique identifier.

Public Methods

Hide inherited methods

Method Description Defined By
get() Get the value of a config value. luya\traits\RegistryTrait
getNameAttribute() Determines what attribute field in the corresponding model table should be used to find the identifier key. luya\traits\RegistryTrait
getValueAttribute() Determines what attribute field in the corresponding model table should be used to store the identifier key and retrieve its data. luya\traits\RegistryTrait
has() Check whether a config value exists or not. luya\traits\RegistryTrait
remove() Remove an existing config value. luya\traits\RegistryTrait
set() Store or Update an existing/new config value. luya\traits\RegistryTrait

Protected Methods

Hide inherited methods

Method Description Defined By
clearData() Clear data array. luya\traits\RegistryTrait
getData() Loads all config data into an array. luya\traits\RegistryTrait

Method Details

Hide inherited methods

clearData() protected static method (available since version 1.3.0)

Clear data array.

protected static void clearData ( )

                protected static function clearData()
{
    self::$_data = null;
}

            
get() public static method

Get the value of a config value.

Returns the value from the registry for the given $name, if not found the defaultValue is returned.

public static mixed get ( $name, $defaultValue null )
$name string

The key to lookup.

$defaultValue mixed

The default value to return if the key does not exist.

                public static function get($name, $defaultValue = null)
{
    if (self::has($name)) {
        return self::getData()[$name];
    }

    return $defaultValue;
}

            
getData() protected static method (available since version 1.3.0)

Loads all config data into an array.

protected static array getData ( )

                protected static function getData()
{
    if (self::$_data === null) {
        self::$_data = self::find()
            ->select([self::getValueAttribute(), self::getNameAttribute()])
            ->indexBy(self::getNameAttribute())
            ->column();
    }
    return self::$_data;
}

            
getNameAttribute() public static method

Determines what attribute field in the corresponding model table should be used to find the identifier key.

public static string getNameAttribute ( )
return string

The name attribute field defaults to name.

                public static function getNameAttribute()
{
    return 'name';
}

            
getValueAttribute() public static method

Determines what attribute field in the corresponding model table should be used to store the identifier key and retrieve its data.

public static string getValueAttribute ( )
return string

The value attribute field defaults to value.

                public static function getValueAttribute()
{
    return 'value';
}

            
has() public static method

Check whether a config value exists or not.

If a value exists but is empty, has will return false.

public static boolean has ( $name )
$name string

The key to lookup. If not found false is returned.

return boolean

Whether the key exists or not.

                public static function has($name)
{
    return array_key_exists($name, self::getData());
}

            
remove() public static method

Remove an existing config value.

If the value is not found in the config, false is returned.

public static boolean remove ( $name )
$name string

The key to remove.

return boolean

If element was found and deleting was successfull true is returned, otherwise false.

                public static function remove($name)
{
    $model = self::find()->where([self::getNameAttribute() => $name])->one();

    if ($model) {
        self::clearData();
        return (bool) $model->delete();
    }

    return false;
}

            
set() public static method

Store or Update an existing/new config value.

If the config value is not found, a new record will be created.

public static boolean set ( $name, $value )
$name string

They config key

$value string

They config value. When working with array data, encode the data first with Json::encode.

return boolean

Whether saving was successfull or not.

                public static function set($name, $value)
{
    $model = self::find()->where([self::getNameAttribute() => $name])->one();
    self::clearData();
    if ($model) {
        return (bool) $model->updateAttributes([
            self::getValueAttribute() => $value,
        ]);
    }

    $model = new self();
    $model->value = $value;
    $model->name = $name;
    return $model->save();
}