ChainableArray_Wip_Trait::tableize_aux()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 31
nc 9
nop 2
dl 0
loc 49
rs 8.0555
c 0
b 0
f 0
1
<?php
2
namespace JClaveau\Arrays;
3
4
/**
5
 * Functions that are not fully implemented or buggy
6
 */
7
trait ChainableArray_Wip_Trait
8
{
9
10
    /**
11
     *
12
     */
13
    public function hierarchize(array $options)
14
    {
15
        // $options = [
16
            // 'before' => [
17
                // 'action' => '.*',
18
                // 'values' => 'values|formatted_values',
19
                // 'group'  => '.*'
20
            // ],
21
            // 'after'  => [
22
                // 'group', 'action', 'values'
23
            // ],
24
        // ];
25
26
        $out = $this->tableize(
27
            array_keys($options['before'])
28
        )
29
        ->piramidize($options['after'])
30
        ->dumpJson(true)
31
        ;
32
33
        return $out;
34
    }
35
36
    /**
37
     * Transforms an array in a 2 dimension array (like a table).
38
     *
39
     * @param array $key_columns_names The names to give as column for
40
     *                                 every dimension.
41
     * @return Helper_Table
42
     */
43
    public function tableize(array $key_column_names)
44
    {
45
        $key_column_names = new Helper_Table($key_column_names);
0 ignored issues
show
Bug introduced by
The type JClaveau\Arrays\Helper_Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
47
        $out = $this->tableize_aux($key_column_names->copy(), $this->data);
48
49
        // return $this->returnConstant($out->getArray());
50
        return $this->returnConstant($out);
51
    }
52
53
    /**
54
     * Transforms an array in a 2 dimension array (like an SQL table)
55
     *
56
     *  action_calculate_paid_impressions: {
57
            values: [ ],
58
            options: {
59
                label: "Paid Impressions"
60
            },
61
            formatted_values: [ ]
62
        }
63
        *
64
        =>
65
     *  action_calculate_paid_impressions: {
66
            values: [ ],
67
            options: {
68
                label: "Paid Impressions"
69
            },
70
            formatted_values: [ ]
71
        }
72
        *
73
        *
74
        *data: [
75
            {
76
            paidImpressions: {
77
                value: 246367,
78
                formatted: "246 367"
79
            },
80
            publisherCalls: {
81
                value: 22096941,
82
                formatted: "22 096 941"
83
            },
84
*      *
85
     *
86
     *
87
     *
88
     *
89
     */
90
    private function tableize_aux($key_column_names, $rows)
91
    {
92
        $column_name = $key_column_names->shift();
93
94
        if (!$column_name)
95
            return $rows;
96
97
        // $out = new static;
98
        $out = [];
99
        foreach ($rows as $key => $sub_rows) {
100
101
            if ($key_column_names->count()) {
102
103
                $sub_rows = $this->tableize_aux(
104
                    $key_column_names->copy(),
105
                    $sub_rows
106
                );
107
108
                foreach ($sub_rows as $row) {
109
                    try {
110
                        if (is_array($row))
111
                            $row[$column_name] = $key;
112
                    }
113
                    catch (\Exception $e) {
114
                        echo json_encode($column_name);
115
                        echo json_encode($key);
116
                        echo json_encode($e);
117
                        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
118
                    }
119
                    $out[] = $row;
120
                }
121
            }
122
            else {
123
                try {
124
                    if (is_array($sub_rows))
125
                        $sub_rows[$column_name] = $key;
126
                }
127
                catch (\Exception $e) {
128
                    echo json_encode($sub_rows);
129
                    echo json_encode($column_name);
130
                    echo json_encode($key);
131
                    echo json_encode($e);
132
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
133
                }
134
                $out[] = $sub_rows;
135
            }
136
        }
137
138
        return $out;
139
    }
140
141
    /**
142
     * @todo : move to unit test
143
     */
144
    public function test_tabelize()
145
    {
146
        $values = new Helper_Table([
147
            'a' => [
148
                'a1' => [
149
                    'a2' => [
150
                        'a3' => 1,
151
                        'b3' => 1,
152
                    ],
153
                    'b2' => [
154
                        'a3' => 1,
155
                        'b3' => 1,
156
                    ],
157
                    'c2' => [
158
                        'a3' => 1,
159
                        'b3' => 1,
160
                    ],
161
                ],
162
                'b1' => [
163
                    'a2' => [
164
                        'a3' => 1,
165
                        'b3' => 1,
166
                    ],
167
                    'b2' => [
168
                        'a3' => 1,
169
                        'b3' => 1,
170
                    ],
171
                    'c2' => [
172
                        'a3' => 1,
173
                        'b3' => 1,
174
                    ],
175
                ],
176
            ],
177
            'b' => [
178
                'a1' => [
179
                    'a2' => [
180
                        'a3' => 1,
181
                        'b3' => 1,
182
                    ],
183
                    'b2' => [
184
                        'a3' => 1,
185
                        'b3' => 1,
186
                    ],
187
                    'c2' => [
188
                        'a3' => 1,
189
                        'b3' => 1,
190
                    ],
191
                ],
192
                'b1' => [
193
                    'a2' => [
194
                        'a3' => 1,
195
                        'b3' => 1,
196
                    ],
197
                    'b2' => [
198
                        'a3' => 1,
199
                        'b3' => 1,
200
                    ],
201
                    'c2' => [
202
                        'a3' => 1,
203
                        'b3' => 1,
204
                    ],
205
                ],
206
            ],
207
        ]);
208
209
        $tableized = $values->tableize([
210
            '1st',
211
            '2nd',
212
            '3rd'
213
        ]);
214
215
        $expected = array (
216
            0 => array (
217
                'a3' => 1,
218
                'b3' => 1,
219
                '3rd' => 'a2',
220
                '2nd' => 'a1',
221
                '1st' => 'a',
222
            ),
223
            1 => array (
224
                'a3' => 1,
225
                'b3' => 1,
226
                '3rd' => 'b2',
227
                '2nd' => 'a1',
228
                '1st' => 'a',
229
            ),
230
            2 => array (
231
                'a3' => 1,
232
                'b3' => 1,
233
                '3rd' => 'c2',
234
                '2nd' => 'a1',
235
                '1st' => 'a',
236
            ),
237
            3 => array (
238
                'a3' => 1,
239
                'b3' => 1,
240
                '3rd' => 'a2',
241
                '2nd' => 'b1',
242
                '1st' => 'a',
243
            ),
244
            4 => array (
245
                'a3' => 1,
246
                'b3' => 1,
247
                '3rd' => 'b2',
248
                '2nd' => 'b1',
249
                '1st' => 'a',
250
            ),
251
            5 => array (
252
                'a3' => 1,
253
                'b3' => 1,
254
                '3rd' => 'c2',
255
                '2nd' => 'b1',
256
                '1st' => 'a',
257
            ),
258
            6 => array (
259
                'a3' => 1,
260
                'b3' => 1,
261
                '3rd' => 'a2',
262
                '2nd' => 'a1',
263
                '1st' => 'b',
264
            ),
265
            7 => array (
266
                'a3' => 1,
267
                'b3' => 1,
268
                '3rd' => 'b2',
269
                '2nd' => 'a1',
270
                '1st' => 'b',
271
            ),
272
            8 => array (
273
                'a3' => 1,
274
                'b3' => 1,
275
                '3rd' => 'c2',
276
                '2nd' => 'a1',
277
                '1st' => 'b',
278
            ),
279
            9 => array (
280
                'a3' => 1,
281
                'b3' => 1,
282
                '3rd' => 'a2',
283
                '2nd' => 'b1',
284
                '1st' => 'b',
285
            ),
286
            10 => array (
287
                'a3' => 1,
288
                'b3' => 1,
289
                '3rd' => 'b2',
290
                '2nd' => 'b1',
291
                '1st' => 'b',
292
            ),
293
            11 => array (
294
                'a3' => 1,
295
                'b3' => 1,
296
                '3rd' => 'c2',
297
                '2nd' => 'b1',
298
                '1st' => 'b',
299
            ),
300
        );
301
302
        if ($expected != $tableized->getArray())
303
            throw new \Exception("test failed");
304
305
        echo 'Helper_Tabe->tableize tested successfully';
306
    }
307
308
    /**
309
     * Transforms an array in a 2 dimension array (like a table).
310
     *
311
     * @param array $key_columns_names The names to give as column for
312
     *                                 every dimension.
313
     * @return Helper_Table
314
     */
315
    public function piramidize(array $key_column_names)
316
    {
317
        $key_column_names = new Helper_Table($key_column_names);
318
319
        $out = $this->piramidize_aux($key_column_names->copy(), $this->data);
320
321
        // return $this->returnConstant($out->getArray());
322
        return $this->returnConstant($out);
0 ignored issues
show
Bug introduced by
It seems like returnConstant() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

322
        return $this->/** @scrutinizer ignore-call */ returnConstant($out);
Loading history...
323
    }
324
325
    /**
326
     * Transforms an array in a 2 dimension array (like an SQL table)
327
     */
328
    private function piramidize_aux($key_column_names, $rows)
329
    {
330
        if (!is_array($rows))
331
            return $rows;
332
333
        $column_name = $key_column_names->shift();
334
335
        if (!$column_name)
336
            return $rows;
337
338
        // $out = new static;
339
        $out = [];
340
        foreach ($rows as $key => $row) {
341
342
            if (!isset($row[$column_name])) {
343
                throw new \ErrorException("No value found for column"
344
                    ." name '$column_name' in the row: ".var_export($row, true));
345
            }
346
347
            $key = $row[$column_name];
348
            unset($row[$column_name]);
349
350
            if ($key_column_names->count()) {
351
                if (!isset($out[$key]))
352
                    $out[$key] = [];
353
354
                $out[$key][] = $row;
355
            }
356
            else {
357
                $out[$key] = $row;
358
            }
359
        }
360
361
        if ($key_column_names->count()) {
362
            foreach ($out as $key => $sub_rows) {
363
                $out[$key] = $this->piramidize_aux(
364
                    $key_column_names->copy(),
365
                    $sub_rows
366
                );
367
            }
368
        }
369
370
        return $out;
371
    }
372
373
    /**
374
     * @todo : move to unit test
375
     */
376
    public static function test_piramidize()
377
    {
378
        $values = new Helper_Table([
379
            'a' => [
380
                'a1' => [
381
                    'a2' => [
382
                        'a3' => 1,
383
                        'b3' => 1,
384
                    ],
385
                    'b2' => [
386
                        'a3' => 2,
387
                        'b3' => 2,
388
                    ],
389
                    'c2' => [
390
                        'a3' => 3,
391
                        'b3' => 3,
392
                    ],
393
                ],
394
                'b1' => [
395
                    'a2' => [
396
                        'a3' => 1,
397
                        'b3' => 1,
398
                    ],
399
                    'b2' => [
400
                        'a3' => 2,
401
                        'b3' => 2,
402
                    ],
403
                    'c2' => [
404
                        'a3' => 3,
405
                        'b3' => 3,
406
                    ],
407
                ],
408
            ],
409
            'b' => [
410
                'a1' => [
411
                    'a2' => [
412
                        'a3' => 1,
413
                        'b3' => 1,
414
                    ],
415
                    'b2' => [
416
                        'a3' => 2,
417
                        'b3' => 2,
418
                    ],
419
                    'c2' => [
420
                        'a3' => 3,
421
                        'b3' => 3,
422
                    ],
423
                ],
424
                'b1' => [
425
                    'a2' => [
426
                        'a3' => 1,
427
                        'b3' => 1,
428
                    ],
429
                    'b2' => [
430
                        'a3' => 2,
431
                        'b3' => 2,
432
                    ],
433
                    'c2' => [
434
                        'a3' => 3,
435
                        'b3' => 3,
436
                    ],
437
                ],
438
            ],
439
        ]);
440
441
        // $values->dumpJson();
442
443
        $values->tableize([
444
            '1st',
445
            '2nd',
446
            '3rd',
447
        ]);
448
449
        // $values->dumpJson(
450
            // true
451
        // );
452
453
        $values->piramidize([
454
            '3rd',
455
            '2nd',
456
            '1st',
457
        ]);
458
459
        $expected = array (
460
            'a2' => array (
461
                'a1' => array (
462
                    'a' => array (
463
                        'a3' => 1,
464
                        'b3' => 1,
465
                    ),
466
                    'b' => array (
467
                        'a3' => 1,
468
                        'b3' => 1,
469
                    ),
470
                ),
471
                'b1' => array (
472
                    'a' => array (
473
                        'a3' => 1,
474
                        'b3' => 1,
475
                    ),
476
                    'b' => array (
477
                        'a3' => 1,
478
                        'b3' => 1,
479
                    ),
480
                ),
481
            ),
482
            'b2' => array (
483
                'a1' => array (
484
                    'a' => array (
485
                        'a3' => 2,
486
                        'b3' => 2,
487
                    ),
488
                    'b' => array (
489
                        'a3' => 2,
490
                        'b3' => 2,
491
                    ),
492
                ),
493
                'b1' => array (
494
                    'a' => array (
495
                        'a3' => 2,
496
                        'b3' => 2,
497
                    ),
498
                    'b' => array (
499
                        'a3' => 2,
500
                        'b3' => 2,
501
                    ),
502
                ),
503
            ),
504
            'c2' => array (
505
                'a1' => array (
506
                    'a' => array (
507
                        'a3' => 3,
508
                        'b3' => 3,
509
                    ),
510
                    'b' => array (
511
                        'a3' => 3,
512
                        'b3' => 3,
513
                    ),
514
                ),
515
                'b1' => array (
516
                    'a' => array (
517
                        'a3' => 3,
518
                        'b3' => 3,
519
                    ),
520
                    'b' => array (
521
                        'a3' => 3,
522
                        'b3' => 3,
523
                    ),
524
                ),
525
            ),
526
        );
527
528
529
        // echo var_export($values->getArray());
530
        // $values->dumpJson(true);
531
532
        if ($expected != $values->getArray())
533
            throw new \Exception("test failed");
534
535
        echo 'Helper_Tabe->piramidize tested successfully';
536
    }
537
538
    /**/
539
}
540