protected static function insert(&$array, $key, $insert_array, $before) {
    if (!isset($array[$key])) {
      throw new \Exception("The array does not have the key $key.");
    }

    if ($before) {
      $offset = 0;
    }
    else {
      $offset = 1;
    }

    $pos = array_search($key, array_keys($array));
    $pos += $offset;

    $array_before = array_slice($array, 0, $pos);
    $array_after = array_slice($array, $pos);

    $array = array_merge(
      $array_before,
      $insert_array,
      $array_after
    );
  }