Skip to content

Commit

Permalink
Add utility function to convert SugarBeans to array.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Sauvat committed Jan 14, 2016
1 parent 66f8bb4 commit c84a0a3
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

1.1.10-beta
-----
* Bean.php: Utility function to convert to array values.

1.1.9-beta
-----
* LangFileCleaner: Merge global and local version of variables.
Expand Down
85 changes: 85 additions & 0 deletions src/Bean.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class Bean
const SUGAR_CREATED = 1;
const SUGAR_UPDATED = 2;

/**
* Constants for pretty display of beans data
*/
const BOOL_TRUE = "\xE2\x9C\x94"; // Unicode 'HEAVY CHECK MARK' (U+2714)
const BOOL_FALSE = "\xE2\x9C\x95"; // Unicode 'MULTIPLICATION X' (U+2715)


/**
* Prefix that should be set by each class to identify it in logs
*
Expand Down Expand Up @@ -826,4 +833,82 @@ private function cleanMemory()

return true;
}

/**
* Return an array with the field_name and the value after replacing by a label when available.
*
* @param $module_definition Module definition fetch with language as in getModuleFields.
* @param $field_name Technical name of the field
* @param $value Value fetched from db. Enums will be replaced by label from list.
*
* @return array Array with a field_name and value.
*/
public function applyLabelsToField($module_definition, $field_name, $value)
{
$key = $field_name;
if (array_key_exists($field_name, $module_definition)) {
$field_definition = $module_definition[$field_name];
if (isset($field_definition['vname'])) {
$field_name = $field_definition['vname'];
}
if (isset($field_definition['type'])) {
switch ($field_definition['type']) {
case 'enum':
if (isset($field_definition['options_list'][$value])) {
$value = $field_definition['options_list'][$value];
}
break;
case 'bool':
$value = $value ? self::BOOL_TRUE : self::BOOL_FALSE;
break;
}
}
}
return array($field_name, $value);
}

/**
* Fetch values for fields name from bean
*
* @param $pretty if true, will return the display name from the language.
* @param $lang language to use in pretty mode. Default to en_us.
*
* @return An array of key => value pairs.
*/
public function beanToArray(array $fields_name, \SugarBean $bean, $pretty = false, $lang = 'en_us')
{
$module_definition = null;
if ($pretty) {
$module_definition = $this->getModuleFields($bean->module_name, $lang);
}
$fields = array();
foreach ($fields_name as $field_name) {
$key = $field_name;
$value = $bean->$field_name;
if (!is_null($module_definition)) {
list($key, $value) = $this->applyLabelsToField($module_definition, $key, $value);
}
$fields[$key] = $value;
}
return $fields;
}

/**
* Convert an array of \SugarBean objects to an array of arrays matching the Beans.
*
* @param $fields_name Only the fields named in this array will be present.
* @param $bean_list An array of SugarBean objects.
* @param $pretty If true, return the fields using the labels.
* @param $lang Language to use if pretty is true. Default to english.
*
* @return array An array of arrays with bean fields as keys.
*/
public function beanListToArray(array $fields_name, array $bean_list, $pretty = false, $lang = 'en_us')
{
$ret = array();
foreach ($bean_list as $bean) {
$ret[] = $this->beanToArray($fields_name, $bean, $pretty, $lang);
}
return $ret;
}
}
158 changes: 158 additions & 0 deletions tests/BeanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,162 @@ public function tearDown()
$sql = "DELETE from accounts where name='Test PHPUNIT account';";
$db->query($sql);
}

public function testApplyLabelsToField()
{
$bm = $this->getBeanManager();
$field_name = 'test';
$expected_field = 'foo';
$md = array(
$field_name => array(
'vname' => $expected_field,
'type' => 'test',
)
);

list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals('bar', $actual_value);

$md[$field_name]['type'] = 'bool';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, true);
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals("\xE2\x9C\x94", $actual_value);

list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, false);
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals("\xE2\x9C\x95", $actual_value);

$md[$field_name]['type'] = 'enum';
$md[$field_name]['options_list']['bar'] = 'Baz';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals('Baz', $actual_value);

}

public function testApplyLabelsToFieldFromSugar()
{
$bm = $this->getBeanManager();
$md = $bm->getModuleFields('Users', 'fr_FR');
$field_name = 'user_name';
$expected_field = 'Login';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals('bar', $actual_value);

$field_name = 'is_admin';
$expected_field = 'Administrateur ?';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, true);
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals("\xE2\x9C\x94", $actual_value);

list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, false);
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals("\xE2\x9C\x95", $actual_value);

$field_name = 'status';
$expected_field = 'Statut';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'Active');
$this->assertEquals($expected_field, $actual_field);
$this->assertEquals('Actif', $actual_value);

}

public function testApplyLabelsToFieldFailures()
{
$bm = $this->getBeanManager();
$field_name = 'toto';
$expected_field = 'foo';
$md = array(
'test' => array(
'name' => $expected_field,
'type' => 'test',
)
);
// Unkown field name
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($field_name, $actual_field);
$this->assertEquals('bar', $actual_value);
// No vname for field
$field_name = 'test';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($field_name, $actual_field);
$this->assertEquals('bar', $actual_value);
// No options lists
$md[$field_name]['type'] = 'enum';
list($actual_field, $actual_value) = $bm->applyLabelsToField($md, $field_name, 'bar');
$this->assertEquals($field_name, $actual_field);
$this->assertEquals('bar', $actual_value);
}

public function testBeanToArray()
{
$bm = $this->getBeanManager();
$admin = $bm->getBean('Users', '1');
$fields = array('id', 'user_name', 'status', 'is_admin');
// Test pretty off
$bean_array = $bm->beanToArray($fields, $admin);
$expected_array = array(
'id' => '1',
'user_name' => 'admin',
'status' => 'Active',
'is_admin' => '1',
);
$this->assertEquals($expected_array, $bean_array);

// Test pretty on
$bean_array = $bm->beanToArray($fields, $admin, true, 'fr_FR');
$expected_array = array(
'ID' => '1',
'Login' => 'admin',
'Statut' => 'Actif',
'Administrateur ?' => "\xE2\x9C\x94",
);
$this->assertEquals($expected_array, $bean_array);
}

public function testBeanListtoArray()
{
$bm = $this->getBeanManager();
$beans_list = array(
$bm->getBean('Users', '1'),
$bm->getBean('Users', 'seed_jim_id'),
);
$fields = array('id', 'user_name', 'status', 'is_admin');
// Test pretty off
$bean_array = $bm->beanListToArray($fields, $beans_list);
$expected_array = array(
array(
'id' => '1',
'user_name' => 'admin',
'status' => 'Active',
'is_admin' => '1',
),
array(
'id' => 'seed_jim_id',
'user_name' => 'jim',
'status' => 'Active',
'is_admin' => '0',
),
);
$this->assertEquals($expected_array, $bean_array);
// Test pretty on
$bean_array = $bm->beanListToArray($fields, $beans_list, true, 'fr_FR');
$expected_array = array(
array(
'ID' => '1',
'Login' => 'admin',
'Statut' => 'Actif',
'Administrateur ?' => "\xE2\x9C\x94",
),
array(
'ID' => 'seed_jim_id',
'Login' => 'jim',
'Statut' => 'Actif',
'Administrateur ?' => "\xE2\x9C\x95",
),
);
$this->assertEquals($expected_array, $bean_array);
}
}

0 comments on commit c84a0a3

Please sign in to comment.