|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PubPeerFoundation\PublicationDataExtractor\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Tightenco\Collect\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
class Model |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* This var should never be used, in favor of Model's internal var. |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $list = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* List of statically created instances. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $instances = []; |
|
23
|
|
|
|
|
24
|
|
|
protected function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function __clone() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __wakeup() |
|
36
|
|
|
{ |
|
37
|
|
|
throw new Exception('Cannot unserialize singleton'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the current Model's instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @return Model |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getInstance() |
|
46
|
|
|
{ |
|
47
|
|
|
$cls = get_called_class(); |
|
48
|
|
|
if (! isset(self::$instances[$cls])) { |
|
49
|
|
|
self::$instances[$cls] = new static(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return self::$instances[$cls]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Figure out which values are already known. |
|
57
|
|
|
* |
|
58
|
|
|
* @param $key |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function knownIdentifierValues($key): array |
|
62
|
|
|
{ |
|
63
|
|
|
return array_map(function ($value) { |
|
64
|
|
|
return strtolower($value); |
|
65
|
|
|
}, Arr::pluck($this->list, $key)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Should the attribute be kept? |
|
70
|
|
|
* |
|
71
|
|
|
* @param $key |
|
72
|
|
|
* @param $value |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function shouldKeepAttribute($key, $value): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$function = is_array($value) ? 'count' : 'strlen'; |
|
78
|
|
|
|
|
79
|
|
|
if (! isset($this->list[$key])) { |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $function($this->list[$key]) > $function($value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function reset() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->list = []; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|