Skip to content

Instantly share code, notes, and snippets.

@davereid
Last active August 29, 2015 14:12
Show Gist options
  • Save davereid/e736f237b430170dd147 to your computer and use it in GitHub Desktop.
Save davereid/e736f237b430170dd147 to your computer and use it in GitHub Desktop.
<?php
// This assumes used in Drupal and with https://www.drupal.org/project/helper installed.
$data = HttpHelper::getJson('https://api.github.com/users/drewboardman/events/public');
// Transform the array into an array of event types keyed by event ID.
$events = ArrayHelper::extractNestedValuesToArray($data, array('type'), array('id'));
// Build the array of score values, including any even types that have not been accounted for.
$score_values = array(
'PushEvent' => 5,
'CreateEvent' => 4,
'IssuesEvent' => 3,
'CommitCommentEvent' => 2,
) + array_fill_keys($events, 1);
// Transform the array of events into their score values, and sum the value.
$total = array_sum(ArrayHelper::transformAssociativeValues($events, $score_values));
echo "User has a score of $total\n";
<?php
$data = json_decode(file_get_contents('https://api.github.com/users/drewboardman/events/public'), TRUE);
// Transform the array into an array of event types.
$events = array_map(function($event) {
return $event['type'];
}, $data);
// Build the array of score values, including any even types that have not been accounted for.
$score_values = array(
'PushEvent' => 5,
'CreateEvent' => 4,
'IssuesEvent' => 3,
'CommitCommentEvent' => 2,
) + array_fill_keys($events, 1);
// Transform the array of events into their score values, and sum the value.
$total = array_sum(array_map(function($event_type) use ($score_values) {
return $score_values[$event_type];
}, $events));
echo "User has a score of $total\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment