summaryrefslogtreecommitdiffstats
path: root/Twilio/Serialize.php
blob: 077ddbbf8f36064380c14c873f81b46137c9de62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace Twilio;

class Serialize {

    private static function flatten($map, $result = array(), $previous = array()) {
        foreach ($map as $key => $value) {
            if (is_array($value)) {
                $result = self::flatten($value, $result, array_merge($previous, array($key)));
            } else {
                $result[join(".", array_merge($previous, array($key)))] = $value;
            }
        }

        return $result;
    }

    public static function prefixedCollapsibleMap($map, $prefix) {
        if (is_null($map) || $map == \Twilio\Values::NONE) {
            return array();
        }

        $flattened = self::flatten($map);
        $result = array();
        foreach ($flattened as $key => $value) {
            $result[$prefix . '.' . $key] = $value;
        }

        return $result;
    }

}