Extracts some logic into resuable methods
This commit is contained in:
@ -148,4 +148,15 @@ class CustomField extends Model {
|
|||||||
|
|
||||||
return $custom_field->save();
|
return $custom_field->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function extractCustomFieldsFromFromObject($data) {
|
||||||
|
$custom_fields = array();
|
||||||
|
foreach($data as $key => $value) {
|
||||||
|
if(strpos($key, 'cf_') === 0) {
|
||||||
|
$custom_fields[(int)substr($key, 3)] = $value;
|
||||||
|
unset($data[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array($data, $custom_fields);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -491,27 +491,11 @@ class Subscriber extends Model {
|
|||||||
unset($data['segments']);
|
unset($data['segments']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($subscriber === false) {
|
// set required fields' default values
|
||||||
// fields that must exist
|
$data = self::setRequiredFieldsDefaultValues($data);
|
||||||
$not_null_fields = array(
|
|
||||||
'first_name' => '',
|
|
||||||
'last_name' => ''
|
|
||||||
);
|
|
||||||
foreach($not_null_fields as $field => $value) {
|
|
||||||
if(!isset($data[$field])) {
|
|
||||||
$data[$field] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// custom fields
|
// get custom fields
|
||||||
$custom_fields = array();
|
list($data, $custom_fields) = CustomField::extractCustomFieldsFromFromObject($data);
|
||||||
foreach($data as $key => $value) {
|
|
||||||
if(strpos($key, 'cf_') === 0) {
|
|
||||||
$custom_fields[(int)substr($key, 3)] = $value;
|
|
||||||
unset($data[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wipe any unconfirmed data at this point
|
// wipe any unconfirmed data at this point
|
||||||
$data['unconfirmed_data'] = null;
|
$data['unconfirmed_data'] = null;
|
||||||
@ -884,4 +868,17 @@ class Subscriber extends Model {
|
|||||||
}, $subscribers)
|
}, $subscribers)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function setRequiredFieldsDefaultValues($data) {
|
||||||
|
$required_field_default_values = array(
|
||||||
|
'first_name' => '',
|
||||||
|
'last_name' => ''
|
||||||
|
);
|
||||||
|
foreach($required_field_default_values as $field => $value) {
|
||||||
|
if(!isset($data[$field])) {
|
||||||
|
$data[$field] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
@ -131,6 +131,30 @@ class CustomFieldTest extends MailPoetTest {
|
|||||||
expect($subscriber->value)->equals($association->value);
|
expect($subscriber->value)->equals($association->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItExtractsCustomFieldsFromObject() {
|
||||||
|
$data = array(
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'cf_1' => 'Paris',
|
||||||
|
'first_name' => 'John',
|
||||||
|
'cf_2' => 'France',
|
||||||
|
'last_name' => 'Doe'
|
||||||
|
);
|
||||||
|
list($data, $custom_values) = CustomField::extractCustomFieldsFromFromObject($data);
|
||||||
|
expect($data)->equals(
|
||||||
|
array(
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'first_name' => 'John',
|
||||||
|
'last_name' => 'Doe'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect($custom_values)->equals(
|
||||||
|
array(
|
||||||
|
'1' => 'Paris',
|
||||||
|
'2' => 'France'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
CustomField::deleteMany();
|
CustomField::deleteMany();
|
||||||
Subscriber::deleteMany();
|
Subscriber::deleteMany();
|
||||||
|
@ -903,6 +903,16 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItSetsDefaultValuesForRequiredFields() {
|
||||||
|
// MySQL running in strict mode requires a value to be set for certain fields
|
||||||
|
expect(Subscriber::setRequiredFieldsDefaultValues(array()))->equals(
|
||||||
|
array(
|
||||||
|
'first_name' => '',
|
||||||
|
'last_name' => ''
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
|
Reference in New Issue
Block a user