<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
class Pruefziffer_83 implements IPruefziffer
{
  public static function check($pKtoNr, $pBlz, $pGewichtung = array(), $pPruefZifferStelle = 9)
  {
    $ktoNr = \str_pad($pKtoNr, 10, '0', STR_PAD_LEFT);

    if (substr($ktoNr, 2, 2) != '99') {
      $result = self::methodeA($ktoNr);
      if (!$result) {
        $result = self::methodeB($ktoNr);
      }
      if (!$result) {
        $result = self::methodeC($ktoNr);
      }
    } else {
      $result = self::methodeD($ktoNr);
    }

    return $result;
  }

  private static function methodeA($pKtoNr)
  {
    $gewichtung = array(0, 2, 3, 4, 5, 6, 7, 0, 0, 0);

    return Pruefziffer_32::check($pKtoNr, null, $gewichtung);
  }

  private static function methodeB($pKtoNr)
  {
    $gewichtung = array(0, 2, 3, 4, 5, 6, 0, 0, 0, 0);

    return Pruefziffer_33::check($pKtoNr, null, $gewichtung);
  }

  private static function methodeC($pKtoNr)
  {
    $gewichtung = array(0, 2, 3, 4, 5, 6, 0, 0, 0, 0);

    $strZahl = strrev($pKtoNr);

    $ary = PruefZiffHelpers::gewichten($strZahl, $gewichtung);

    $rest = array_sum($ary) % 7;

    $result = (7 - $rest) % 7;

    return ($result == $pKtoNr[9]);
  }

  private static function methodeD($pKtoNr)
  {
    $gewichtung = array(0, 2, 3, 4, 5, 6, 7, 8, 0, 0);

    $strZahl = strrev($pKtoNr);

    $ary = PruefZiffHelpers::gewichten($strZahl, $gewichtung);
    $rest = array_sum($ary) % 11;
    
    $result = ((11 - $rest) % 11) % 10;

    return ($result == $pKtoNr[9]);
  }
}