-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericFieldSet.php
93 lines (76 loc) · 2.16 KB
/
GenericFieldSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search;
use Rollerworks\Component\Search\Exception\UnknownFieldException;
use Rollerworks\Component\Search\Field\FieldConfig;
use Rollerworks\Component\Search\Field\OrderField;
/**
* A FieldSet holds all the search fields and there configuration.
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
final class GenericFieldSet implements FieldSetWithView
{
private $fields = [];
private $name;
private $viewBuilder;
/**
* Constructor.
*
* @param FieldConfig[] $fields
* @param string|null $name FQCN of the FieldSet configurator
* @param callable $viewBuilder A callable to finalize the FieldSetView
*/
public function __construct(array $fields, ?string $name = null, ?callable $viewBuilder = null)
{
$this->fields = $fields;
$this->name = $name;
$this->viewBuilder = $viewBuilder;
}
public function getSetName(): ?string
{
return $this->name;
}
public function get(string $name): FieldConfig
{
if (! isset($this->fields[$name])) {
throw new UnknownFieldException($name);
}
return $this->fields[$name];
}
public function all(): array
{
return $this->fields;
}
public function has(string $name): bool
{
return isset($this->fields[$name]);
}
public function isOrder(string $name): bool
{
return OrderField::isOrder($name);
}
public function isPrivate(string $name): bool
{
return $name[0] === '_';
}
public function createView(): FieldSetView
{
$view = new FieldSetView();
foreach ($this->fields as $name => $field) {
$view->fields[$name] = $field->createView($view);
}
if ($this->viewBuilder !== null) {
\call_user_func($this->viewBuilder, $view);
}
return $view;
}
}