Code Duplication    Length = 45-45 lines in 2 locations

src/Rule/AndRule.php 1 location

@@ 121-165 (lines=45) @@
118
     *
119
     * @todo same as OrRule
120
     */
121
    public function toArray(array $options=[])
122
    {
123
        $default_options = [
124
            'show_instance' => false,
125
            'sort_operands' => false,
126
            'semantic'      => false,
127
        ];
128
        foreach ($default_options as $default_option => &$default_value) {
129
            if ( ! isset($options[ $default_option ])) {
130
                $options[ $default_option ] = $default_value;
131
            }
132
        }
133
134
        if ( ! $options['show_instance'] && ! empty($this->cache['array'])) {
135
            return $this->cache['array'];
136
        }
137
138
        $operands_as_array = [
139
            $options['show_instance'] ? $this->getInstanceId() : self::operator,
140
        ];
141
142
        $operands = $this->operands;
143
        if ($options['semantic']) {
144
            // Semantic array: ['operator', 'semantic_id_of_operand1', 'semantic_id_of_operand2', ...]
145
            // with sorted semantic ids
146
            $operands_semantic_ids = array_keys($operands);
147
            sort($operands_semantic_ids);
148
            return array_merge(
149
                [self::operator],
150
                $operands_semantic_ids
151
            );
152
        }
153
        else {
154
            foreach ($operands as $operand) {
155
                $operands_as_array[] = $operand->toArray($options);
156
            }
157
158
            if ( ! $options['show_instance']) {
159
                return $this->cache['array'] = $operands_as_array;
160
            }
161
            else {
162
                return $operands_as_array;
163
            }
164
        }
165
    }
166
167
    /**
168
     * Generates a string description of the rule.

src/Rule/OrRule.php 1 location

@@ 84-128 (lines=45) @@
81
     *
82
     * @return array
83
     */
84
    public function toArray(array $options=[])
85
    {
86
        $default_options = [
87
            'show_instance' => false,
88
            'sort_operands' => false,
89
            'semantic'      => false,
90
        ];
91
        foreach ($default_options as $default_option => &$default_value) {
92
            if ( ! isset($options[ $default_option ])) {
93
                $options[ $default_option ] = $default_value;
94
            }
95
        }
96
97
        if ( ! $options['show_instance'] && ! empty($this->cache['array'])) {
98
            return $this->cache['array'];
99
        }
100
101
        $operands_as_array = [
102
            $options['show_instance'] ? $this->getInstanceId() : self::operator,
103
        ];
104
105
        $operands = $this->operands;
106
        if ($options['semantic']) {
107
            // Semantic array: ['operator', 'semantic_id_of_operand1', 'semantic_id_of_operand2', ...]
108
            // with sorted semantic ids
109
            $operands_semantic_ids = array_keys($operands);
110
            sort($operands_semantic_ids);
111
            return array_merge(
112
                [self::operator],
113
                $operands_semantic_ids
114
            );
115
        }
116
        else {
117
            foreach ($operands as $operand) {
118
                $operands_as_array[] = $operand->toArray($options);
119
            }
120
121
            if ( ! $options['show_instance']) {
122
                return $this->cache['array'] = $operands_as_array;
123
            }
124
            else {
125
                return $operands_as_array;
126
            }
127
        }
128
    }
129
130
    /**
131
     */