1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\Operation;
13
14 use ICanBoogie\Operation;
15 use ICanBoogie\PropertyNotDefined;
16
17 18 19 20 21
22 class Failure extends \ICanBoogie\HTTP\HTTPError
23 {
24 private $operation;
25
26 public function __construct(Operation $operation, \Exception $previous=null)
27 {
28 $this->operation = $operation;
29
30 $message = $this->format_message($operation);
31 $code = $operation->response->status;
32
33 parent::__construct($message, $code, $previous);
34 }
35
36 protected function format_message(Operation $operation)
37 {
38 $message = $operation->response->message ?: "The operation failed.";
39
40 if ($operation->response->errors->count())
41 {
42 $message .= "\n\nThe following errors where raised:";
43
44 foreach ($operation->response->errors as $id => $error)
45 {
46 if ($error === true)
47 {
48 continue;
49 }
50
51 $message .= "\n– $error";
52 }
53 }
54
55 return $message;
56 }
57
58 public function __get($property)
59 {
60 if ($property == 'operation')
61 {
62 return $this->operation;
63 }
64
65 throw new PropertyNotDefined(array($property, $this));
66 }
67 }