1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/similarity |
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
6
|
|
|
* For the information of copyright and license you should read the file |
7
|
|
|
* LICENSE which is distributed with this source code. |
8
|
|
|
* Para a informação dos direitos autorais e de licença você deve ler o arquivo |
9
|
|
|
* LICENSE que é distribuído com este código-fonte. |
10
|
|
|
* Para obtener la información de los derechos de autor y la licencia debe leer |
11
|
|
|
* el archivo LICENSE que se distribuye con el código fuente. |
12
|
|
|
* For more information, see <https://www.gpupo.com/>. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Gpupo\Similarity; |
16
|
|
|
|
17
|
|
|
use Gpupo\Similarity\Input\InputInterface; |
18
|
|
|
|
19
|
|
|
abstract class SimilarityAbstract |
20
|
|
|
{ |
21
|
|
|
const ACCURACY_DEFAULT = 60; |
22
|
|
|
|
23
|
|
|
protected $input; |
24
|
|
|
|
25
|
|
|
protected $accuracy; |
26
|
|
|
|
27
|
49 |
|
protected function getAccuracy() |
28
|
|
|
{ |
29
|
49 |
|
if (empty($this->accuracy)) { |
30
|
40 |
|
$this->setAccuracy(self::ACCURACY_DEFAULT); |
31
|
|
|
} |
32
|
|
|
|
33
|
49 |
|
return $this->accuracy; |
34
|
|
|
} |
35
|
|
|
|
36
|
49 |
|
public function setAccuracy($number) |
37
|
|
|
{ |
38
|
49 |
|
$this->accuracy = intval($number); |
39
|
|
|
|
40
|
49 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
39 |
|
public function isApproximate() |
44
|
|
|
{ |
45
|
39 |
|
$calc = $this->getProximityCalculation(); |
|
|
|
|
46
|
|
|
|
47
|
39 |
|
if ($calc['difference'] <= $calc['limit']['maxDifference']) { |
48
|
18 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
21 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
68 |
|
protected function getInput() |
55
|
|
|
{ |
56
|
68 |
|
if (empty($this->input)) { |
57
|
|
|
throw new \BadMethodCallException('Missing Input'); |
58
|
68 |
|
} elseif (!$this->input instanceof InputInterface) { |
59
|
|
|
throw new \BadMethodCallException('Incompatible Input'); |
60
|
|
|
} |
61
|
|
|
|
62
|
68 |
|
return $this->input; |
63
|
|
|
} |
64
|
|
|
|
65
|
41 |
|
protected function factoryExpert($name) |
66
|
|
|
{ |
67
|
41 |
|
$expertObject = __NAMESPACE__.'\\Similar'.ucfirst($name); |
68
|
|
|
|
69
|
41 |
|
$expert = new $expertObject($this->getInput(), $this->getAccuracy()); |
70
|
|
|
|
71
|
41 |
|
return $expert; |
72
|
|
|
} |
73
|
|
|
|
74
|
68 |
|
public function __construct(InputInterface $input = null, $accuracy = null) |
75
|
|
|
{ |
76
|
68 |
|
if ($input) { |
77
|
68 |
|
$this->input = $input; |
78
|
|
|
} |
79
|
|
|
|
80
|
68 |
|
if ($accuracy) { |
81
|
41 |
|
$this->setAccuracy($accuracy); |
82
|
|
|
} |
83
|
68 |
|
} |
84
|
|
|
|
85
|
|
|
public function __toString() |
86
|
|
|
{ |
87
|
|
|
return json_encode($this->__toArray()); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
public function __toArray() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
8 |
|
'input' => $this->input, |
94
|
8 |
|
'first' => $this->getInput()->getFirst(), |
95
|
8 |
|
'second' => $this->getInput()->getSecond(), |
96
|
8 |
|
'accuracy' => $this->getAccuracy(), |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: