summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl/PCurlException.php
blob: abed40a63e18b40be4bbb755a097c88883d3e3b2 (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
<?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 {

  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', ArrayUtils::ensureArray($arguments));
      $arguments = array_merge(array($message), $arguments);
      $message = call_user_func_array('sprintf', $arguments);
      $class = self::getClassName();
      throw new $class($message, $code);
    }
  }

  public function __construct($message = "Exception", $code = 400, $previous = null) {
    parent::__construct($message, $code, $previous);
  }

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

}