Skip to content

Commit

Permalink
Merge pull request #626 from DannyvdSluijs/More-type-hints
Browse files Browse the repository at this point in the history
refactor: Add more native type hints
  • Loading branch information
stephangroen authored Jan 16, 2024
2 parents a6b33cd + eb8fa1b commit 2c66c28
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 131 deletions.
7 changes: 3 additions & 4 deletions src/Picqer/Financials/Exact/DocumentAttachment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Picqer\Financials\Exact;

/**
Expand Down Expand Up @@ -31,10 +33,7 @@ class DocumentAttachment extends Model

protected $url = 'documents/DocumentAttachments';

/**
* @return string
*/
public function getDownloadUrl()
public function getDownloadUrl(): string
{
return $this->Url . '&Download=1';
}
Expand Down
7 changes: 3 additions & 4 deletions src/Picqer/Financials/Exact/Item.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Picqer\Financials\Exact;

/**
Expand Down Expand Up @@ -229,10 +231,7 @@ class Item extends Model

protected $url = 'logistics/Items';

/**
* @return string
*/
public function getDownloadUrl()
public function getDownloadUrl(): string
{
return $this->PictureUrl;
}
Expand Down
86 changes: 31 additions & 55 deletions src/Picqer/Financials/Exact/Model.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<?php

declare(strict_types=1);

namespace Picqer\Financials\Exact;

/**
* Class Model.
*/
abstract class Model implements \JsonSerializable
{
/**
* @var Connection
*/
protected $connection;
protected Connection $connection;

/**
* @var array The model's attributes
*/
protected $attributes = [];
protected array $attributes = [];

/**
* @deferred array The model's collection values
*/
protected $deferred = [];
protected array $deferred = [];

/**
* @var array The model's fillable attributes
Expand All @@ -45,40 +38,34 @@ public function __construct(Connection $connection, array $attributes = [])

/**
* Get the connection instance.
*
* @return Connection
*/
public function connection()
public function connection(): Connection
{
return $this->connection;
}

/**
* Get the model's attributes.
*
* @return array
* @return array<string, mixed>
*/
public function attributes()
public function attributes(): array
{
return $this->attributes;
}

/**
* Get the model's url.
*
* @return string
*/
public function url()
public function url(): string
{
return $this->url;
}

/**
* Get the model's primary key.
*
* @return string
*/
public function primaryKey()
public function primaryKey(): string
{
return $this->primaryKey;
}
Expand All @@ -96,7 +83,7 @@ public function primaryKeyContent()
/**
* Fill the entity from an array.
*
* @param array $attributes
* @param array<string, mixed> $attributes
*/
protected function fill(array $attributes)
{
Expand All @@ -110,9 +97,9 @@ protected function fill(array $attributes)
/**
* Get the fillable attributes of an array.
*
* @param array $attributes
* @param array<string, mixed> $attributes
*
* @return array
* @return array<string, mixed>
*/
protected function fillableFromArray(array $attributes)
{
Expand All @@ -123,31 +110,30 @@ protected function fillableFromArray(array $attributes)
return $attributes;
}

protected function isFillable($key)
protected function isFillable($key): bool
{
return in_array($key, $this->fillable);
}

public function getFillable()
/**
* @return array<string, mixed>
*/
public function getFillable(): array
{
return $this->fillable;
}

protected function setAttribute($key, $value)
protected function setAttribute($key, $value): void
{
$this->attributes[$key] = $value;
}

/**
* Resolve deferred values.
*
* @param string $key
*
* @return bool Returns true when collection is found
*/
protected function lazyLoad($key)
protected function lazyLoad(string $key): bool
{
// Check previously resolved or manualy set.
// Check previously resolved or manually set.
if (isset($this->deferred[$key])) {
return true;
}
Expand All @@ -170,7 +156,7 @@ protected function lazyLoad($key)
return false;
}

public function __get($key)
public function __get(string $key)
{
if ($this->lazyLoad($key)) {
return $this->deferred[$key];
Expand All @@ -181,7 +167,7 @@ public function __get($key)
}
}

public function __set($key, $value)
public function __set(string $key, $value)
{
if ($this->isFillable($key)) {
if (is_array($value)) {
Expand All @@ -194,12 +180,12 @@ public function __set($key, $value)
}
}

public function __isset($name)
public function __isset(string $name)
{
return $this->__get($name) !== null;
}

public function __call($name, $arguments)
public function __call(string $name, $arguments)
{
return $this->__get($name);
}
Expand All @@ -220,10 +206,8 @@ public function refresh($key)

/**
* Checks if primaryKey holds a value.
*
* @return bool
*/
public function exists()
public function exists(): bool
{
if (! array_key_exists($this->primaryKey, $this->attributes)) {
return false;
Expand All @@ -234,12 +218,8 @@ public function exists()

/**
* Return the JSON representation of the data.
*
* @param int $options http://php.net/manual/en/json.constants.php
*
* @return string
*/
public function json($options = 0, $withDeferred = false)
public function json(int $options = 0, bool $withDeferred = false): string
{
$attributes = $this->attributes;
if ($withDeferred) {
Expand Down Expand Up @@ -269,30 +249,26 @@ public function json($options = 0, $withDeferred = false)
/**
* Return serializable data.
*
* @return array
* @return array<string, mixed>
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->attributes;
}

/**
* Check whether the current user has rights for an action on this endpoint
* https://start.exactonline.nl/docs/HlpRestAPIResources.aspx?SourceAction=10.
*
* @param string $action
*
* @return bool|null
*/
public function userHasRights($action = 'GET')
public function userHasRights(string $action = 'GET'): ?bool
{
$action = preg_match('/^GET|POST|PUT|DELETE$/i', $action) ? strtoupper($action) : 'GET';
$result = $this->connection()->get('users/UserHasRights', [
'endpoint' => "'{$this->url}'",
'action' => "'{$action}'",
]);

return isset($result['UserHasRights']) ? $result['UserHasRights'] : null;
return $result['UserHasRights'] ?? null;
}
}
20 changes: 6 additions & 14 deletions src/Picqer/Financials/Exact/Persistance/Downloadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@

use GuzzleHttp\Client;
use Picqer\Financials\Exact\Connection;
use Psr\Http\Message\StreamInterface;

trait Downloadable
{
/**
* @return Connection
*/
abstract public function connection();

/**
* @return string
*/
abstract public function getDownloadUrl();

/**
* @return mixed Binary representation of file
*/
public function download()
abstract public function connection(): Connection;

abstract public function getDownloadUrl(): string;

public function download(): StreamInterface
{
$client = new Client();

Expand Down
29 changes: 8 additions & 21 deletions src/Picqer/Financials/Exact/Persistance/Storable.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
<?php

declare(strict_types=1);

namespace Picqer\Financials\Exact\Persistance;

use Picqer\Financials\Exact\ApiException;
use Picqer\Financials\Exact\Connection;

trait Storable
{
/**
* @return bool
*/
abstract public function exists();
abstract public function exists(): bool;

/**
* @param array $attributes
* @param array<string, mixed> $attributes
*/
abstract protected function fill(array $attributes);

/**
* @param int $options
* @param bool $withDeferred
*
* @return string
*/
abstract public function json($options = 0, $withDeferred = false);
abstract public function json(int $options = 0, bool $withDeferred = false): string;

/**
* @return Connection
*/
abstract public function connection();
abstract public function connection(): Connection;

/**
* @return string
*/
abstract public function url();
abstract public function url(): string;

/**
* @return mixed
Expand All @@ -45,7 +32,7 @@ abstract public function primaryKeyContent();
*
* @return $this
*/
public function save()
public function save(): self
{
if ($this->exists()) {
$this->fill($this->update());
Expand Down
9 changes: 4 additions & 5 deletions src/Picqer/Financials/Exact/Query/Findable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Picqer\Financials\Exact\Query;

use Generator;
Expand All @@ -17,12 +19,9 @@ abstract protected function isFillable($key);
/**
* @return string
*/
abstract public function url();
abstract public function url(): string;

/**
* @return string
*/
abstract public function primaryKey();
abstract public function primaryKey(): string;

public function find($id)
{
Expand Down
Loading

0 comments on commit 2c66c28

Please sign in to comment.