Capture status code 0 on API updates, pluck() requires 2 args, enable adding attributes to new models

This commit is contained in:
Deon George 2023-05-10 17:54:46 +09:00
parent 0076ecef31
commit 4be82315c5
3 changed files with 10 additions and 2 deletions

View File

@ -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) {

View File

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

View File

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