Completed
Push — master ( c65b2e...91a3af )
by Peter
05:35
created

SearchFilter::fromModel()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
ccs 9
cts 10
cp 0.9
rs 8.5806
cc 4
eloc 10
nc 4
nop 2
crap 4.016
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Manganel\Filters;
10
11
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
12
use Maslosoft\Mangan\Interfaces\Filters\Property\TransformatorFilterInterface;
13
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
14
use Maslosoft\Manganel\Meta\ManganelMeta;
15
16
/**
17
 * Search Filter is meant to filter out non indexable properties from model.
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
class SearchFilter implements TransformatorFilterInterface
22
{
23
24
	/**
25
	 * TODO: Should filter out:
26
	 *
27
	 * 1. Fields marked with:
28
	 * ```
29
	 * @Search(false)
30
	 * ```
31
	 * 2. Empty strings
32
	 * 3. Possibly _class fields, as it might blur search results
33
	 *
34
	 * @param AnnotatedInterface $model
35
	 * @param DocumentPropertyMeta $fieldMeta
36
	 * @return boolean
37
	 */
38 16
	public function fromModel($model, DocumentPropertyMeta $fieldMeta)
39
	{
40 16
		$name = $fieldMeta->name;
41
		// Create Manganel meta instance of field
42 16
		$meta = ManganelMeta::create($model)->field($name);
43
44
		// Skip if explicitly not searchable
45 16
		if (false === $meta->searchable)
46
		{
47 3
			return false;
48
		}
49
50
		// Skip non-persistent fields
51 16
		if (false === $meta->persistent)
52
		{
53
			return false;
54
		}
55
56
		// Skip secret fields
57 16
		if ($meta->secret)
58
		{
59 3
			return false;
60
		}
61 15
		return true;
62
	}
63
64 5
	public function toModel($model, DocumentPropertyMeta $fieldMeta)
65
	{
66 5
		return true;
67
	}
68
69
}
70