Last active
November 7, 2017 15:56
-
-
Save timkelty/f10a7505fbc8d2f2557c to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Craft; | |
class Recurly_PlanModel extends BaseModel | |
{ | |
protected function defineAttributes() { | |
return array( | |
'sortOrder' => AttributeType::SortOrder, | |
'code' => AttributeType::String, | |
'max' => AttributeType::Number, | |
'min' => AttributeType::Number, | |
'baseEndpoints' => AttributeType::Number, | |
'endpointCode' => AttributeType::String, | |
'annual' => array('type' => AttributeType::Bool, 'default' => false), | |
'baseCost' => array('type' => AttributeType::Number, 'decimals' => 2), | |
'endpointCost' => array('type' => AttributeType::Number, 'decimals' => 2), | |
); | |
} | |
public function setAttribute($name, $value) | |
{ | |
parent::setAttribute($name, $value); | |
if (in_array($name, $this->attributeNames())) | |
{ | |
$attributes = $this->getAttributeConfigs(); | |
$config = $attributes[$name]; | |
// Handle special case attribute types | |
switch ($config['type']) | |
{ | |
case AttributeType::Bool: | |
{ | |
if ($value) | |
{ | |
$value = (bool) $value; | |
} | |
break; | |
} | |
case AttributeType::Number: | |
{ | |
if ($value) | |
{ | |
$value = floatval(number_format($value, $config['decimals'])); | |
} | |
break; | |
} | |
} | |
$this->$name = $value; | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} |
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
<?php | |
namespace Craft; | |
class Recurly_PlanRecord extends BaseRecord | |
{ | |
public function getTableName() | |
{ | |
return 'recurly_plans'; | |
} | |
protected function defineAttributes() | |
{ | |
return array( | |
'sortOrder' => AttributeType::SortOrder, | |
'code' => AttributeType::String, | |
'max' => AttributeType::Number, | |
'min' => AttributeType::Number, | |
'baseEndpoints' => AttributeType::Number, | |
'endpointCode' => AttributeType::String, | |
'annual' => array('type' => AttributeType::Bool, 'default' => false), | |
'baseCost' => array('type' => AttributeType::Number, 'decimals' => 2), | |
'endpointCost' => array('type' => AttributeType::Number, 'decimals' => 2), | |
); | |
} | |
public function prepAttributesForUse() | |
{ | |
parent::prepAttributesForUse(); | |
$attributes = $this->getAttributeConfigs(); | |
$attributes['dateUpdated'] = array('type' => AttributeType::DateTime, 'column' => ColumnType::DateTime, 'required' => true); | |
$attributes['dateCreated'] = array('type' => AttributeType::DateTime, 'column' => ColumnType::DateTime, 'required' => true); | |
foreach ($attributes as $name => $config) | |
{ | |
$value = $this->getAttribute($name); | |
switch ($config['type']) | |
{ | |
case AttributeType::Bool: | |
{ | |
if ($value) | |
{ | |
$this->setAttribute($name, (bool) $value); | |
} | |
break; | |
} | |
case AttributeType::Number: | |
{ | |
if ($value) | |
{ | |
$value = floatval(number_format($value, $config['decimals'])); | |
$this->setAttribute($name, $value); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment