Created
February 22, 2013 08:00
-
-
Save lukeholder/5011641 to your computer and use it in GitHub Desktop.
working
This file contains hidden or 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
<?php | |
namespace BlocksCommerce\Models; | |
use Blocks\AttributeType; | |
use Blocks\Blocks; | |
class PaymentMethodModel extends BaseModel | |
{ | |
public function __toString() | |
{ | |
return $this->name; | |
} | |
public function defineAttributes() | |
{ | |
return array( | |
'id' => AttributeType::Number, | |
'name' => AttributeType::String, | |
'class' => AttributeType::String, | |
'enabled' => AttributeType::Bool, | |
'config' => array(AttributeType::String, 'column' => ColumnType::Text) | |
); | |
} | |
public function beforeSave($event) | |
{ | |
$this->serialize('config'); | |
parent::beforeSave(); | |
} | |
public function afterSave($event) | |
{ | |
$this->deserialize('config'); | |
parent::afterSave(); | |
} | |
public function afterFind($event) | |
{ | |
$this->deserialize('config'); | |
parent::afterFind(); | |
} | |
private function deserialize($attribute) | |
{ | |
$att = $this->getAttribute($attribute); | |
if(!empty($att) && is_scalar($att)) { | |
$a = @$this->jsonToArray($att); | |
if($a !== false) { | |
$this->setAttribute($attribute, $a); | |
} else { | |
$this->setAttribute($attribute, $this->arrayToJson(array())); | |
} | |
} | |
} | |
private function serialize($attribute) | |
{ | |
$att = $this->getAttribute($attribute); | |
if(is_array($att)){ | |
$this->setAttribute($attribute, $this->jsonToArray($att)); | |
}else{ | |
$this->setAttribute($attribute, $this->arrayToJson(array())); | |
} | |
} | |
private function jsonToArray($json){ | |
return json_decode($json,true); | |
} | |
private function arrayToJson($array){ | |
return json_encode($array); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment