<commit_before><?php
namespace Pinvoice\Pipedrive;

// Require Composer packages
require_once ('vendor/autoload.php');

// Require API objects
require_once ('Pipelines.php');

class API
{
    
    /**
     * The current API instance
     */
    private static $_instance = null;
    
    /**
     * The Pipedrive API token
     */
    private static $token = null;
    
    /**
     * The API endpoint to use
     */
    const ENDPOINT = 'http://api.pipedrive.com/v1/';

    /**
     * Returns singleton class instance
     *
     * @return object Pipedrive API instance
     */
    public static function getInstance() {
        if (self::$_instance === null) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    
    /**
     * Gets the API token
     *
     * @return string API token
     */
    public static function getToken() {
        return self::$token;
    }
    
    /**
     * Sets the API token
     *
     * @param string $token Pipedrive API token
     *
     * @return void
     */
    public static function setToken($token) {
        self::$token = $token;
    }
    
    /**
     * Handles errors for requests
     * @param array $data JSON object
     *
     * @return array
     */
    public static function safe_return($data) {
        if (!$data->success) {
            throw new \Exception(isset($data->error) ? $data->error : "Unknown error");
        } else {
            return $data->data;
        }
    }

    public static function http_get($url) {
        $curl = new \Curl\Curl();
        $curl->get(self::ENDPOINT . $url . '?api_token=' . self::getToken());
        $curl->close();
        
        return json_decode($curl->response);
    }

    public static function http_get_with_params($url) {
        $curl = new \Curl\Curl();
        $curl->get(self::ENDPOINT . $url . '&api_token=' . self::getToken());
        $curl->close();
        
        return json_decode($curl->response);
    }

    public static function http_post($url, array $args = array()) {
        $curl = new \Curl\Curl();
        $curl->post(self::ENDPOINT . $url . '?api_token=' . self::getToken(), $args);
        $curl->close();
        
        return json_decode($curl->response);
    }

    
    /**
     * Define API funcs
     */
    public static function getPipelines() {
        return Pipelines::getPipelines();
    }
    public static function getPipeline($id) {
        return Pipelines::getPipeline($id);
    }
    public static function addPipeline(array $args) {
        return Pipelines::addPipeline($args);
    }
    public static function getStages() {
        return Stages::getStages();
    }
    public static function getStagesByPipelineId($pipeline_id) {
        return Stages::getStagesByPipelineId($pipeline_id);
    }
    
    // etc...
    
    
}
?><commit_msg>Dynamic functions with namespaces :+1:<commit_after><?php
namespace Pinvoice\Pipedrive;

// Require Composer packages
require_once ('vendor/autoload.php');

class API
{

    /**
     * The current API instance
     */
    private static $_instance = null;
    
    /**
     * The Pipedrive API token
     */
    private static $token = null;
    
    /** 
     * The available API classes
     */
    private static $classes = array('Pipelines', 'Stages');

    /**
     * The API endpoint to use
     */
    const ENDPOINT = 'http://api.pipedrive.com/v1/';

    /**
     * Returns singleton class instance
     *
     * @return object Pipedrive API instance
     */
    public static function getInstance() {
        if (self::$_instance === null) {
            self::$_instance = new self;
        }   
        return self::$_instance;
    }
    
    public function __call($name, $arguments)
    {
        foreach(self::$classes as $class) {
          if (method_exists (__NAMESPACE__ . '\\' . $class, $name))
          {
            return call_user_func_array(__NAMESPACE__ . '\\' . $class . '::' . $name, $arguments);
          }
          
      }
      return null; // or error
    }

    /**
     * Gets the API token
     *
     * @return string API token
     */
    public static function getToken() {
        return self::$token;
    }
    
    /**
     * Sets the API token
     *
     * @param string $token Pipedrive API token
     *
     * @return void
     */
    public static function setToken($token) {
        self::$token = $token;
    }
    
    /**
     * Handles errors for requests
     * @param array $data JSON object
     *
     * @return array
     */
    public static function safe_return($data) {
        if (!$data->success) {
            throw new \Exception(isset($data->error) ? $data->error : "Unknown error");
        } else {
            return $data->data;
        }
    }

    public static function http_get($url) {
        $curl = new \Curl\Curl();
        $curl->get(self::ENDPOINT . $url . '?api_token=' . self::getToken());
        $curl->close();
        
        return json_decode($curl->response);
    }

    public static function http_get_with_params($url) {
        $curl = new \Curl\Curl();
        $curl->get(self::ENDPOINT . $url . '&api_token=' . self::getToken());
        $curl->close();
        
        return json_decode($curl->response);
    }

    public static function http_post($url, array $args = array()) {
        $curl = new \Curl\Curl();
        $curl->post(self::ENDPOINT . $url . '?api_token=' . self::getToken(), $args);
        $curl->close();
        
        return json_decode($curl->response);
    }
    
}
?>
{'fraction_non_alphanumeric': 0.13888387213292397, 'fraction_numerical': 0.000541809644211667, 'mean_word_length': 1.911671924290221, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3769083', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1648, 'n_words': 392}