This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSearchProcessor.php
76 lines (66 loc) · 2.32 KB
/
AbstractSearchProcessor.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
<?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\Processor;
use Rollerworks\Component\Search\SearchFactory;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface as PropertyAccessor;
/**
* AbstractCacheSearchProcessor provides the basic logic for all processors.
*
* @author Sebastiaan Stok <[email protected]>
*/
abstract class AbstractSearchProcessor implements SearchProcessor
{
protected $propertyAccessor;
protected $searchFactory;
/**
* Constructor.
*
* @param SearchFactory $searchFactory
* @param PropertyAccessor $propertyAccessor
*/
public function __construct(SearchFactory $searchFactory, PropertyAccessor $propertyAccessor = null)
{
$this->searchFactory = $searchFactory;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}
/**
* Gets a Request object value from the request.
*
* @param array $parameters
* @param ProcessorConfig $config
* @param string $name
* @param string|array|null $default
* @param string $type Eg. string or array
*
* @return array|null|string
*/
final protected function getRequestParam(array $parameters, ProcessorConfig $config, string $name, $default = null, string $type = null)
{
if ($prefix = $config->getRequestPrefix()) {
$name = "[{$prefix}][{$name}]";
}
if (false === strpos($name, '[')) {
return $parameters[$name] ?? $default;
}
try {
$value = $this->propertyAccessor->getValue($parameters, $name) ?? $default;
if (null !== $type && false === ('is_'.$type)($value)) {
return $default;
}
return $value;
} catch (AccessException | UnexpectedTypeException $e) {
return $default;
}
}
}