Result.class.php
3.62 KB
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
<?php
/**
* Created by PhpStorm.
* User: Администратор
* Date: 14.03.14
* Time: 13:47
*/
namespace Cpeople\Classes\Search;
class Result
{
protected $data;
protected $element = null;
public function __construct($data = array())
{
$this->data = $data;
}
public function getTitle()
{
return $this->data['TITLE'];
}
public function getText($limit = 500)
{
return trim(mb_substr($this->data['BODY'], 0, $limit));
}
public function getUrl()
{
if ($this->data['MODULE_ID'] === 'iblock')
{
}
return $this->data['MODULE_ID'] === 'iblock'
? ($this->getElement() ? $this->getElement()->getUrl() : false)
: $this->data['URL'];
}
public function getPath()
{
}
public function getItemID()
{
return $this->data['ITEM_ID'];
}
public function getChangeDate($format)
{
return FormatDate($format, strtotime($this->data['DATE_CHANGE']));
}
public function getBodyHighlighted($keywords = null, $wordsAround = 5, $tag = '<b>', $delimiter = ' … ', $maxMatchesNum = NULL)
{
if (!is_array($keywords))
{
$keywords = preg_replace('/<.*>/Uu', '', $keywords);
$keywords = preg_replace('/\s+/u', ' ', $keywords);
$keywords = preg_split('/\s+/u', $keywords, -1, PREG_SPLIT_NO_EMPTY);
}
$tag_close = preg_replace('/<([a-z0-9]+).*>/isx', '</$1>', $tag);
array_walk($keywords, 'preg_quote');
$words = preg_split('/\s+/u', strip_tags($this->data['BODY']), -1, PREG_SPLIT_NO_EMPTY);
if (!$matched = preg_grep("/(" . join('|', $keywords) . ")/iu", $words))
{
return false;
}
foreach ($matched as $i => $word)
{
$words[$i] = "{$tag}{$words[$i]}{$tag_close}";
}
$matches = array();
$prev = -$wordsAround * 2;
foreach ($matched as $i => $word)
{
if ($i - $wordsAround > $prev)
{
$start = $i - $wordsAround;
if ($start < 0) $start = 0;
$matches[] = join(' ', array_slice($words, $start, $wordsAround * 2 + 1));
}
$prev = $i;
}
if($maxMatchesNum)
{
$matches = array_slice($matches, 0, $maxMatchesNum);
}
return join($delimiter, $matches);
}
/**
* @return \Cpeople\Classes\Block\Object|\Cpeople\Classes\Section\Object
*/
public function getElement()
{
if($this->element === null)
{
$itemId = $this->getItemID();
$isSection = !is_numeric($itemId);
if($isSection)
{
$itemId = substr($itemId, 1);
$instance = \Cpeople\Classes\Section\Getter::instance();
}
else
{
$instance = \Cpeople\Classes\Block\Getter::instance();
}
$this->element = $instance->getById($itemId);
}
return $this->element;
}
public function __get($name)
{
if (isset($this->data[strtoupper($name)]))
{
return $this->data[strtoupper($name)];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property in __get(): ' . $name .
' in file ' . $trace[0]['file'] .
' line ' . $trace[0]['line'], E_USER_NOTICE
);
}
}