Skip to content

Commit

Permalink
Use some match expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
flack committed May 6, 2024
1 parent 4622a61 commit 2874e43
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 227 deletions.
22 changes: 5 additions & 17 deletions lib/midcom/config/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,11 @@ private function print_section(string $heading, array $messages)

foreach ($messages as $testname => $data) {
echo " <tr class=\"test\">\n <th>\n";
switch ($data['result']) {
case self::OK:
echo " <i class='fa fa-check' style='color: green;' title='OK'></i>";
break;

case self::WARNING:
echo " <i class='fa fa-exclamation-triangle' style='color: orange;' title='WARNING'></i>";
break;

case self::ERROR:
echo " <i class='fa fa-exclamation-circle' style='color: red;' title='ERROR'></i>";
break;

default:
throw new midcom_error("Unknown error code {$data['result']}.");
}

echo match ($data['result']) {
self::OK => " <i class='fa fa-check' style='color: green;' title='OK'></i>",
self::WARNING => " <i class='fa fa-exclamation-triangle' style='color: orange;' title='WARNING'></i>",
self::ERROR => " <i class='fa fa-exclamation-circle' style='color: red;' title='ERROR'></i>"
};
echo " {$testname}</th>\n";
echo " <td>{$data['message']}</td>\n";
echo " </tr>\n";
Expand Down
29 changes: 11 additions & 18 deletions lib/midcom/helper/reflector/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,23 @@ private static function get_icon(string $object_class, string $object_baseclass,
self::$_cache[$mode . '_map'] ??= self::_get_icon_map($mode . '_magic', $mode === 'create_type' ? 'file-o' : 'file');
$map = self::$_cache[$mode . '_map'];

switch (true) {
case (isset($map[$object_class])):
return $map[$object_class];
return match (true) {
isset($map[$object_class]) => $map[$object_class],

case (isset($map[$object_baseclass])):
return $map[$object_baseclass];
isset($map[$object_baseclass]) => $map[$object_baseclass],

case (str_contains($object_class, 'person')):
return $mode === 'create_type' ? 'user-o' : 'user';
str_contains($object_class, 'person') => $mode === 'create_type' ? 'user-o' : 'user',

case (str_contains($object_class, 'event')):
return 'calendar-o';
str_contains($object_class, 'event') => 'calendar-o',

case (str_contains($object_class, 'member')):
case (str_contains($object_class, 'organization')):
case (str_contains($object_class, 'group')):
return 'users';
str_contains($object_class, 'member'),
str_contains($object_class, 'organization'),
str_contains($object_class, 'group') => 'users',

case (str_contains($object_class, 'element')):
return 'file-code-o';
str_contains($object_class, 'element') => 'file-code-o',

default:
return $map['__default__'];
}
default => $map['__default__']
};
}

/**
Expand Down
23 changes: 6 additions & 17 deletions lib/midgard/admin/asgard/handler/object/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,12 @@ public function _handler_edit(Request $request, string $handler_id, string $guid
$data['attachment_text_types'] = $this->_config->get_array('attachment_text_types');
if (array_key_exists($this->_file->mimetype, $data['attachment_text_types'])) {
// Figure out correct syntax from MIME type
switch (preg_replace('/.+?\//', '', $this->_file->mimetype)) {
case 'css':
$data['file_syntax'] = 'css';
break;

case 'html':
$data['file_syntax'] = 'html';
break;

case 'x-javascript':
case 'javascript':
$data['file_syntax'] = 'javascript';
break;

default:
$data['file_syntax'] = 'text';
}
$data['file_syntax'] = match (preg_replace('/.+?\//', '', $this->_file->mimetype)) {
'css' => 'css',
'html' => 'html',
'x-javascript', 'javascript' => 'javascript',
default => 'text'
};
}

midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data);
Expand Down
41 changes: 12 additions & 29 deletions lib/midgard/admin/asgard/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,19 @@ public static function bind_to_object(midcom_core_dbaobject $object, string $han

public static function set_pagetitle($object, string $handler_id, array &$data)
{
$l10n = midcom::get()->i18n->get_l10n('midgard.admin.asgard');
// Figure out correct title and language handling
switch ($handler_id) {
case 'object_edit':
$title_string = midcom::get()->i18n->get_string('edit %s %s', 'midgard.admin.asgard');
break;
case 'object_metadata':
$title_string = midcom::get()->i18n->get_string('metadata of %s %s', 'midgard.admin.asgard');
break;
case 'object_attachments':
case 'object_attachments_edit':
$title_string = midcom::get()->i18n->get_string('attachments of %s %s', 'midgard.admin.asgard');
break;
case 'object_parameters':
$title_string = midcom::get()->i18n->get_string('parameters of %s %s', 'midgard.admin.asgard');
break;
case 'object_permissions':
// Figure out label for the object's class
$type = $data['object_reflector']->get_class_label();
$title_string = sprintf(midcom::get()->i18n->get_string('permissions for %s %s', 'midgard.admin.asgard'), $type, $data['object_reflector']->get_object_label($object));
break;
case 'object_create':
$title_string = sprintf(midcom::get()->i18n->get_string('create %s under %s', 'midgard.admin.asgard'), self::get_type_label($data['current_type']), '%s %s');
break;
case 'object_delete':
$title_string = midcom::get()->i18n->get_string('delete %s %s', 'midgard.admin.asgard');
break;
default:
$title_string = midcom::get()->i18n->get_string('%s %s', 'midgard.admin.asgard');
break;
}
$title_string = match ($handler_id) {
'object_edit' => $l10n->get('edit %s %s'),
'object_metadata' => $l10n->get('metadata of %s %s'),
'object_attachments',
'object_attachments_edit' => $l10n->get('attachments of %s %s'),
'object_parameters' => $l10n->get('parameters of %s %s'),
'object_permissions' => $l10n->get('permissions for %s %s'),
'object_create' => sprintf($l10n->get('create %s under %s'), self::get_type_label($data['current_type']), '%s %s'),
'object_delete' => $l10n->get('delete %s %s'),
default => $l10n->get('%s %s')
};

$label = $data['object_reflector']->get_object_label($object);
$type_label = self::get_type_label(get_class($object));
Expand Down
49 changes: 16 additions & 33 deletions lib/midgard/admin/asgard/schemadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,16 @@ private function _get_score(string $field) : int
$address_fields = $this->_config->get_array('object_address_fields');
$location_fields = $this->_config->get_array('object_location_fields');

$score = 7;

if ($this->reflector->get_midgard_type($field) == MGD_TYPE_LONGTEXT) {
$score = 1;
} elseif (in_array($field, $preferred_fields)) {
$score = 0;
} elseif ($this->reflector->is_link($field)) {
$score = 2;
} elseif (in_array($field, $timerange_fields)) {
$score = 3;
} elseif (in_array($field, $phone_fields)) {
$score = 4;
} elseif (in_array($field, $address_fields)) {
$score = 5;
} elseif (in_array($field, $location_fields)) {
$score = 6;
}

return $score;
return match (true) {
$this->reflector->get_midgard_type($field) == MGD_TYPE_LONGTEXT => 1,
in_array($field, $preferred_fields) => 0,
$this->reflector->is_link($field) => 2,
in_array($field, $timerange_fields) => 3,
in_array($field, $phone_fields) => 4,
in_array($field, $address_fields) => 5,
in_array($field, $location_fields) => 6,
default => 7
};
}

public function sort_schema_fields(string $first, string $second) : int
Expand All @@ -251,20 +242,12 @@ public function sort_schema_fields(string $first, string $second) : int
|| $score1 > 6) {
return strnatcmp($first, $second);
}
switch ($score1) {
case 3:
$type = 'timerange';
break;
case 4:
$type = 'phone';
break;
case 5:
$type = 'address';
break;
case 6:
$type = 'location';
break;
}
$type = match ($score1) {
3 => 'timerange',
4 => 'phone',
5 => 'address',
6 => 'location'
};
$fields = $this->_config->get_array('object_' . $type . '_fields');
return array_search($first, $fields) <=> array_search($second, $fields);
}
Expand Down
30 changes: 8 additions & 22 deletions lib/org/openpsa/helpers/handler/chooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,10 @@ private function _load_component_node()
throw new midcom_error("Could not load node information for topic {$topic_guid}. Last error was: " . midcom_connection::get_error_string());
}

switch ($this->_dbaclass) {
case 'org_openpsa_contacts_person_dba':
$title = 'person';
break;
case 'org_openpsa_products_product_group_dba':
$title = 'product group';
break;
default:
throw new midcom_error("The DBA class {$this->_dbaclass} is unsupported");
}
$title = match ($this->_dbaclass) {
'org_openpsa_contacts_person_dba' => 'person',
'org_openpsa_products_product_group_dba' => 'product group'
};
$title = sprintf($this->_l10n_midcom->get('create %s'), $this->_i18n->get_string($title, $this->_node[MIDCOM_NAV_COMPONENT]));
midcom::get()->head->set_pagetitle($title);
}
Expand All @@ -111,18 +105,10 @@ private function _load_component_node()
*/
private function _get_schemadb_snippet() : string
{
$config_key = 'schemadb';

switch ($this->_dbaclass) {
case 'org_openpsa_contacts_person_dba':
$config_key .= '_person';
break;
case 'org_openpsa_products_product_group_dba':
$config_key .= '_group';
break;
default:
throw new midcom_error("The DBA class {$this->_dbaclass} is unsupported");
}
$config_key = 'schemadb' . match ($this->_dbaclass) {
'org_openpsa_contacts_person_dba' => '_person',
'org_openpsa_products_product_group_dba' => '_group'
};

return $this->_node[MIDCOM_NAV_CONFIGURATION]->get($config_key);
}
Expand Down
23 changes: 7 additions & 16 deletions lib/org/openpsa/invoices/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,13 @@ public function __construct(org_openpsa_invoices_invoice_dba $invoice)

public function get_current_status() : string
{
switch ($this->invoice->get_status()) {
case 'unsent':
return $this->l10n->get('unsent');

case 'open':
return sprintf($this->l10n->get('due on %s'), $this->l10n->get_formatter()->date($this->invoice->due));

case 'overdue':
return '<span class="bad">' . sprintf($this->l10n->get('overdue since %s'), $this->l10n->get_formatter()->date($this->invoice->due)) . '</span>';

case 'paid':
return sprintf($this->l10n->get('paid on %s'), $this->l10n->get_formatter()->date($this->invoice->paid));

case 'canceled':
return sprintf($this->l10n->get('invoice canceled on %s'), $this->l10n->get_formatter()->date($this->invoice->paid));
}
return match ($this->invoice->get_status()) {
'unsent' => $this->l10n->get('unsent'),
'open' => sprintf($this->l10n->get('due on %s'), $this->l10n->get_formatter()->date($this->invoice->due)),
'overdue' => '<span class="bad">' . sprintf($this->l10n->get('overdue since %s'), $this->l10n->get_formatter()->date($this->invoice->due)) . '</span>',
'paid' => sprintf($this->l10n->get('paid on %s'), $this->l10n->get_formatter()->date($this->invoice->paid)),
'canceled' => sprintf($this->l10n->get('invoice canceled on %s'), $this->l10n->get_formatter()->date($this->invoice->paid))
};
}

public function get_status_class() : string
Expand Down
27 changes: 9 additions & 18 deletions lib/org/openpsa/projects/handler/task/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,17 @@ public function _show_list(string $handler_id, array &$data)

private function get_status_type(org_openpsa_projects_task_dba $task) : string
{
$type = 'closed';
$is_manager = $task->manager == midcom_connection::get_user();
switch ($task->status) {
case org_openpsa_projects_task_status_dba::PROPOSED:
$type = ($is_manager) ? 'pending_accept' : 'proposed';
break;
case org_openpsa_projects_task_status_dba::STARTED:
case org_openpsa_projects_task_status_dba::REOPENED:
case org_openpsa_projects_task_status_dba::ACCEPTED:
$type = 'current';
break;
case org_openpsa_projects_task_status_dba::DECLINED:
$type = 'declined';
break;
case org_openpsa_projects_task_status_dba::COMPLETED:
$type = ($is_manager) ? 'pending_approve' : 'completed';
break;
}

return $type;
return match ($task->status) {
org_openpsa_projects_task_status_dba::PROPOSED => ($is_manager) ? 'pending_accept' : 'proposed',
org_openpsa_projects_task_status_dba::STARTED,
org_openpsa_projects_task_status_dba::REOPENED,
org_openpsa_projects_task_status_dba::ACCEPTED => 'current',
org_openpsa_projects_task_status_dba::DECLINED => 'declined',
org_openpsa_projects_task_status_dba::COMPLETED => ($is_manager) ? 'pending_approve' : 'completed',
default => 'closed'
};
}

private function render_workflow_controls(org_openpsa_projects_task_dba $task) : string
Expand Down
46 changes: 15 additions & 31 deletions lib/org/openpsa/relatedto/handler/relatedto.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ private function _get_object_links(bool $inbound) : array
*/
private function _get_object_links_sort_time(midcom_core_dbaobject $obj)
{
switch (true) {
case $obj instanceof org_openpsa_calendar_event_dba:
case $obj instanceof org_openpsa_projects_task_dba:
return $obj->start;
default:
return $obj->metadata->created;
}
return match (true) {
$obj instanceof org_openpsa_calendar_event_dba,
$obj instanceof org_openpsa_projects_task_dba => $obj->start,
default => $obj->metadata->created
};
}

/**
Expand Down Expand Up @@ -197,30 +195,16 @@ private function _render_line(array $link, midcom_core_dbaobject &$other_obj)
$other_obj = new $link['class']($other_obj);
}

switch ($link['class']) {
case net_nemein_wiki_wikipage::class:
$this->_render_line_wikipage($other_obj);
break;
case org_openpsa_calendar_event_dba::class:
$this->_render_line_event($other_obj);
break;
case org_openpsa_projects_task_dba::class:
case org_openpsa_projects_project::class:
$this->_render_line_task($other_obj);
break;
case org_openpsa_documents_document_dba::class:
$this->_render_line_document($other_obj);
break;
case org_openpsa_sales_salesproject_dba::class:
$this->_render_line_salesproject($other_obj);
break;
case org_openpsa_invoices_invoice_dba::class:
$this->_render_line_invoice($other_obj);
break;
default:
$this->_render_line_default($link, $other_obj);
break;
}
match ($link['class']) {
net_nemein_wiki_wikipage::class => $this->_render_line_wikipage($other_obj),
org_openpsa_calendar_event_dba::class => $this->_render_line_event($other_obj),
org_openpsa_projects_task_dba::class,
org_openpsa_projects_project::class => $this->_render_line_task($other_obj),
org_openpsa_documents_document_dba::class => $this->_render_line_document($other_obj),
org_openpsa_sales_salesproject_dba::class => $this->_render_line_salesproject($other_obj),
org_openpsa_invoices_invoice_dba::class => $this->_render_line_invoice($other_obj),
default => $this->_render_line_default($link, $other_obj)
};
}

private function get_node_url(string $component)
Expand Down
Loading

0 comments on commit 2874e43

Please sign in to comment.