<commit_before><?php
/*-------------------------------------------------------+
| Project 60 - Little BIC extension                      |
| Copyright (C) 2014                                     |
| Author: <NAME> (endres -at- systopia.de)            |
| http://www.systopia.de/                                |
+--------------------------------------------------------+
| This program is released as free software under the    |
| Affero GPL license. You can redistribute it and/or     |
| modify it under the terms of this license which you    |
| can read by viewing the included agpl.txt or online    |
| at www.gnu.org/licenses/agpl.html. Removal of this     |
| copyright header is strictly prohibited without        |
| written permission from the original author(s).        |
+--------------------------------------------------------*/

/**
 * API call to look up BIC codes for a given IBAN
 * 
 * @param 'iban'  an IBAN number
 */
function civicrm_api3_bic_getfromiban($params) {
  if (empty($params['iban'])) {
    return civicrm_api3_create_error("You need to provied an 'iban'.");
  }

  $country = strtoupper(substr($params['iban'], 0, 2));
  $parser = CRM_Bic_Parser_Parser::getParser($country);
  if (empty($parser)) {
    return civicrm_api3_create_error("Parser for '$country' not found!");
  }

  $nbids = $parser->extractNBIDfromIBAN($params['iban']);
  if ($nbids==FALSE) {
    return civicrm_api3_create_error("IBAN parsing not supported for country '$country'!");
  }

  foreach ($nbids as $nbid) {
    try {
      return civicrm_api3('Bic', 'getsingle', array('country' => $country, 'nbid' => $nbid));
    } catch (Exception $e) {
      // not found? no problem, just keep looking
    }<commit_msg>[#42] fixed findbyiban for duplicate entries (merged dev_42)<commit_after><?php
/*-------------------------------------------------------+
| Project 60 - Little BIC extension                      |
| Copyright (C) 2014                                     |
| Author: <NAME> (endres -at- systopia.de)            |
| http://www.systopia.de/                                |
+--------------------------------------------------------+
| This program is released as free software under the    |
| Affero GPL license. You can redistribute it and/or     |
| modify it under the terms of this license which you    |
| can read by viewing the included agpl.txt or online    |
| at www.gnu.org/licenses/agpl.html. Removal of this     |
| copyright header is strictly prohibited without        |
| written permission from the original author(s).        |
+--------------------------------------------------------*/

/**
 * API call to look up BIC codes for a given IBAN
 * 
 * @param 'iban'  an IBAN number
 */
function civicrm_api3_bic_getfromiban($params) {
  if (empty($params['iban'])) {
    return civicrm_api3_create_error("You need to provied an 'iban'.");
  }

  $country = strtoupper(substr($params['iban'], 0, 2));
  $parser = CRM_Bic_Parser_Parser::getParser($country);
  if (empty($parser)) {
    return civicrm_api3_create_error("Parser for '$country' not found!");
  }

  $nbids = $parser->extractNBIDfromIBAN($params['iban']);
  if ($nbids==FALSE) {
    return civicrm_api3_create_error("IBAN parsing not supported for country '$country'!");
  }

  foreach ($nbids as $nbid) {
    try {
      $search = civicrm_api3('Bic', 'get', ['country' => $country, 'nbid' => $nbid]);
      // check if all entities are the same
      $candidate = NULL;
      foreach ($search['values'] as $entity) {
        if ($candidate == NULL) {
          $candidate = $entity;
        } else {
          if ($candidate != $entity) {
            // two different matches found!
            Civi::log()->debug("LittleBIC: contradicting bank records detected: {$country}{$nbid}");
            return civicrm_api3_create_error("Contradicting bank records detected: {$country}{$nbid}");

          } else {
            // identical records!
            Civi::log()->debug("LittleBIC: duplicate bank records detected: {$country}{$nbid}");
          }
        }
      }

      if ($candidate) {
        return $candidate;
      }

    } catch (Exception $e) {
      // not found? no problem, just keep looking
    }
{'fraction_non_alphanumeric': 0.201329534662868, 'fraction_numerical': 0.0073599240265906935, 'mean_word_length': 1.9669014084507042, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4688355', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1151, 'n_words': 344}