/
AbstractBicDictionary.php
43 lines (34 loc) · 1.09 KB
/
AbstractBicDictionary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace rikudou\SkQrPayment\IbanToBic\Dictionary;
use Rikudou\Iban\Iban\IbanInterface;
use rikudou\SkQrPayment\Exception\BicNotFoundException;
use rikudou\SkQrPayment\Helper\CacheableBicDictionaryTrait;
abstract class AbstractBicDictionary implements BicDictionaryInterface
{
use CacheableBicDictionaryTrait;
public function getBic(IbanInterface $iban): string
{
if ($this->isCached($iban)) {
return $this->getCached($iban);
}
if (isset($this->getMap()[$this->getBankCode($iban)])) {
$bic = $this->getMap()[$this->getBankCode($iban)];
$this->cacheResult($iban, $bic);
return $bic;
}
throw new BicNotFoundException(sprintf(
'Could not find BIC for IBAN "%s"',
$iban->asString()
));
}
/**
* Returns the bank code map in format 'bankCode' => 'BIC'
*
* @return array<string, string>
*/
abstract protected function getMap(): array;
private function getBankCode(IbanInterface $iban): string
{
return substr($iban, 4, 4);
}
}