How to store array/widget configuration to the database with config validation rules

Comparing #11 with #13

Content

[...]
Design
------
The design approach involves creating the data structure to store the serialized array configurations with tags as variables that will be replaced at runtime. The rules/validators for validating the widget configuration, will also be stored serialized in a rules column. While running the widget at runtime, the rules will be validated dynamically using the yii\base\DynamicModel.

### 1. Data Structure
Create a table `tbl_widget_config` in your database with the following minimum columns. Create a model called
`WidgetConfig` for this table through gii.

~~~
[...]
### 3. Validating Config and running widget
You can use your own custom validator to parse any tags (as called in the example above but not yet described here) or simply use a 'parseTags' function such as in the following example ... ```php // Parse tags public static function parseConfigTags($config) {
return unserialize(strtr($config, [
'{{homeUrl}}' => Yii::$app->homeUrl,
[...]
// Retrieve the configuration
public function runWidget($configId) {
$dbWidget = WidgetConfig::find
One($configId)->asArray(); $widgetConfig = \yii\base\DynamicModel::validateData( static::parseTags($dbWidget['->config']), static::parseTags($dbWidget['->rules']) ); // To run the widget call below  $class = $dbWidget['->class_name'];
 
    $class
::widget($widgetConfig->getAttributes()); } ```