| Total Complexity | 10 |
| Total Lines | 86 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | class Response |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The HTTP status code. |
||
| 12 | * |
||
| 13 | * @var int |
||
| 14 | */ |
||
| 15 | protected $status; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The errors on response. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $errors = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create new JsonApi response passing the errors. |
||
| 26 | * |
||
| 27 | * @param mixed $errors |
||
| 28 | */ |
||
| 29 | public function __construct($errors) |
||
| 30 | { |
||
| 31 | if ($errors instanceof ErrorCollection) { |
||
| 32 | $this->errors = $errors; |
||
|
|
|||
| 33 | } elseif (is_array($errors) || $errors instanceof Collection) { |
||
| 34 | $this->errors = new ErrorCollection($errors); |
||
| 35 | } elseif ($errors instanceof Error) { |
||
| 36 | $this->errors = (new ErrorCollection)->push($errors); |
||
| 37 | $this->errors->setStatusCode($errors->getStatus()); |
||
| 38 | } |
||
| 39 | |||
| 40 | if (! $this->errors instanceof ErrorCollection) { |
||
| 41 | throw new InvalidArgumentException('The errors must be an array, '.Collection::class.','.Error::class.' or '.ErrorCollection::class.'.'); |
||
| 42 | } |
||
| 43 | |||
| 44 | $this->errors->validate(); |
||
| 45 | |||
| 46 | $this->setStatus($this->errors->getStatusCode()); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the HTTP status code. |
||
| 51 | * |
||
| 52 | * @return int |
||
| 53 | */ |
||
| 54 | public function getStatus() |
||
| 55 | { |
||
| 56 | return $this->status; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set the HTTP status code. |
||
| 61 | * |
||
| 62 | * @param int $status The HTTP status code. |
||
| 63 | * |
||
| 64 | * @return self |
||
| 65 | */ |
||
| 66 | public function setStatus(int $status) |
||
| 67 | { |
||
| 68 | $this->status = $status; |
||
| 69 | |||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the errors on response. |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getErrors() |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Convert the object to its JSON representation. |
||
| 85 | * |
||
| 86 | * @param int $options |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function json() |
||
| 97 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..