-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
112 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
modules/template/components/ActiveRecordDynamicAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace humhub\modules\custom_pages\modules\template\components; | ||
|
||
use humhub\components\ActiveRecord; | ||
use yii\validators\Validator; | ||
|
||
/** | ||
* Abstract ActiveRecord which allows you to use not only the regular AR attributes but also other dynamic attributes. | ||
* These are stored in a JSON field. | ||
* | ||
* @todo Avoid mark `dynAttribute` model attribute as Safe attribute | ||
*/ | ||
abstract class ActiveRecordDynamicAttributes extends ActiveRecord | ||
{ | ||
/** | ||
* Get all possible fields for this element content | ||
* | ||
* @return array Key - element index name, Value - default value | ||
*/ | ||
abstract protected function getDynamicAttributes(): array; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __get($name) | ||
{ | ||
if ($this->hasDynamicAttribute($name)) { | ||
return $this->dynAttributes[$name] ?? $this->getDynamicAttributeDefaultValue($name); | ||
} | ||
|
||
$value = parent::__get($name); | ||
|
||
if ($name === 'dynAttributes' && !is_array($value)) { | ||
$value = empty($value) ? [] : json_decode($value, true); | ||
$this->setAttribute($name, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
if ($this->hasDynamicAttribute($name)) { | ||
$fields = $this->dynAttributes; | ||
$fields[$name] = $value; | ||
$this->setAttribute('dynAttributes', $fields); | ||
} else { | ||
parent::__set($name, $value); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @noinspection PhpMissingReturnTypeInspection | ||
*/ | ||
public function beforeSave($insert) | ||
{ | ||
if (parent::beforeSave($insert)) { | ||
$this->dynAttributes = is_array($this->dynAttributes) ? json_encode($this->dynAttributes) : null; | ||
return true; | ||
} | ||
|
||
return parent::beforeSave($insert); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createValidators() | ||
{ | ||
$validators = parent::createValidators(); | ||
$validators->append(Validator::createValidator('safe', $this, 'dynAttributes')); | ||
|
||
return $validators; | ||
} | ||
|
||
private function hasDynamicAttribute(string $name): bool | ||
{ | ||
return array_key_exists($name, $this->getDynamicAttributes()); | ||
} | ||
|
||
private function getDynamicAttributeDefaultValue(string $name): mixed | ||
{ | ||
return $this->getDynamicAttributes()[$name] ?? null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.