_encryptor = $encryptor; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * Magic method called during class serialization * * @return string[] */ public function __sleep() { $properties = parent::__sleep(); return array_diff($properties, ['_encryptor']); } /** * Magic method called during class un-serialization * * @return void */ public function __wakeup() { parent::__wakeup(); $this->_encryptor = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Encryption\EncryptorInterface::class ); } /** * Decrypt value after loading * * @return void */ protected function _afterLoad() { $value = (string)$this->getValue(); if (!empty($value) && ($decrypted = $this->_encryptor->decrypt($value))) { $this->setValue($decrypted); } } /** * Encrypt value before saving * * @return void */ public function beforeSave() { $this->_dataSaveAllowed = false; $value = (string)$this->getValue(); // don't save value, if an obscured value was received. This indicates that data was not changed. if (!preg_match('/^\*+$/', $value) && !empty($value)) { $this->_dataSaveAllowed = true; $encrypted = $this->_encryptor->encrypt($value); $this->setValue($encrypted); } elseif (empty($value)) { $this->_dataSaveAllowed = true; } } /** * Process config value * * @param string $value * @return string */ public function processValue($value) { return $this->_encryptor->decrypt($value); } }