1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Helpers; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use function array_merge; |
17
|
|
|
use function in_array; |
18
|
|
|
use function is_array; |
19
|
|
|
use function is_string; |
20
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
21
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
22
|
|
|
use Maslosoft\Mangan\Helpers\CompositionIterator; |
23
|
|
|
use Maslosoft\Manganel\Meta\DocumentPropertyMeta; |
24
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
25
|
|
|
use MongoId; |
26
|
|
|
use function strip_tags; |
27
|
|
|
use function strpos; |
28
|
|
|
|
29
|
|
|
class TextExtractor |
30
|
|
|
{ |
31
|
|
|
private static $banFields = [ |
32
|
|
|
'_class', |
33
|
|
|
'id', |
34
|
|
|
'_id', |
35
|
|
|
'createDate', |
36
|
|
|
'updateDate', |
37
|
|
|
'createUser', |
38
|
|
|
'createUserId', |
39
|
|
|
'updateUser', |
40
|
|
|
'updateUserId', |
41
|
|
|
'wasSaved' |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Extract text values from model with $minBoost for field |
46
|
|
|
* @param AnnotatedInterface $model |
47
|
|
|
* @param float $minBoost |
48
|
|
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
public static function extract(AnnotatedInterface $model, $minBoost = 1.0) |
51
|
|
|
{ |
52
|
|
|
$texts = []; |
53
|
|
|
|
54
|
|
|
foreach(ManganelMeta::create($model)->fields() as $metaProperty) |
55
|
|
|
{ |
56
|
|
|
/* @var $metaProperty DocumentPropertyMeta */ |
57
|
|
|
$name = $metaProperty->name; |
58
|
|
|
|
59
|
|
|
// Skip system fields |
60
|
|
|
if(in_array($name, self::$banFields)) |
61
|
|
|
{ |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if($metaProperty->searchBoost < $minBoost) |
66
|
|
|
{ |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
if(!$metaProperty->searchable) |
70
|
|
|
{ |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$values = $model->$name; |
75
|
|
|
if(!is_array($values) && !is_string($values)) |
76
|
|
|
{ |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
if(!is_array($values) && is_string($values)) |
80
|
|
|
{ |
81
|
|
|
$values = [$values]; |
82
|
|
|
} |
83
|
|
|
foreach($values as $value) |
84
|
|
|
{ |
85
|
|
|
if (is_string($value)) |
86
|
|
|
{ |
87
|
|
|
// Skip class names |
88
|
|
|
if(strpos($value, '\\') !== false && ClassChecker::exists($value)) |
89
|
|
|
{ |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
if(empty($value)) |
93
|
|
|
{ |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Ignore ID's |
98
|
|
|
if(MongoId::isValid($value)) |
99
|
|
|
{ |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
if(strpos($value, '<') !== false) |
103
|
|
|
{ |
104
|
|
|
$value = strip_tags($value); |
105
|
|
|
} |
106
|
|
|
$value = trim($value); |
107
|
|
|
if(empty($value)) |
108
|
|
|
{ |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$texts[] = $value; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach(new CompositionIterator($model) as $subModel) |
117
|
|
|
{ |
118
|
|
|
$texts = array_merge($texts, self::extract($subModel, $minBoost)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $texts; |
122
|
|
|
} |
123
|
|
|
} |