From 4be82315c57429460c938ce1e1b04cfd94ba2340 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 10 May 2023 17:54:46 +0900 Subject: [PATCH] Capture status code 0 on API updates, pluck() requires 2 args, enable adding attributes to new models --- src/API.php | 5 +++++ src/Response/ListList.php | 5 ++++- src/Traits/CompareAttributes.php | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/API.php b/src/API.php index ce96a5e..f3f0d39 100644 --- a/src/API.php +++ b/src/API.php @@ -122,6 +122,7 @@ final class API $response = curl_exec($request); switch($x=curl_getinfo($request,CURLINFO_HTTP_CODE)) { + case 0: case 400: case 401: case 403: @@ -132,6 +133,10 @@ final class API } curl_close($request); + + if (! $response) + throw new \Exception(sprintf('%s:Request to [%s] returned no data?',self::LOGKEY,$url),['code'=>$x]); + return json_decode(self::CURLOPT_HEADER ? substr($response,curl_getinfo($request,CURLINFO_HEADER_SIZE)) : $response); } catch (\Exception $e) { diff --git a/src/Response/ListList.php b/src/Response/ListList.php index 82b7606..bfdc666 100644 --- a/src/Response/ListList.php +++ b/src/Response/ListList.php @@ -63,7 +63,10 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator case 'first': return $this->_data->{$name}(); case 'pluck': - return $this->_data->map(function($item) use ($args) { return $item->{$args[0]}; }); + if (count($args) !== 2) + throw new \BadMethodCallException(sprintf('Pluck requires to arguments %s::%s()',get_class($this),$name)); + + return $this->_data->map(function($item) use ($args) { return [$item->{$args[1]} => $item->{$args[0]}]; })->flatMap(function($item) { return $item; }); default: throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()',get_class($this),$name)); diff --git a/src/Traits/CompareAttributes.php b/src/Traits/CompareAttributes.php index 0041644..18b3893 100644 --- a/src/Traits/CompareAttributes.php +++ b/src/Traits/CompareAttributes.php @@ -29,7 +29,7 @@ trait CompareAttributes * @return void */ public function __set($key,$value) { - if (array_key_exists($key,$this->attributes) && (! $this->isSame($key,$value))) + if (! array_key_exists($key,$this->attributes) || (! $this->isSame($key,$value))) parent::__set($key,$value); }