phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
SearchResults.php
Go to the documentation of this file.
1<?php
2
4
6
7class SearchResults extends AbstractSearchResult implements ISearch {
8 private $elements;
9
10 public function __construct($objects=NULL) {
11 $this->elements=array ();
12 if (isset($objects)) {
13 if (\is_array($objects)) {
14 $this->addResults($objects);
15 } else {
16 $this->addResult($objects);
17 }
18 }
19 }
20
21 public function addResult($object) {
22 if ($object instanceof SearchResult) {
23 $this->elements[]=$object;
24 return $this;
25 }
26 if (\is_array($object) === false) {
27 $object=[ "title" => $object ];
28 }
29 $this->elements[]=new SearchResult($object);
30 return $this;
31 }
32
33 public function addResults($objects) {
34 if (!\is_array($objects)) {
35 return $this->addResult($objects);
36 }
37 if (JArray::dimension($objects) === 1) {
38 foreach ( $objects as $object ) {
39 $this->addResult([ "title" => $object ]);
40 }
41 } else
42 $this->elements=\array_merge($this->elements, $objects);
43 return $this;
44 }
45
46 public function _search($query, $field="title") {
47 $result=array ();
48 foreach ( $this->elements as $element ) {
49 if ($element instanceof SearchResult) {
50 if ($element->search($query, $field) !== false)
51 $result[]=$element->asArray();
52 } else {
53 if (\array_key_exists($field, $element)) {
54 $value=$element[$field];
55 if (\stripos($value, $query) !== false) {
56 $result[]=$element;
57 }
58 }
59 }
60 }
61 if (\sizeof($result) > 0) {
62 return $result;
63 }
64 return false;
65 }
66
67 public function search($query, $field="title") {
68 $result=$this->_search($query, $field);
69 if ($result === false)
70 $result=NULL;
71 return new SearchResults($result);
72 }
73
74 public function __toString() {
75 $result="\"results\": " . \json_encode($this->elements);
76 return $result;
77 }
78
79 public function count() {
80 return \sizeof($this->elements);
81 }
82
83 public function getResponse() {
84 return "{" . $this . "}";
85 }
86
92 public function fromDatabaseObjects($objects, $function) {
93 parent::fromDatabaseObjects($objects, $function);
94 }
95
96 protected function fromDatabaseObject($object, $function) {
97 $this->addResult($function($object));
98 }
99}
fromDatabaseObjects($objects, $function)
Loads results from a collection of DB objects.