<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Ci_view_trait.php';

/**
 * Description of Ci
 *
 * @author andhy
 */
class Ci {

    use Ci_view_trait;

    public static function call(string $module, $object) {
        $CI = ancillary_object();
        $CI->load->$module($object);
        if (is_string($object)) {
            if (strpos($object, '/') !== false) {
                $object = substr($object, strrpos($object, '/') + 1, strlen($object));
            }
        }
        return is_array($object) ? $CI : $CI->$object;
    }

    public static function models(array $models) {

        $called_models = self::call('model', $models);
        $class_lists = array_keys(get_object_vars($called_models));

        foreach ($class_lists as $class) {
            if (!in_array($class, $models)) {
                unset($called_models->$class);
            }
        }

        return $called_models;
    }

    public static function model(string $model) {
        return self::call('model', $model);
    }

    public static function library(string $library) {
        return self::call('library', $library);
    }

    public static function helper(string $helper) {
        self::call('helper', [$helper]);
    }

    public static function helpers(array $helper) {
        self::call('helper', $helper);
    }

    public static function post($key = null, $default = null, $can_null = false, $xss_clean = true) {
        $CI = ancillary_object();
        $post = $CI->input->post($key, $xss_clean);
        if((!isset($_POST[$key]) || empty($_POST[$key])) && $default != null){
            $_POST[$key] = $default;
            $post = $default;
        }
        return $post;
    }

    public static function get($key = null, $default = null, $can_null = false, $xss_clean = true) {
        $CI = ancillary_object();
        $get = $CI->input->get($key, $xss_clean);
        if((!isset($_GET[$key]) || empty($_GET[$key])) && $default != null){
            $_GET[$key] = $default;
            $get = $default;
        }
        return $get;
    }

}
