I'm building a website that contains users with user profiles. Many of the fields in the profile are optional.
There is an opportunity for a lot of user-generated content, and so I need to display the author of this content in many different locations of the site (comments, posts, etc.). In the user's profile, he is able to (optionally) fill out his "first name", his "last name", and a "display name".
To display the author, I wrote a helper method that looks through a provided array of these fields and returns the most appropriate name for the user, in this order of preference:
display_name
, this will be displayed. first_name
and last_name
, but no display_name
, it will display both names first_name
, it will display first_name
. last_name
, it will display last_name
. user123
NULL
The method works great, but it's ugly. There must be a way to beautify this with an alternative to nested if/else statements.
public function nameify($names = NULL) {
$name = '';
if (!empty($names)) {
if (!empty($names['display_name'])) {
$name = $names['display_name'];
} elseif (!empty($names['first_name'])) {
$name = $names['first_name'];
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
} elseif (!empty($names['last_name'])) {
$name = $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
} else {
$name = 'NULL';
}
} else {
$name = 'NULL';
}
return $name;
}
public function nameify($names = NULL) {
if ($names) {
if (!empty($names['display_name'])) {
return $names['display_name'];
}
if (!empty($names['first_name'])) {
$name = $names['first_name'];
}
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
}
}
return $name ? ltrim($name) : 'NULL';
}
Set the default first, and return that if nothing else matches. Then since we always want to return the display name if we have it do just that.
EDIT: Tweak to prevent returning "NULL "
Using ternary conditions we can shorten and beautify the code:
public function nameify($names = NULL) {
$name = 'NULL';
if (!empty($names)) {
$name = ($names['display_name']) ? $names['display_name'] : trim($names['first_name']." ".$names['last_name']);
if(!$name) $name = ($names['id'] > 0) ? 'user'.$names['id'] : 'NULL';
}
return $name;
}
I would propose this:
public function nameify($names = null) { if(empty($names)) return null; if(!empty($names['display_name'])) return $names['display_name']; if(!empty($names['first_name'])) { $name = $names['first_name']; if (!empty($names['last_name'])) { $name .= ' ' . $names['last_name']; } return $name; } if(!empty($names['id])) return 'user' . $names['id']; return null; }
It is not much, but because $name it is at least NULL:
public function nameify($names = NULL) {
$name = 'NULL';
if (!empty($names)) {
if (!empty($names['display_name'])) {
$name = $names['display_name'];
} elseif (!empty($names['first_name'])) {
$name = $names['first_name'];
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
} elseif (!empty($names['last_name'])) {
$name = $names['last_name'];
}
if ($name=='NULL' && !empty($names['id'])) {
$name = 'user' . $names['id'];
}
}
return $name;
}
I'd go with:
if( empty($names['display_name']) ) {
$name = $names['first_name'] . ' ' $names['last_name'];
else
$name = $names['display_name'];
$name = trim($name);
if( empty($name) )
$name = 'user'.$names['id'];
if( empty($name) )
$name = 'NULL';
This would be the 'core logic'... there will need to be other checks like $names != NULL
or something..
//pointers to functions
$arrayOfSulutions{"display_name_strategy", "full_name_strategy" ..., "null_strategy" }
function display_name_strategy{
return $names['display_name'];
}
$i = 0;
while($res == null){
$res = call($arrayOfSulutions[$i++]);
}
Somewhat less readable, but effective):
list($idx,$name) = array_shift(array_filter(array(
$names['display_name'],
implode(' ',array_filter(array($names['first_name'],$names['last_name']))),
'user'.$names['id'];
)));
A State machine works very nicely for involved logic like that. It's very simple to implement as well (using a switch statement).
I'm not sure that my version would be simplier, but here it is:
public function nameify($names = null) {
$result = array();
if( !empty($names['display_name']) ) {
array_push($result,$names['display_name']);
} else {
if( !empty($names['first_name']) ) {
array_push($result, $names['first_name']);
}
if( !empty($names['last_name']) ) {
array_push($result, $names['last_name']);
}
}
if( empty($result) && !empty($names['id']) ) {
array_push($result, 'user'.$names['id']);
}
return (empty($result) ? 'NULL' : implode(' ', $result));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.