summaryrefslogtreecommitdiffstats
path: root/src/PCurlException.php
blob: 3c4580dc7eb8daa4e66a2186162fa19332ac094f (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
<?php

namespace PurpleCode;

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);
  }

}