<?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_53 implements IPruefziffer
{
  public static function check($pKtoNr, $pBlz, $pGewichtung = array(), $pPruefZifferStelle = 2)
  {
    $blz = (string)$pBlz;
    if (empty($pGewichtung)) {
      $gewichtung = array(2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4);
    } else {
      $gewichtung = $pGewichtung;
    }

    if ($blz[3] != 5) {
      return false;
    }

    $laengeKtoNr = strlen((string)($pKtoNr * 1));
    $ktoNr = (string)($pKtoNr * 1);

    if ($laengeKtoNr == 9) {
      
      $altKtoNr = substr($blz, 4, 2) . $ktoNr[1] . $blz[7] . $ktoNr[0] . '0' . (string)(substr($ktoNr, 3, 6) * 1);

      $result = ($ktoNr[$pPruefZifferStelle] == Pruefziffer_52::calculatePZ_52($altKtoNr, $gewichtung));
    } elseif ($laengeKtoNr == 10 && $ktoNr[0] == 9) {
      $result = Pruefziffer_20::check($pKtoNr, $blz);
    } else {
      $result = false;
    }

    return $result;
  }
}