summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl/PCurlException.php
blob: ccd9269c33031e82ba31087f9caad1d2104d726f (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

/**
 * PCurl is a REST client libary for PHP.
 *
 * See http://github.com/purplecode/php.curl for details.
 *
 * This code is licensed for use, modification, and distribution
 * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
 */

namespace PurpleCode\PCurl;

class PCurlException extends \Exception
{

    private $httpCode;

    public static function getClassName()
    {
        return get_called_class();
    }

    public static function assert($condition, $message, $arguments = array(), $code = 400)
    {
        if (!$condition) {
            $arguments = array_map('json_encode', self::ensureArray($arguments));
            $arguments = array_merge(array($message), $arguments);
            $message = call_user_func_array('sprintf', $arguments);
            $class = self::getClassName();
            throw new $class($message, $code);
        }
    }

    private static function ensureArray($item)
    {
        if (is_null($item)) {
            return array();
        }
        return is_array($item) ? $item : array($item);
    }

    public function __construct($message = "Exception", $httpCode = 400, $curlErrorCode = CURLE_OK, $previous = null)
    {
        parent::__construct($message, $curlErrorCode, $previous);
        $this->httpCode = $httpCode;
    }

    public function getHttpCode(){
        return $this->httpCode;
    }

}