Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 90 additions & 75 deletions src/Database/Barry/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static function retrieveAndDelete(
}

if ($model instanceof Collection) {
$model->dropAll();
$model->delete();
return $model;
}

Expand Down Expand Up @@ -904,56 +904,7 @@ public function __get(string $name): mixed
return null;
}

if (in_array($name, $this->mutableDateAttributes())) {
return new Carbon($this->attributes[$name]);
}

if (array_key_exists($name, $this->casts)) {
$type = $this->casts[$name];
$value = $this->attributes[$name];
if ($type === "date") {
return new Carbon($value);
}
if ($type === "int") {
return (int)$value;
}
if ($type === "float") {
return (float)$value;
}
if ($type === "double") {
return (double)$value;
}
if ($type === "json") {
if (is_array($value)) {
return (object)$value;
}
if (is_object($value)) {
return (object)$value;
}
return json_decode(
$value,
false,
512,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
);
}
if ($type === "array") {
if (is_array($value)) {
return (array)$value;
}
if (is_object($value)) {
return (array)$value;
}
return json_decode(
$value,
true,
512,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
);
}
}

return $this->attributes[$name];
return $this->executeDataCasting($name);
}

/**
Expand All @@ -968,28 +919,29 @@ public function __set(string $name, mixed $value)
}

/**
* Lists of mutable properties
* __toString
*
* @return array
* @return string
*/
private function mutableDateAttributes(): array
public function __toString(): string
{
return array_merge(
$this->dates,
[
$this->created_at, $this->updated_at, 'expired_at', 'logged_at', 'signed_at'
]
);
foreach ($this->attributes as $name => $value) {
$this->attributes[$name] = $this->executeDataCasting($name);
}

return $this->toJson();
}

/**
* __toString
* Lists of mutable properties
*
* @return string
* @return array
*/
public function __toString(): string
private function mutableDateAttributes(): array
{
return $this->toJson();
return array_merge($this->dates, [
$this->created_at, $this->updated_at, 'expired_at', 'logged_at', 'signed_at'
]);
}

/**
Expand All @@ -999,13 +951,11 @@ public function __toString(): string
*/
public function toJson(): string
{
$data = array_filter(
$this->attributes,
function ($key) {
return !in_array($key, $this->hidden);
},
ARRAY_FILTER_USE_KEY
);
foreach ($this->attributes as $name => $value) {
$this->attributes[$name] = $this->executeDataCasting($name);
}

$data = array_filter($this->attributes, fn ($key) => !in_array($key, $this->hidden), ARRAY_FILTER_USE_KEY);

return json_encode($data);
}
Expand All @@ -1025,9 +975,74 @@ public function __call(string $name, array $arguments = [])
return call_user_func_array([$model, $name], $arguments);
}

throw new BadMethodCallException(
'method ' . $name . ' is not defined.',
E_ERROR
);
throw new BadMethodCallException('Method ' . $name . ' is not defined.', E_ERROR);
}

/**
* Executes data casting for a given attribute name
*
* @param string $name
* @return mixed
*/
private function executeDataCasting(string $name): mixed
{
if (in_array($name, $this->mutableDateAttributes())) {
return new Carbon($this->attributes[$name]);
}

if (!array_key_exists($name, $this->casts)) {
return $this->attributes[$name];
}

$type = $this->casts[$name];
$value = $this->attributes[$name];

if ($type === "date") {
return new Carbon($value);
}

if ($type === "int") {
return (int)$value;
}

if ($type === "float") {
return (float)$value;
}

if ($type === "double") {
return (float)$value;
}

if ($type === "json") {
if (is_array($value)) {
return (object)$value;
}
if (is_object($value)) {
return (object)$value;
}
return json_decode(
$value,
false,
512,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
);
}

if ($type === "array") {
if (is_array($value)) {
return (array) $value;
}
if (is_object($value)) {
return (array) $value;
}
return json_decode(
$value,
true,
512,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
);
}

return $this->attributes[$name];
}
}
119 changes: 119 additions & 0 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ class HttpClient
*/
private ?string $base_url = null;

/**
* The request timeout in seconds
*
* @var int|null
*/
private ?int $timeout = null;

/**
* The connection timeout in seconds
*
* @var int|null
*/
private ?int $connect_timeout = null;

/**
* Whether to verify SSL certificates
*
* @var bool
*/
private bool $verify_ssl = true;

/**
* HttpClient Constructor.
*
Expand Down Expand Up @@ -121,6 +142,19 @@ private function applyCommonOptions(): void
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);

if ($this->timeout !== null) {
curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeout);
}

if ($this->connect_timeout !== null) {
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
}

if (!$this->verify_ssl) {
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
}
}

/**
Expand Down Expand Up @@ -311,4 +345,89 @@ public function withHeaders(array $headers): HttpClient

return $this;
}

/**
* Set HTTP authentication credentials
*
* @param string $username
* @param string $password
* @return HttpClient
*/
public function auth(string $username, string $password): HttpClient
{
curl_setopt($this->ch, CURLOPT_USERPWD, $username . ":" . $password);

return $this;
}

/**
* Set Basic HTTP authentication
*
* @param string $key
* @param string $secret
* @return HttpClient
*/
public function basicAuth(string $key, string $secret): HttpClient
{
$this->withHeaders([
'Authorization' => 'Basic ' . base64_encode($key . ':' . $secret)
]);

return $this;
}

/**
* Set Bearer token authentication
*
* @param string $token
* @return HttpClient
*/
public function bearerAuth(string $token): HttpClient
{
$this->withHeaders([
'Authorization' => 'Bearer ' . $token
]);

return $this;
}

/**
* Set the maximum time the request is allowed to take
*
* @param int $seconds
* @return HttpClient
*/
public function timeout(int $seconds): HttpClient
{
$this->timeout = $seconds;

return $this;
}

/**
* Set the maximum time to wait for a connection
*
* @param int $seconds
* @return HttpClient
*/
public function connectTimeout(int $seconds): HttpClient
{
$this->connect_timeout = $seconds;

return $this;
}

/**
* Disable SSL certificate verification
*
* Warning: This should only be used in development environments
*
* @return HttpClient
*/
public function disableSslVerification(): HttpClient
{
$this->verify_ssl = false;

return $this;
}
}
Loading
Loading