forked from sstok/gush-experiments-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchConditionSerializer.php
99 lines (87 loc) · 3.24 KB
/
SearchConditionSerializer.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
<?php
/*
* 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\InvalidArgumentException;
/**
* SearchConditionSerializer, serializes a search condition for persistent storage.
*
* In practice this serializes the root ValuesGroup and of the condition
* and bundles it with the FieldSet-name for future unserializing.
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
class SearchConditionSerializer
{
/**
* @var FieldSetRegistry
*/
private $fieldSetRegistry;
/**
* Constructor.
*
* @param FieldSetRegistryInterface $fieldSetRegistry A FieldSet registry for loading
* FieldSet configurations
*/
public function __construct(FieldSetRegistryInterface $fieldSetRegistry)
{
$this->fieldSetRegistry = $fieldSetRegistry;
}
/**
* Serialize a search condition.
*
* The returned value is an array, you should serialize it yourself.
* This is not done already because storing a serialized SearchCondition
* in a php session would serialize the serialized result again.
*
* @param SearchConditionInterface $searchCondition
*
* @throws InvalidArgumentException when the FieldSet of the search condition
* is not registered in the FieldSetRegistry
*
* @return array [FieldSet-name, serialized ValuesGroup object]
*/
public function serialize(SearchConditionInterface $searchCondition)
{
$setName = $searchCondition->getFieldSet()->getSetName();
if (!$this->fieldSetRegistry->has($setName)) {
throw new InvalidArgumentException(
sprintf(
'FieldSet "%s" is not registered in the FieldSetRegistry, '.
'you should register the FieldSet before serializing the search condition.',
$setName
)
);
}
return [$setName, serialize($searchCondition->getValuesGroup())];
}
/**
* Unserialize a serialized search condition.
*
* @param array $searchCondition [FieldSet-name, serialized ValuesGroup object]
*
* @throws InvalidArgumentException when serialized SearchCondition is invalid
* (invalid structure or failed to unserialize)
*
* @return SearchCondition
*/
public function unserialize($searchCondition)
{
if (2 !== count($searchCondition) || !isset($searchCondition[0], $searchCondition[1])) {
throw new InvalidArgumentException(
'Serialized search condition must be exactly two values [FieldSet-name, serialized ValuesGroup].'
);
}
$fieldSet = $this->fieldSetRegistry->get($searchCondition[0]);
if (false === $group = unserialize($searchCondition[1])) {
throw new InvalidArgumentException('Unable to unserialize invalid value.');
}
return new SearchCondition($fieldSet, $group);
}
}