@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Serialize for RuleTransactions / Editor.
5 */
6final 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 /**
16<<<<<<< HEAD
17 * @param $match_all
18 * @param array<HeraldCondition> $conditions
19 * @param array<HeraldActionRecord> $actions
20 * @param string $repetition_policy One of the HeraldRule::REPEAT_ constants
21 * @return array
22 */
23 public function serializeRuleComponents(
24 $match_all,
25 array $conditions,
26 array $actions,
27 $repetition_policy) {
28
29 assert_instances_of($conditions, HeraldCondition::class);
30 assert_instances_of($actions, HeraldActionRecord::class);
31
32 $conditions_array = array();
33 foreach ($conditions as $condition) {
34 $conditions_array[] = array(
35 'field' => $condition->getFieldName(),
36 'condition' => $condition->getFieldCondition(),
37 'value' => $condition->getValue(),
38 );
39 }
40
41 $actions_array = array();
42 foreach ($actions as $action) {
43 $actions_array[] = array(
44 'action' => $action->getAction(),
45 'target' => $action->getTarget(),
46 );
47 }
48
49 return array(
50 'match_all' => $match_all,
51 'conditions' => $conditions_array,
52 'actions' => $actions_array,
53 'repetition_policy' => $repetition_policy,
54 );
55 }
56
57 public function deserializeRuleComponents(array $serialized) {
58 $deser_conditions = array();
59 foreach ($serialized['conditions'] as $condition) {
60 $deser_conditions[] = id(new HeraldCondition())
61 ->setFieldName($condition['field'])
62 ->setFieldCondition($condition['condition'])
63 ->setValue($condition['value']);
64 }
65
66 $deser_actions = array();
67 foreach ($serialized['actions'] as $action) {
68 $deser_actions[] = id(new HeraldActionRecord())
69 ->setAction($action['action'])
70 ->setTarget($action['target']);
71 }
72
73 return array(
74 'match_all' => $serialized['match_all'],
75 'conditions' => $deser_conditions,
76 'actions' => $deser_actions,
77 'repetition_policy' => $serialized['repetition_policy'],
78 );
79 }
80
81}