Completed
Push — master ( 735cac...641d44 )
by Jean-Christophe
04:20
created

SearchCategories::fromDatabaseObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Ajax\semantic\components\search;
4
5
class SearchCategories extends AbstractSearchResult {
6
	private $categories;
7
8
	public function __construct() {
9
		$this->categories=array ();
10
	}
11
12
	public function add($results, $category) {
13
		$count=\sizeof($this->categories);
14
		if (\array_key_exists($category, $this->categories)) {
15
			$this->categories[$category]->addResults($results);
16
		} else {
17
			$categoryO=new SearchCategory("category" . $count, $category, $results);
18
			$this->categories[$category]=$categoryO;
19
		}
20
		return $this;
21
	}
22
23
	public function search($query, $field="title") {
24
		$result=array ();
25
		foreach ( $this->categories as $category ) {
26
			$r=$category->search($query, $field);
27
			if ($r !== false)
28
				$result[]=$r;
29
		}
30
		$this->categories=$result;
31
		return $this;
32
	}
33
34
	public function __toString() {
35
		return "{\"results\":{" . \implode(",", \array_values($this->categories)) . "}}";
36
	}
37
38
	public function getResponse() {
39
		return $this->__toString();
40
	}
41
42
	/**
43
	 * Loads results and categories from a collection of DB objects
44
	 * @param array $objects the collection of objects
45
	 * @param callable $function return an instance of SearchCategory
46
	 */
47
	public function fromDatabaseObjects($objects, $function) {
48
		parent::fromDatabaseObjects($objects, $function);
49
	}
50
51
	protected function fromDatabaseObject($object, $function) {
52
		$result=$function($object);
53
		if ($result instanceof SearchCategory) {
54
			$this->categories[]=$result;
55
		}
56
	}
57
}