<?php

namespace Sibs;

class SibsObjectify
{
    public static function json_mapper($value, $recursive = true)
    {
        if (
            ! empty($value) && is_string($value) &&
            $decoded = json_decode($value, true)
        ) {
            return $decoded;
        } elseif (is_array($value) && $recursive) {
            return array_map('\\Sibs\\SibsObjectify::json_mapper', $value);
        } else {
            return $value;
        }
    }

    public static function json_mapper_norecurse($value)
    {
        return self::json_mapper($value, false);
    }

    public static function json_to_array($array, $recursive = true)
    {
        // if $array is not an array, let's make it array with one value of
        // former $array.
        if (! is_array($array)) {
            $array = [$array];
        }

        return array_map(
            $recursive ? '\\Sibs\\SibsObjectify::json_mapper'
                : '\\Sibs\\SibsObjectify::json_mapper_norecurse',
            $array
        );
    }
}
