Added encode

This commit is contained in:
Daniel Maya 2019-06-10 13:04:52 +02:00
parent d14ad39785
commit 78e01d9c7d
1 changed files with 464 additions and 7 deletions

View File

@ -240,7 +240,7 @@ class Item extends CachedModel
private static function extractX(array $data): int
{
return static::parseIntOr(
static::issetInArray($data, ['x', 'pos_x']),
static::issetInArray($data, ['x', 'pos_x', 'posX']),
0
);
}
@ -256,7 +256,7 @@ class Item extends CachedModel
private static function extractY(array $data): int
{
return static::parseIntOr(
static::issetInArray($data, ['y', 'pos_y']),
static::issetInArray($data, ['y', 'pos_y', 'posY']),
0
);
}
@ -272,7 +272,7 @@ class Item extends CachedModel
private static function extractAclGroupId(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['id_group', 'aclGroupId']),
static::issetInArray($data, ['id_group', 'aclGroupId', 'idGroup']),
null
);
}
@ -288,7 +288,7 @@ class Item extends CachedModel
private static function extractParentId(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['parentId', 'parent_item']),
static::issetInArray($data, ['parentId', 'parent_item', 'parentItem']),
null
);
}
@ -304,7 +304,7 @@ class Item extends CachedModel
private static function extractIsOnTop(array $data): bool
{
return static::parseBool(
static::issetInArray($data, ['isOnTop', 'show_on_top'])
static::issetInArray($data, ['isOnTop', 'show_on_top', 'showOnTop'])
);
}
@ -319,7 +319,7 @@ class Item extends CachedModel
private static function extractIsLinkEnabled(array $data): bool
{
return static::parseBool(
static::issetInArray($data, ['isLinkEnabled', 'enable_link'])
static::issetInArray($data, ['isLinkEnabled', 'enable_link', 'enableLink'])
);
}
@ -376,7 +376,23 @@ class Item extends CachedModel
private static function extractAgentId(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['agentId', 'id_agent', 'id_agente']),
static::issetInArray($data, ['agentId', 'id_agent', 'id_agente', 'idAgent', 'idAgente']),
null
);
}
/**
* Extract a custom id graph value.
*
* @param array $data Unknown input data structure.
*
* @return integer Valid identifier of an agent.
*/
private static function extractIdCustomGraph(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['id_custom_graph', 'idCustomGraph', 'customGraphId']),
null
);
}
@ -398,6 +414,9 @@ class Item extends CachedModel
'moduleId',
'id_agente_modulo',
'id_modulo',
'idModulo',
'idAgenteModulo',
'idAgentModule',
]
),
null
@ -1187,4 +1206,442 @@ class Item extends CachedModel
}
/**
* Return a valid representation of a record in database.
*
* @param array $data Input data.
*
* @return array Data structure representing a record in database.
*/
protected function encode(array $data): array
{
$result = [];
$id = static::extractId($data);
if ($id) {
$result['id'] = $id;
}
$id_layout = static::extractIdLayout($data);
if ($id_layout) {
$result['id_layout'] = $id_layout;
}
$pos_x = static::parseIntOr(
static::issetInArray($data, ['x', 'pos_x', 'posX']),
null
);
if ($pos_x !== null) {
$result['pos_x'] = $pos_x;
}
$pos_y = static::parseIntOr(
static::issetInArray($data, ['y', 'pos_y', 'posY']),
null
);
if ($pos_y !== null) {
$result['pos_y'] = $pos_y;
}
$height = static::extractHeight($data);
if ($height !== null) {
$result['height'] = $height;
}
$width = static::extractWidth($data);
if ($width !== null) {
$result['width'] = $width;
}
$label = static::extractLabel($data);
if ($label !== null) {
$result['label'] = $label;
}
$image = static::extractImageSrc($data);
if ($image !== null) {
$result['image'] = $image;
}
$type = static::parseIntOr(
static::issetInArray($data, ['type']),
null
);
if ($type !== null) {
$result['type'] = $type;
}
$period = static::parseIntOr(
static::issetInArray($data, ['period']),
null
);
if ($period !== null) {
$result['period'] = $period;
}
$id_agente_modulo = static::extractModuleId($data);
if ($id_agente_modulo !== null) {
$result['id_agente_modulo'] = $id_agente_modulo;
}
$id_agent = static::extractAgentId($data);
if ($id_agent !== null) {
$result['id_agent'] = $id_agent;
}
$id_layout_linked = static::parseIntOr(
static::issetInArray($data, ['linkedLayoutId', 'id_layout_linked', 'idLayoutLinked']),
null
);
if ($id_layout_linked !== null) {
$result['id_layout_linked'] = $id_layout_linked;
}
$parent_item = static::extractParentId($data);
if ($parent_item !== null) {
$result['parent_item'] = $parent_item;
}
$enable_link = static::issetInArray($data, ['isLinkEnabled', 'enable_link', 'enableLink']);
if ($enable_link !== null) {
$result['enable_link'] = static::parseBool($enable_link);
}
$id_metaconsole = static::extractMetaconsoleId($data);
if ($id_metaconsole !== null) {
$result['id_metaconsole'] = $id_metaconsole;
}
$id_group = static::extractAclGroupId($data);
if ($id_group !== null) {
$result['id_group'] = $id_group;
}
$id_custom_graph = static::extractIdCustomGraph($data);
if ($id_custom_graph !== null) {
$result['id_custom_graph'] = $id_custom_graph;
}
$border_width = static::extractBorderWidth($data);
if ($border_width !== null) {
$result['border_width'] = $border_width;
}
$type_graph = static::extractTypeGraph($data);
if ($type_graph !== null) {
$result['type_graph'] = $type_graph;
}
$label_position = static::notEmptyStringOr(
static::issetInArray($data, ['labelPosition', 'label_position']),
null
);
if ($label_position !== null) {
$result['label_position'] = $label_position;
}
$border_color = static::extractBorderColor($data);
if ($border_color !== null) {
$result['border_color'] = $border_color;
}
$fill_color = static::extractFillColor($data);
if ($fill_color !== null) {
$result['fill_color'] = $fill_color;
}
$show_statistics = static::issetInArray($data, ['showStatistics', 'show_statistics']);
if ($show_statistics !== null) {
$result['show_statistics'] = static::parseBool($show_statistics);
}
$linked_layout_node_id = static::parseIntOr(
static::issetInArray(
$data,
[
'linkedLayoutAgentId',
'linked_layout_node_id',
]
),
null
);
if ($linked_layout_node_id !== null) {
$result['linked_layout_node_id'] = $linked_layout_node_id;
}
$linked_layout_status_type = static::notEmptyStringOr(
static::issetInArray($data, ['linkedLayoutStatusType', 'linked_layout_status_type']),
null
);
if ($linked_layout_status_type !== null) {
$result['linked_layout_status_type'] = $linked_layout_status_type;
}
$id_layout_linked_weight = static::parseIntOr(
static::issetInArray($data, ['linkedLayoutStatusTypeWeight', 'id_layout_linked_weight']),
null
);
if ($id_layout_linked_weight !== null) {
$result['id_layout_linked_weight'] = $id_layout_linked_weight;
}
$linked_layout_status_as_service_warning = static::parseIntOr(
static::issetInArray(
$data,
[
'linkedLayoutStatusTypeWarningThreshold',
'linked_layout_status_as_service_warning',
]
),
null
);
if ($linked_layout_status_as_service_warning !== null) {
$result['linked_layout_status_as_service_warning'] = $linked_layout_status_as_service_warning;
}
$linked_layout_status_as_service_critical = static::parseIntOr(
static::issetInArray(
$data,
[
'linkedLayoutStatusTypeCriticalThreshold',
'linked_layout_status_as_service_critical',
]
),
null
);
if ($linked_layout_status_as_service_critical !== null) {
$result['linked_layout_status_as_service_critical'] = $linked_layout_status_as_service_critical;
}
$element_group = static::parseIntOr(
static::issetInArray($data, ['elementGroup', 'element_group']),
null
);
if ($element_group !== null) {
$result['element_group'] = $element_group;
}
$show_on_top = static::issetInArray($data, ['isOnTop', 'show_on_top', 'showOnTop']);
if ($show_on_top !== null) {
$result['show_on_top'] = static::parseBool($show_on_top);
}
$clock_animation = static::notEmptyStringOr(
static::issetInArray($data, ['clockType', 'clock_animation', 'clockAnimation']),
null
);
if ($clock_animation !== null) {
$result['clock_animation'] = $clock_animation;
}
$time_format = static::notEmptyStringOr(
static::issetInArray($data, ['clockFormat', 'time_format', 'timeFormat']),
null
);
if ($time_format !== null) {
$result['time_format'] = $time_format;
}
$timezone = static::notEmptyStringOr(
static::issetInArray($data, ['timezone', 'timeZone', 'time_zone']),
null
);
if ($timezone !== null) {
$result['timezone'] = $timezone;
}
$show_last_value = static::parseIntOr(
static::issetInArray($data, ['show_last_value', 'showLastValue']),
null
);
if ($show_last_value !== null) {
$result['show_last_value'] = $show_last_value;
}
$cache_expiration = static::parseIntOr(
static::issetInArray($data, ['cache_expiration', 'cacheExpiration']),
null
);
if ($cache_expiration !== null) {
$result['cache_expiration'] = $cache_expiration;
}
return $result;
}
/**
* Extract item id.
*
* @param array $data Unknown input data structure.
*
* @return integer Item id. 0 by default.
*/
private static function extractId(array $data): integer
{
return static::parseIntOr(
static::issetInArray($data, ['id', 'itemId']),
0
);
}
/**
* Extract layout id.
*
* @param array $data Unknown input data structure.
*
* @return integer Item id. 0 by default.
*/
private static function extractIdLayout(array $data): integer
{
return static::parseIntOr(
static::issetInArray($data, ['id_layout', 'idLayout', 'layoutId']),
0
);
}
/**
* Extract item width.
*
* @param array $data Unknown input data structure.
*
* @return integer Item width. 0 by default.
*/
private static function extractWidth(array $data): integer
{
return static::parseIntOr(
static::issetInArray($data, ['width']),
null
);
}
/**
* Extract item height.
*
* @param array $data Unknown input data structure.
*
* @return integer Item height. 0 by default.
*/
private static function extractHeight(array $data): integer
{
return static::parseIntOr(
static::issetInArray($data, ['height']),
null
);
}
/**
* Extract a image src value.
*
* @param array $data Unknown input data structure.
*
* @return mixed String representing the image url (not empty) or null.
*/
private static function extractImageSrc(array $data): string
{
$imageSrc = static::notEmptyStringOr(
static::issetInArray($data, ['imageSrc', 'image']),
null
);
return $imageSrc;
}
/**
* Extract a border width value.
*
* @param array $data Unknown input data structure.
*
* @return integer Valid border width.
*/
private static function extractBorderWidth(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['border_width', 'borderWidth']),
null
);
}
/**
* Extract a type graph value.
*
* @param array $data Unknown input data structure.
*
* @return string One of 'vertical' or 'horizontal'. 'vertical' by default.
*/
private static function extractTypeGraph(array $data): string
{
return static::notEmptyStringOr(
static::issetInArray($data, ['typeGraph', 'type_graph']),
null
);
}
/**
* Extract a border color value.
*
* @param array $data Unknown input data structure.
*
* @return mixed String representing the border color (not empty) or null.
*/
private function extractBorderColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['borderColor', 'border_color']),
null
);
}
/**
* Extract a fill color value.
*
* @param array $data Unknown input data structure.
*
* @return mixed String representing the fill color (not empty) or null.
*/
private function extractFillColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['fillColor', 'fill_color']),
null
);
}
/**
* .
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*/
public function save(array $data=[]): boolean
{
$mergedData = \array_merge(static::data, $data);
$save = static::encode($mergedData);
if (empty($save['id'])) {
// Insert.
$result = db_process_sql_insert('tlayout_data', $save);
} else {
// Update.
$result = db_process_sql_update('tlayout_data', $save, ['id' => $save['id']]);
}
if ($result) {
}
return $result;
}
}