<?php

namespace api\v2\objects;
use api\v2\Base;

use function api\v2\import_common;
import_common();

/**
 * API wrapper for get_xml_hostgroup_member_objects, look into the function to learn more about the structure of the output
 */
class servicegroupmembers extends Base {
    public function authorized_for_get() {
        return true;
    }

    public function get() {
        global $request;
        $response = get_xml_servicegroup_member_objects($request);
        if(!$response || $response->recordcount == 0) {
            $response = array( "recordcount" => 0, "servicegroup" => [] );
        }
        
        // Ensure object is always an array
        $response = json_decode(json_encode($response), false);
        if (!is_array($response->servicegroup)) {
            $response->servicegroup = [$response->servicegroup];
        }
        foreach($response->servicegroup as $servicegroup) {
            if (!is_array($servicegroup->members->service)) {
                $servicegroup->members->service = [$servicegroup->members->service];
            }
        }
        return $response;
    }
}