1 <?php
2 3 4
5 namespace Team3\PayU\SignatureCalculator\Validator;
6
7 use Team3\PayU\SignatureCalculator\Encoder\Algorithms\AlgorithmInterface;
8
9 class AlgorithmExtractor implements AlgorithmExtractorInterface
10 {
11 12 13 14 15 16 17 18 19 20 21
22 public function extractAlgorithm($signatureHeader, array $algorithms)
23 {
24 $algorithmName = $this->extractAlgorithmString($signatureHeader);
25
26 foreach ($algorithms as $algorithm) {
27 if ($this->isNameEqual($algorithmName, $algorithm)) {
28 return $algorithm;
29 }
30 }
31
32 throw new AlgorithmExtractorException(sprintf(
33 'There is no algorithm with name %s.',
34 $algorithmName
35 ));
36 }
37
38 39 40 41 42 43
44 private function extractAlgorithmString($signatureHeader)
45 {
46 $matches = [];
47 preg_match('/algorithm=([a-zA-Z0-9]+);/', $signatureHeader, $matches);
48 if (array_key_exists(1, $matches)) {
49 return $matches[1];
50 }
51
52 throw new AlgorithmExtractorException(sprintf(
53 'Could not extract algorithm name from string "%s"',
54 $signatureHeader
55 ));
56 }
57
58 59 60 61 62 63
64 private function isNameEqual($name, AlgorithmInterface $algorithm)
65 {
66 return 0 === strcasecmp($name, $algorithm->getName());
67 }
68 }
69