Example #1
0
 /**
  * Restore old attribute value
  *
  * @param \Magento\Framework\Object $object
  * @param mixed $oldAttrValue
  * @return void
  */
 protected function _restoreOldAttrValue($object, $oldAttrValue)
 {
     $attrCode = $this->getAttribute();
     if (is_null($oldAttrValue)) {
         $object->unsetData($attrCode);
     } else {
         $object->setData($attrCode, $oldAttrValue);
     }
 }
Example #2
0
 /**
  * Try to unserialize the attribute value
  *
  * @param \Magento\Framework\Object $object
  * @return $this
  */
 protected function _unserialize(\Magento\Framework\Object $object)
 {
     $attrCode = $this->getAttribute()->getAttributeCode();
     if ($object->getData($attrCode)) {
         try {
             $unserialized = unserialize($object->getData($attrCode));
             $object->setData($attrCode, $unserialized);
         } catch (\Exception $e) {
             $object->unsetData($attrCode);
         }
     }
     return $this;
 }
Example #3
0
 /**
  * Tests \Magento\Framework\Object->unsetData()
  */
 public function testUnsetData()
 {
     $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 3, 'key4' => 4];
     $this->_object->setData($data);
     $this->_object->unsetData('key1');
     unset($data['key1']);
     $this->assertEquals($data, $this->_object->getData());
     $this->_object->unsetData(['key2', 'key3']);
     unset($data['key2']);
     unset($data['key3']);
     $this->assertEquals($data, $this->_object->getData());
     $this->_object->unsetData();
     $this->assertEquals([], $this->_object->getData());
 }
 /**
  * Serialize specified field in an object
  *
  * @param \Magento\Framework\Object $object
  * @param string $field
  * @param mixed $defaultValue
  * @param bool $unsetEmpty
  * @return $this
  */
 protected function _serializeField(\Magento\Framework\Object $object, $field, $defaultValue = null, $unsetEmpty = false)
 {
     $value = $object->getData($field);
     if (empty($value)) {
         if ($unsetEmpty) {
             $object->unsetData($field);
         } else {
             if (is_object($defaultValue) || is_array($defaultValue)) {
                 $defaultValue = serialize($defaultValue);
             }
             $object->setData($field, $defaultValue);
         }
     } elseif (is_array($value) || is_object($value)) {
         $object->setData($field, serialize($value));
     }
     return $this;
 }