4 * Serialize for RuleTransactions / Editor.
6 final class HeraldRuleSerializer
extends Phobject
{
7 public function serializeRule(HeraldRule
$rule) {
8 return $this->serializeRuleComponents(
9 (bool)$rule->getMustMatchAll(),
10 $rule->getConditions(),
12 $rule->getRepetitionPolicyStringConstant());
15 public function serializeRuleComponents(
21 assert_instances_of($conditions, 'HeraldCondition');
22 assert_instances_of($actions, 'HeraldActionRecord');
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(),
33 $actions_array = array();
34 foreach ($actions as $action) {
35 $actions_array[] = array(
36 'action' => $action->getAction(),
37 'target' => $action->getTarget(),
42 'match_all' => $match_all,
43 'conditions' => $conditions_array,
44 'actions' => $actions_array,
45 'repetition_policy' => $repetition_policy,
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']);
58 $deser_actions = array();
59 foreach ($serialized['actions'] as $action) {
60 $deser_actions[] = id(new HeraldActionRecord())
61 ->setAction($action['action'])
62 ->setTarget($action['target']);
66 'match_all' => $serialized['match_all'],
67 'conditions' => $deser_conditions,
68 'actions' => $deser_actions,
69 'repetition_policy' => $serialized['repetition_policy'],