*/ function GetApiResults(string $api_url): mixed { $response = file_get_contents($api_url); return json_decode($response, true); } /** * Formats a given set of API results into an HTML section * * @access public * @param mixed $api_results The decoded API results * @param string $inline_title The

title to use in the HTML * @return string $html_output The formatted HTML result of stories from the API or the error message * @author cmc */ function ParseStories(mixed $api_results, string $inline_title): string { if ($api_results == "null") { return '

ERROR: Stories not found. API returned `null`.

'; } else { $html_output = '

' . $inline_title . '

'; for ($i = 0; $i < count($api_results); $i++) { $story_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results[$i] . '.json'); $html_output .= ConstructStory($story_api_results); } return $html_output; } } /** *Extract a user's profile from Hacker News API and format in HTML * * @access public * @param mixed $api_results The decoded API results * @param string $inline_title The

title to use in the HTML * @return string $html_output The formatted HTML result of stories from the API * @author cmc */ function ParseUser(mixed $api_results, string $inline_title): string { if ($api_results == "null") { return '

ERROR: User not found.

'; } else { // TODO: Create function to format $about using the following guidelines // : https://news.ycombinator.com/formatdoc // : hint: nl2br() will solve the first formatting requirement $about = $api_results['about']; $karma = $api_results['karma']; $created = date('Y-m-d h:m:s', $api_results['created']); $html_output = <<

$inline_title

About: $about

Karma: $karma

Created:


Recently Submitted

EOT; $limit = (count($api_results['submitted']) > 10) ? 10 : count($api_results['submitted']); if (count($api_results['submitted']) > 0) { for ($i = 0; $i < $limit; $i++) { $user_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['submitted'][$i] . '.json'); $html_output .= GetItem($user_api_results); } } else { $html_output .= '

User has no submissions.

'; } return $html_output; } } /** * Formats one specific item requested by the user * * @access public * @param mixed $api_results The decoded API results * @param string $inline_title The

title to use in the HTML * @return string $html_output The formatted HTML result of stories from the API or the error message * @author cmc */ function ParseItem(mixed $api_results): string { if ($api_results == "null") { return '

ERROR: User not found.

'; } else { $html_output = ''; if (in_array($api_results['type'], array("job", "story", "poll", "pollopt"))) { $html_output .= ConstructStoryDiscussion($api_results); } else { if (array_key_exists('parent', $api_results)) { $parent_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['parent'] . '.json'); $html_output .= GetItem($parent_api_results); $html_output .= '
'; } $html_output .= GetItem($api_results); if ($api_results['descendants'] != 0) { $html_output .= '
'; $child_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['kids'][0] . '.json'); $html_output .= GetItem($child_api_results); } } return $html_output; } } /** * Formats one item from the API to HTML * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function GetItem(mixed $api_results): string { $type = $api_results['type']; return match ($type) { 'story', 'job' => ConstructStory($api_results), 'comment' => ConstructComment($api_results), 'poll' => ConstructPoll($api_results), 'pollopt' => ConstructPollOpt($api_results), default => '[ERROR] Item type not found: ' . $type, }; } /** * Creates a story HTML element * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function ConstructStory(mixed $api_results): string { $id = $api_results['id']; $url = $api_results['url']; $title = $api_results['title']; $time = date('Y-m-d h:m:s', $api_results['time']); $by = $api_results['by']; $score = $api_results['score']; if (array_key_exists('descendants', $api_results)) { $descendants = $api_results['descendants']; } else { $descendants = 'No'; } return << $title

by $by | $score points | $descendants comments

EOT; } /** * Creates a story discussion page with comments * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function ConstructStoryDiscussion(mixed $api_results): string { $id = $api_results['id']; $url = $api_results['url']; $title = $api_results['title']; $time = date('Y-m-d h:m:s', $api_results['time']); $by = $api_results['by']; $score = $api_results['score']; $descendants = $api_results['descendants']; if (array_key_exists('text', $api_results)) { $text = $api_results['text']; } else { $text = ''; } $html_output = <<

$title

by $by | $score points | $descendants comments

$text

EOT; // TODO: Add support for more than just top-level kids (i.e., recursive). if ($api_results['descendants'] != 0) { $html_output .= <<

Comments

EOT; for ($i = 0; $i < count($api_results['kids']); $i++) { $child_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['kids'][$i] . '.json'); $html_output .= GetItem($child_api_results); } $html_output .= ''; } return $html_output; } /** * Creates a comment HTML element * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function ConstructComment(mixed $api_results): string { $time = date('Y-m-d h:m:s', $api_results['time']); $text = $api_results['text']; $by = $api_results['by']; $parent = $api_results['parent']; return <<

$text

Submitted in response to: $parent

by $by

EOT; } /** * Creates a poll HTML element * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function ConstructPoll(mixed $api_results): string { return 'TODO'; } /** * Creates a poll-option HTML element * * @access public * @param mixed $api_results The decoded API results * @return string The formatted HTML result of stories from the API or the error message * @author cmc */ function ConstructPollOpt(mixed $api_results): string { return 'TODO'; }