<commit_before><?php

/*
  All input and output of this module, is strictly associative arrays and json.
  Has no awareness of spec classes.
*/

// Retrieves a json list and returns it as an associative array if id is not specified.
// If the id is specified, it will return one document. If it finds nothing, this returns null.
function getJson ($resource, $id) {
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  if ($id === NULL)
    return $json;
  else {
    foreach ($json as $item) {
      if ($item['id'] == $id)
        return $item;
    }
    return NULL;
  }
}

// Retrieves a json list and returns it as an associative array if id is not specified.
// If the id is specified, it will return one document. If it finds nothing, this returns null.
function getNewId ($resource, $id) {
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  $highest = 1;
  foreach ($json as $item) {
    if ($item['id'] > $highest)
      $highest = $item['id'] + 1;
  }
  return $highest;
}

// Adds an entry to a json source. $data must have toArray() associative
function saveJson ($resource, $data) {
  // Retrieve json list. associative.
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  // Add to list, data must be turned into associative array.<commit_msg>Fix parameters in getNewId() in json module<commit_after><?php

/*
  All input and output of this module, is strictly associative arrays and json.
  Has no awareness of spec classes.
*/

// Retrieves a json list and returns it as an associative array if id is not specified.
// If the id is specified, it will return one document. If it finds nothing, this returns null.
function getJson ($resource, $id) {
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  if ($id === NULL)
    return $json;
  else {
    foreach ($json as $item) {
      if ($item['id'] == $id)
        return $item;
    }
    return NULL;
  }
}

// Retrieves a json list and returns it as an associative array if id is not specified.
// If the id is specified, it will return one document. If it finds nothing, this returns null.
function getNewId ($resource) {
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  $highest = 1;
  foreach ($json as $item) {
    if ($item['id'] > $highest)
      $highest = $item['id'] + 1;
  }
  return $highest;
}

// Adds an entry to a json source. $data must have toArray() associative
function saveJson ($resource, $data) {
  // Retrieve json list. associative.
  $json = file_get_contents('./json/' . $resource . '.json');
  $json = json_decode($json, true);

  // Add to list, data must be turned into associative array.
{'fraction_non_alphanumeric': 0.13493465206640762, 'fraction_numerical': 0.001412928293889085, 'mean_word_length': 3.128279883381924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '705176', 'n_tokens_mistral': 884, 'n_tokens_neox': 847, 'n_words': 356}