| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace common\services\mall;
- use Yii;
- use common\interfaces\AdsSettingInterface;
- use Exception;
- /**
- * 插件设置Service
- * @package common\services
- */
- abstract class BaseAdsSettingService extends BaseService implements AdsSettingInterface
- {
- /**
- * @var int
- */
- public $mall_id;
- /**
- * @var array
- */
- protected $hiddens = [];
- /**
- * @var array
- */
- protected $shows = [];
- /**
- * @var boolean
- */
- protected $isSet = false;
- /**
- * @return void
- * @throws Exception
- */
- private function _before()
- {
- if (!isset($this->mall_id)) {
- throw new Exception('未设置Mall Id');
- }
- }
- /**
- * 获取设置前处理
- *
- * @param array $setting
- * @return array
- */
- public function beforeGetSetting(array $setting): array
- {
- return $setting;
- }
- /**
- * 存储设置前处理
- *
- * @param array $setting
- * @return array
- */
- public function beforeSetSetting(array $setting): array
- {
- return $setting;
- }
- /**
- * 获取设置
- *
- * @return array
- */
- public function getSetting(string $name = 'basic')
- {
- $this->_before();
- $this->isSet = false;
- $setting = Yii::$app->services->setting->addons->get($this->mall_id, $this->getAddosName(), $name);
- if ($this->hiddens) return $this->hiddenArrayKeys($setting);
- if ($this->shows) return $this->showArrayKeys($setting);
- return $this->getMergeSetting($setting);
- }
- /**
- * 设置配置
- *
- * @param array $setting
- * @param string $name
- * @return boolean
- */
- public function setSetting(array $setting, string $name = 'basic')
- {
- $this->_before();
- $this->isSet = true;
- return Yii::$app->services->setting->addons->set(
- $this->mall_id,
- $this->getAddosName(),
- [$name => $this->getMergeSetting($setting)]
- );
- }
- /**
- * 合并设置
- *
- * @param array $setting
- * @return array
- */
- protected function getMergeSetting(array $setting)
- {
- !$this->isSet && $setting = $this->beforeGetSetting($setting);
- $this->isSet && $setting = $this->beforeSetSetting($setting);
- return array_merge($this->defaultSetting(), $setting);
- }
- /**
- * 隐藏部分配置文件不返回
- *
- * @param array $hidden_keys
- * @return self
- */
- public function hiddenSetting(array $hidden_keys)
- {
- $this->shows = [];
- $this->hiddens = array_values($hidden_keys);
- return $this;
- }
- /**
- * 只显示部分配置文件
- *
- * @param array $show_keys
- * @return self
- */
- public function showSetting(array $show_keys)
- {
- $this->hiddens = [];
- $this->shows = array_values($show_keys);
- return $this;
- }
- /**
- * @param array $setting
- * @return array
- */
- protected function hiddenArrayKeys(array $setting)
- {
- return array_diff_key($this->getMergeSetting($setting), array_flip($this->hiddens));
- }
- /**
- * @param array $setting
- * @return array
- */
- protected function showArrayKeys(array $setting)
- {
- return array_intersect_key($this->getMergeSetting($setting), array_flip($this->shows));
- }
- }
|