-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConceptSearchTerm.inc.php
executable file
·153 lines (139 loc) · 4.99 KB
/
ConceptSearchTerm.inc.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Represents a single search term. Search terms can be string, numeric,
* numeric range, or map_code. A single term may be more than one type.
*/
class ConceptSearchTerm
{
/**
* Type of search term.
*/
public $term_type = MCL_SEARCH_TERM_TYPE_DEFAULT;
/**
* Full text of the search term before parsing.
*/
public $full_search_term = '';
/**
* Search term after the operator has been parsed out.
*/
public $needle = '';
/**
* Value of the search operator used, if any.
*/
public $search_operator = '';
/**
* Value of the search function used, if any.
*/
public $search_function = '';
/**
* Constructor
*/
public function ConceptSearchTerm($term_type, $needle, $full_search_term = '',
$search_operator = '', $search_function = '')
{
$this->term_type = $term_type;
$this->needle = $needle;
if ($full_search_term) {
$this->full_search_term = $full_search_term;
} else {
$this->full_search_term = $needle;
}
$this->search_operator = $search_operator;
$this->search_function = $search_function;
}
/**
* If the search term is a numeric range, returns an array whose first element
* is the range minimum and second element is the range maximum. If this term
* is not a numeric range, returns null.
*/
public function getRange()
{
if (substr_count($this->needle, '-') != 1) return null;
list($min, $max) = explode('-', $this->needle);
if ((is_numeric($min) && ((int)$min == $min)) &&
(is_numeric($max) && ((int)$max == $max)) )
{
$min = (int)$min;
$max = (int)$max;
if ($min <= $max) return array($min, $max);
else return array($max, $min);
}
return null;
}
/**
* True if the search needle evaluates to an integer
* (meaning, it can be a string representation of an integer).
*/
public function isInteger()
{
if (is_numeric($this->needle)) {
if ((int)$this->needle == $this->needle) {
return true;
}
return false;
}
return false;
}
/**
* Whether this search term has an operator of type MCL_OPERATOR_FILTER
*/
public function isFilter() {
if (!$this->search_operator) return false;
$cstf = new ConceptSearchTermFactory();
return $cstf->isFilterOperator($this->search_operator);
}
/**
* True if this search term object matches the passed Concept; false otherwise.
* @param Concept $c
* @param mixed $search_term_type One of the MCL_SEARCH_TERM_TYPE_* constants. Use to force a specific type of comparison
* @return bool
*/
public function isMatch(Concept $c, $search_term_type = null)
{
// Prepare the search term type for comparison
if (is_null($search_term_type)) $search_term_type = $this->term_type;
// Now do the comparison
if ($search_term_type == MCL_SEARCH_TERM_TYPE_ALL) {
return true;
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_CONCEPT_ID) {
if ($this->needle == $c->concept_id) return true;
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_CONCEPT_ID_RANGE) {
list($min, $max) = explode('-', $this->needle);
if ( ($c->concept_id >= $min) && ($c->concept_id <= $max) ) return true;
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_MAP_CODE) {
foreach ($c->getConceptMappingIds() as $mapcode_id) {
$subject = $c->getConceptMapping($mapcode_id)->source_code;
if ( preg_match('/\b' . addslashes($this->needle) . '/i', $subject) ) return true;
}
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_MAP_CODE_RANGE) {
// TODO: MCL_SEARCH_TERM_TYPE_MAP_CODE_RANGE
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_TEXT) {
foreach ($c->getConceptNameIds() as $name_id) {
$subject = $c->getConceptName($name_id)->name;
if ( preg_match('/\b' . addslashes($this->needle) . '/i', $subject) ) return true;
}
foreach ($c->getConceptDescriptionIds() as $desc_id) {
$subject = $c->getConceptDescription($desc_id)->description;
if ( preg_match('/\b' . addslashes($this->needle) . '/i', $subject) ) return true;
}
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_CONCEPT_NAME) {
foreach ($c->getConceptNameIds() as $name_id) {
$subject = $c->getConceptName($name_id)->name;
if ( preg_match('/\b' . addslashes($this->needle) . '/i', $subject) ) return true;
}
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_CONCEPT_DESCRIPTION) {
foreach ($c->getConceptDescriptionIds() as $desc_id) {
$subject = $c->getConceptDescription($desc_id)->description;
if ( preg_match('/\b' . addslashes($this->needle) . '/i', $subject) ) return true;
}
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_UUID) {
if ( strtolower($this->needle) == strtolower(substr($c->uuid, 0, strlen($this->needle))) ) return true;
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_LIST) {
// TODO: MCL_SEARCH_TERM_TYPE_LIST
} elseif ($search_term_type == MCL_SEARCH_TERM_TYPE_MAP_SOURCE) {
// TODO: MCL_SEARCH_TERM_TYPE_MAP_SOURCE
}
return false;
}
}
?>