Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/herald/editor/HeraldRuleSerializer.php
12256 views
1
<?php
2
3
/**
4
* Serialize for RuleTransactions / Editor.
5
*/
6
final class HeraldRuleSerializer extends Phobject {
7
public function serializeRule(HeraldRule $rule) {
8
return $this->serializeRuleComponents(
9
(bool)$rule->getMustMatchAll(),
10
$rule->getConditions(),
11
$rule->getActions(),
12
$rule->getRepetitionPolicyStringConstant());
13
}
14
15
public function serializeRuleComponents(
16
$match_all,
17
array $conditions,
18
array $actions,
19
$repetition_policy) {
20
21
assert_instances_of($conditions, 'HeraldCondition');
22
assert_instances_of($actions, 'HeraldActionRecord');
23
24
$conditions_array = array();
25
foreach ($conditions as $condition) {
26
$conditions_array[] = array(
27
'field' => $condition->getFieldName(),
28
'condition' => $condition->getFieldCondition(),
29
'value' => $condition->getValue(),
30
);
31
}
32
33
$actions_array = array();
34
foreach ($actions as $action) {
35
$actions_array[] = array(
36
'action' => $action->getAction(),
37
'target' => $action->getTarget(),
38
);
39
}
40
41
return array(
42
'match_all' => $match_all,
43
'conditions' => $conditions_array,
44
'actions' => $actions_array,
45
'repetition_policy' => $repetition_policy,
46
);
47
}
48
49
public function deserializeRuleComponents(array $serialized) {
50
$deser_conditions = array();
51
foreach ($serialized['conditions'] as $condition) {
52
$deser_conditions[] = id(new HeraldCondition())
53
->setFieldName($condition['field'])
54
->setFieldCondition($condition['condition'])
55
->setValue($condition['value']);
56
}
57
58
$deser_actions = array();
59
foreach ($serialized['actions'] as $action) {
60
$deser_actions[] = id(new HeraldActionRecord())
61
->setAction($action['action'])
62
->setTarget($action['target']);
63
}
64
65
return array(
66
'match_all' => $serialized['match_all'],
67
'conditions' => $deser_conditions,
68
'actions' => $deser_actions,
69
'repetition_policy' => $serialized['repetition_policy'],
70
);
71
}
72
73
}
74
75