SearchFilter::fromModel()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 9
cts 10
cp 0.9
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.016
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Filters;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
use Maslosoft\Mangan\Interfaces\Filters\Property\TransformatorFilterInterface;
17
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
18
use Maslosoft\Manganel\Meta\ManganelMeta;
19
20
/**
21
 * Search Filter is meant to filter out non indexable properties from model.
22
 *
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class SearchFilter implements TransformatorFilterInterface
26
{
27
28
	/**
29
	 * This will filter out:
30
	 *
31
	 * 1. Fields marked with:
32
	 * ```
33
	 * @Search(false)
34
	 * ```
35
	 * 2. Secret fields marked in any way with:
36
	 * ```
37
	 * @Secret
38
	 * ```
39
	 * 3. Non-persistent fields marked with:
40
	 * ```
41
	 * @Persistent(false)
42
	 * ```
43
	 *
44
	 * @param AnnotatedInterface $model
45
	 * @param DocumentPropertyMeta $fieldMeta
46
	 * @return boolean
47
	 */
48 54
	public function fromModel($model, DocumentPropertyMeta $fieldMeta)
49
	{
50 54
		$name = $fieldMeta->name;
51
		// Create Manganel meta instance of field
52 54
		$meta = ManganelMeta::create($model)->field($name);
53
54
		// Skip if explicitly not searchable
55 54
		if (false === $meta->searchable)
56
		{
57 5
			return false;
58
		}
59
60
		// Skip non-persistent fields
61 54
		if (false === $meta->persistent)
62
		{
63
			return false;
64
		}
65
66
		// Skip secret fields
67 54
		if ($meta->secret)
68
		{
69 3
			return false;
70
		}
71 53
		return true;
72
	}
73
74
	/**
75
	 * Allow any previously set fields to be set.
76
	 * @param AnnotatedInterface $model
77
	 * @param DocumentPropertyMeta $fieldMeta
78
	 * @return boolean
79
	 */
80 26
	public function toModel($model, DocumentPropertyMeta $fieldMeta)
81
	{
82 26
		return true;
83
	}
84
85
}
86