<?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_A4 implements IPruefziffer
{
  private static $ktoNr;
  private static $blz;

  public static function check($pKtoNr, $pBlz, $pGewichtung = array(), $pPruefZifferStelle = 9)
  {
    self::$ktoNr = \str_pad($pKtoNr, 10, '0', STR_PAD_LEFT);
    self::$blz = $pBlz;

    if (self::$ktoNr[2] == 9 && self::$ktoNr[3] == 9) {
      if (self::variante3()) {
        return true;
      } elseif (self::variante4()) {
        return true;
      } else {
        return false;
      }
    } else {
      if (self::variante1()) {
        return true;
      } elseif (self::variante2()) {
        return true;
      } elseif (self::variante4()) {
        return true;
      } else {
        return false;
      }
    }
  }

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

    return Pruefziffer_06::check(self::$ktoNr, self::$blz, $gewichtung);
  }

  private static function variante2()
  {
    $ktoNrRev = strrev(self::$ktoNr);
    $gewichtung = array(0, 2, 3, 4, 5, 6, 7, 0, 0, 0);
    $ary = PruefZiffHelpers::gewichten($ktoNrRev, $gewichtung);

    $sum = array_sum($ary);

    $pfz = 7 - ($sum % 7);

    return ($pfz == self::$ktoNr[9]);
  }

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

    return Pruefziffer_06::check(self::$ktoNr, self::$blz, $gewichtung);
  }

  private static function variante4()
  {
    return Pruefziffer_93::check(self::$ktoNr, self::$blz);
  }
}