-
Notifications
You must be signed in to change notification settings - Fork 0
/
Searches.php
140 lines (134 loc) · 4.62 KB
/
Searches.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search;
/**
* Entry point of the Search system.
*
* Use this class to conveniently create new search factories:
*
* <code>
* use Rollerworks\Component\Search\Searches;
* use Rollerworks\Component\Search\Exception\InvalidSearchConditionException;
* use Rollerworks\Component\Search\Extension\Core\Type\TextType;
* use Rollerworks\Component\Search\Extension\Core\Type\IntegerType;
* use Rollerworks\Component\Search\Extension\Core\Type\ChoiceType;
* use Rollerworks\Component\Search\Input\StringQueryInput;
*
* // The factory is reusable, you create it only once.
* $searchFactory = Searches::createSearchFactory();
*
* // Create a fieldset to inform the system about your configuration.
* // Usually you will have a FieldSet for each data structure (users, invoices, etc).
* $userFieldSet = $searchFactory->createFieldSetBuilder()
* ->add('firstName', TextType::class)
* ->add('lastName', TextType::class)
* ->add('age', IntegerType::class)
* ->add('gender', ChoiceType::class, [
* 'choices' => ['Female' => 'f', 'Male' => 'm'],
* ])
* ->getFieldSet('users');
*
* // Now lets process a simple string query.
* // Tip: the input processor is reusable.
* $inputProcessor = new StringQueryInput();
*
* try {
* // The ProcessorConfig allows to limit the amount of values, groups
* // and maximum nesting level.
* $processorConfig = new ProcessorConfig($userFieldSet);
*
* // The `process` method processes the input and produces
* // a valid SearchCondition (or throws an exception when something is wrong).
* $condition = $inputProcessor->process('firstName: sebastiaan, melany;');
* } catch (InvalidSearchConditionException $e) {
* // Each error message can be easily transformed to a localized version.
* // Read the documentation for more details.
* foreach ($e->getErrors() as $error) {
* echo (string) $error.PHP_EOL;
* }
* }
* </code>
*
* You can also add custom extensions to the search factory:
*
* <code>
* $searchFactory = Searches::createSearchFactoryBuilder();
* ->addExtension(new AcmeExtension())
* ->getSearchFactory();
* </code>
*
* If you create custom field types or type extensions, it is
* generally recommended to create your own extensions that lazily
* loads these types and type extensions. In projects where performance
* does not matter that much, you can also pass them directly to the
* search factory:
*
* <code>
* use Rollerworks\Component\Search\Searches;
*
* $searchFactory = Searches::createSearchFactoryBuilder();
* ->addType(new PersonType())
* ->addType(new PhoneNumberType())
* ->addTypeExtension(new DoctrineDbalExtension())
* ->getSearchFactory();
* </code>
*
* Tip: Field types without dependency injection don't have to be
* registered with the factory and can be loaded by there FQCN.
* Field type-extensions must always be registered.
*
* Support for the Validator component is provided by ValidatorExtension,
* located in another repository.
*
* This extension needs a Validator object to function properly:
*
* <code>
* use Rollerworks\Component\Search\Searches;
* use Rollerworks\Component\Search\Extension\Validator\ValidatorExtension;
* use Rollerworks\Component\Search\Extension\Validator\InputValidator;
* use Rollerworks\Component\Search\Input\StringQueryInput;
* use Symfony\Component\Validator\Validation;
*
* $searchFactory = Searches::createSearchFactoryBuilder();
* ->addExtension(new ValidatorExtension())
* ->getSearchFactory();
*
* $validatorBuilder = Validation::createValidatorBuilder();
* $validator = $validatorBuilder->getValidator();
*
* $inputProcessor = new StringQueryInput(new InputValidator($validator));
* </code>
*
* @author Sebastiaan Stok <[email protected]>
*/
final class Searches
{
/**
* Creates a SearchFactoryBuilder with the default configuration.
*/
public static function createSearchFactoryBuilder(): SearchFactoryBuilder
{
return new SearchFactoryBuilder();
}
/**
* Creates a new GenericSearchFactory with the default configuration.
*/
public static function createSearchFactory(): SearchFactory
{
return self::createSearchFactoryBuilder()->getSearchFactory();
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}