/**
* Get the excerpt of a given piece of text.
*
* @param int $length
* @param bool $includeTitle
* @param array|string|null $focus
*
* @return string|null
*/
public function getExcerpt($length = 200, $includeTitle = false, $focus = null)
{
$title = null;
if ($includeTitle && $this->title !== null) {
$title = Html::trimText(strip_tags($this->title), $length);
$length = $length - strlen($title);
}
if ($this->body instanceof Content) {
$this->body = $this->body->getValues();
}
if (is_array($this->body)) {
// Assume it's an array, strip some common fields that we don't need, implode the rest.
$stripKeys = ['id', 'slug', 'datecreated', 'datechanged', 'username', 'ownerid', 'title', 'contenttype', 'status', 'taxonomy', 'templatefields'];
foreach ($stripKeys as $key) {
unset($this->body[$key]);
}
$excerpt = implode(' ', $this->body);
} elseif (is_string($this->body) || is_object($this->body) && method_exists($this->body, '__toString')) {
// otherwise we just use the string.
$excerpt = (string) $this->body;
} else {
// Nope, got nothing.
$excerpt = '';
}
$excerpt = str_replace('>', '> ', $excerpt);
if (empty($focus)) {
$excerpt = Html::trimText(strip_tags($excerpt), $length);
} else {
$excerpt = $this->extractRelevant($focus, strip_tags($excerpt), $length);
}
if ($title !== null) {
$excerpt = '<b>' . $title . '</b> ' . $excerpt;
}
return $excerpt;
}