Skip to content

Commit

Permalink
Merge pull request #20 from xray-labs/task/add-function-namespace
Browse files Browse the repository at this point in the history
[Task] add function namespace
  • Loading branch information
sjspereira authored Sep 28, 2024
2 parents 10d60e7 + 0807003 commit 46c590d
Show file tree
Hide file tree
Showing 27 changed files with 373 additions and 289 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"Xray\\AzureStoragePhpSdk\\": "src/"
},
"files": [
"src/helpers.php"
"src/helpers.php",
"src/Support/functions.php"
]
},
"autoload-dev": {
Expand Down
236 changes: 118 additions & 118 deletions composer.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Xray\AzureStoragePhpSdk\Exceptions\Authentication\InvalidAuthenticationMethodException;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidResourceTypeException;

use function Xray\AzureStoragePhpSdk\Support\convert_to_ISO;

class UserDelegationSas implements SharedAccessSignature
{
/** @var array<string, string> */
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Account/AccountInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DateTimeImmutable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

final readonly class AccountInformation
{
public string $server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Xray\AzureStoragePhpSdk\Contracts\Arrayable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type DeleteRetentionPolicyType array{Enabled?: bool, AllowPermanentDelete?: bool, Days?: int}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Xray\AzureStoragePhpSdk\Contracts\Arrayable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type RetentionPolicyType array{Days?: int, Enabled: bool}
* @phpstan-type HourMetricsType array{Version: ?string, Enabled?: bool, IncludeAPIs?: bool, RetentionPolicy?: RetentionPolicyType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Xray\AzureStoragePhpSdk\Contracts\Arrayable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type RetentionPolicyType array{Days?: int, Enabled: bool}
* @phpstan-type LoggingType array{Version: ?string, Delete?: bool, Read?: bool, Write?: bool, RetentionPolicy?: RetentionPolicyType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Xray\AzureStoragePhpSdk\Contracts\Arrayable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type RetentionPolicyType array{Days?: int, Enabled: bool}
* @phpstan-type MinuteMetricsType array{Version: ?string, Enabled?: bool, IncludeAPIs?: bool, RetentionPolicy?: RetentionPolicyType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Xray\AzureStoragePhpSdk\Contracts\Arrayable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type StaticWebsiteType array{Enabled?: bool, IndexDocument?: string, DefaultIndexDocumentPath?: string, ErrorDocument404Path?: string}
*
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Account/KeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Xray\AzureStoragePhpSdk\Converter\XmlConverter;
use Xray\AzureStoragePhpSdk\Exceptions\RequiredFieldException;

use function Xray\AzureStoragePhpSdk\Support\convert_to_ISO;

/** @implements Arrayable<array{KeyInfo: array{Start: string, Expiry: string}}> */
final readonly class KeyInfo implements Arrayable, Xmlable
{
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Blob/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Xray\AzureStoragePhpSdk\Concerns\HasManager;
use Xray\AzureStoragePhpSdk\Exceptions\RequiredFieldException;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-import-type PropertiesType from Properties
*
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Blob/BlobProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use DateTimeImmutable;
use Xray\AzureStoragePhpSdk\BlobStorage\Resource;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type BlobPropertyHeaders array{Last-Modified?: string, x-ms-creation-time?: string, x-ms-tag-count?: int, x-ms-blob-type?: string, x-ms-copy-completion-time?: string|null, x-ms-copy-status-description?: string|null, x-ms-copy-id?: string|null, x-ms-copy-progress?: string|null, x-ms-copy-source?: string|null, x-ms-copy-status?: string|null, x-ms-incremental-copy?: string|null, x-ms-copy-destination-snapshot?: string|null, x-ms-lease-duration?: string|null, x-ms-lease-state?: string|null, x-ms-lease-status?: string|null, Content-Length?: int, Content-Type?: string|null, ETag?: string, Content-MD5?: string|null, Content-Encoding?: string|null, Content-Language?: string|null, Content-Disposition?: string|null, Cache-Control?: string|null, x-ms-blob-sequence-number?: int, x-ms-request-id?: string|null, x-ms-version?: string|null, Date?: string, Accept-Ranges?: string|null, x-ms-blob-committed-block-count?: string|null, x-ms-server-encrypted?: bool, x-ms-encryption-key-sha256?: string|null, x-ms-encryption-context?: string|null, x-ms-encryption-scope?: string|null, x-ms-access-tier?: string|null, x-ms-access-tier-inferred?: string|null, x-ms-archive-status?: string|null, x-ms-access-tier-change-time?: string|null, x-ms-client-request-id?: string|null, x-ms-rehydrate-priority?: string|null, x-ms-or-policy-id?: string|null, x-ms-last-access-time?: string|null, x-ms-blob-sealed?: string|null, x-ms-immutability-policy-until-date?: string|null, x-ms-immutability-policy-mode?: string|null, x-ms-legal-hold?: string|null, x-ms-owner?: string|null, x-ms-group?: string|null, x-ms-permissions?: string|null, x-ms-acl?: string|null, x-ms-resource-type?: string|null, x-ms-expiry-time?: string|null, leaseId?: string|null, sequenceNumberAction?: string|null, Origin?: string|null}
* @suppressWarnings(PHPMD)
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Blob/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DateTimeImmutable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type PropertiesType array{Creation-Time?: string, Last-Modified?: string, Etag?: string, LeaseStatus?: string, LeaseState?: string, Owner?: string, Group?: string, Permissions?: string, Acl?: string, ResourceType?: string, Placeholder?: string, Content-Length?: string, Content-Type?: string, Content-Encoding?: string, Content-Language?: string, Content-MD5?: string, Cache-Control?: string, x-ms-blob-sequence-number?: string, BlobType?: string, AccessTier?: string, LeaseDuration?: string, CopyId?: string, CopyStatus?: string, CopySource?: string, CopyProgress?: string, CopyCompletionTime?: string, CopyStatusDescription?: string, ServerEncrypted?: string, CustomerProvidedKeySha256?: string, EncryptionContext?: string, EncryptionScope?: string, IncrementalCopy?: string, AccessTierInferred?: string, AccessTierChangeTime?: string, TagCount?: string, RehydratePriority?: string, ExpiryTime?: string, DeletedTime?: string, RemainingRetentionDays?: string}
* @suppressWarnings(PHPMD.TooManyFields)
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Xray\AzureStoragePhpSdk\Concerns\HasManager;
use Xray\AzureStoragePhpSdk\Exceptions\RequiredFieldException;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-import-type PropertiesType from Properties
*
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Container/ContainerProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use DateTimeImmutable;
use Xray\AzureStoragePhpSdk\BlobStorage\Resource;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

final readonly class ContainerProperties
{
public DateTimeImmutable $lastModified;
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Entities/Container/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DateTimeImmutable;

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type PropertiesType array{Last-Modified?: string, Etag?: string, LeaseStatus?: string, LeaseState?: string, DefaultEncryptionScope?: string, DenyEncryptionScopeOverride?: bool, HasImmutabilityPolicy?: bool, HasLegalHold?: bool, ImmutableStorageWithVersioningEnabled?: bool, DeletedTime?: string, RemainingRetentionDays?: int}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Managers/Blob/BlobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Xray\AzureStoragePhpSdk\Contracts\Manager;
use Xray\AzureStoragePhpSdk\Exceptions\{InvalidArgumentException, RequestException};

use function Xray\AzureStoragePhpSdk\Support\{convert_to_RFC1123, convert_to_RFC3339_micro};

/**
* @phpstan-import-type BlobType from Blob as BlobTypeStan
* @phpstan-import-type FileType from File
Expand Down
2 changes: 2 additions & 0 deletions src/BlobStorage/Resources/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
InvalidFileMimeTypeException,
};

use function Xray\AzureStoragePhpSdk\Support\to_boolean;

/**
* @phpstan-type FileType array{Content-Length?: string, Content-Type?: string, Content-MD5?: string, Last-Modified?: string, Accept-Ranges?: string, ETag?: string, Vary?: string, Server?: string, x-ms-request-id?: string, x-ms-version?: string, x-ms-creation-time?: string, x-ms-lease-status?: string, x-ms-lease-state?: string, x-ms-blob-type?: string, x-ms-server-encrypted?: bool, Date?: string}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Concerns/HasStreamingResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Xray\AzureStoragePhpSdk\BlobStorage\Resources\File;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException;

use function Xray\AzureStoragePhpSdk\Support\{is_running_in_console, with};

trait HasStreamingResponse
{
public static function stream(File $file, ?int $expires = null): string
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Xray\AzureStoragePhpSdk\Contracts\{Arrayable, Stringable};
use Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException;

use function Xray\AzureStoragePhpSdk\Support\str_camel_to_header;

/**
* @property-read ?string $contentEncoding
* @property-read ?string $contentLanguage
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Contracts\Http\{Request as RequestContract, Response as ResponseContract};

use function Xray\AzureStoragePhpSdk\Support\{validate_protocol, with};

/** @suppressWarnings(PHPMD.ExcessiveParameterList) */
final class Request implements RequestContract
{
Expand Down
105 changes: 105 additions & 0 deletions src/Support/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Support;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException;

define('RFC3339_MICRO', 'Y-m-d\TH:i:s.u\Z');

/**
* Applies a closure to a value and returns the value.
*
* @template T
* @param T $value The value to be passed to the closure.
* @param \Closure(T): void $callback The closure to be applied to the value.
* @return T The original value after the closure has been applied.
*/
function with(mixed $value, callable $callback): mixed
{
call_user_func($callback, $value);

return $value;
}

/**
* Checks if the application is running in the console.
*/
function is_running_in_console(): bool
{
return in_array(\PHP_SAPI, ['cli', 'phpdbg'], true);
}

/**
* Validates the protocol being http or https.
* @throws \Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException
*/
function validate_protocol(string $value): true
{
$validProtocols = ['http', 'https'];

if (!in_array($value, $validProtocols, true)) {
throw InvalidArgumentException::create(sprintf(
'Invalid protocol: %s. Valid protocols: %s',
$value,
implode(', ', $validProtocols),
));
}

return true;
}

/**
* Converts a string from camelCase to snake_case.
*/
function str_camel_to_header(string $value): string
{
return ucwords(preg_replace('/([a-z])([A-Z])/', '$1-$2', $value) ?? '', '-');
}

/**
* Casts a value to a boolean.
*/
function to_boolean(mixed $value): bool
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}

/**
* Converts a DateTime object to RFC1123 format.
*/
function convert_to_RFC1123(DateTime $dateTime): string
{
return (clone $dateTime)->setTimezone(new DateTimeZone('GMT'))->format('D, d M Y H:i:s') . ' GMT';
}

/**
* Converts a DateTime object to RFC3339 micro format.
*/
function convert_to_RFC3339_micro(DateTime $dateTime): string
{
$utcDateTime = $dateTime->setTimezone(new DateTimeZone('UTC'));

$microseconds = $dateTime->format('u');
$microseconds = str_pad($microseconds, 7, '0', STR_PAD_RIGHT);

return $utcDateTime->format('Y-m-d\TH:i:s.') . $microseconds . 'Z';
}

/**
* Converts a DateTime object to ISO format.
*/
function convert_to_ISO(DateTimeImmutable|string $dateTime): string
{
if (is_string($dateTime)) {
$dateTime = new DateTimeImmutable($dateTime);
}

$dateTime = $dateTime->setTimezone(new DateTimeZone('UTC'));

return str_replace('+00:00', 'Z', $dateTime->format('c'));
}
92 changes: 2 additions & 90 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use Xray\AzureStoragePhpSdk\Application\Application;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException;
declare(strict_types=1);

define('RFC3339_MICRO', 'Y-m-d\TH:i:s.u\Z');
use Xray\AzureStoragePhpSdk\Application\Application;

if (!function_exists('azure_app')) {
/**
Expand All @@ -26,90 +25,3 @@ function azure_app(?string $key = null, array $parameters = []): mixed
return $instance->make($key, $parameters);
}
}

if (!function_exists('with')) {
/**
* Applies a closure to a value and returns the value.
*
* @template T
* @param T $value The value to be passed to the closure.
* @param \Closure(T): void $callback The closure to be applied to the value.
* @return T The original value after the closure has been applied.
*/
function with(mixed $value, Closure $callback): mixed
{
$callback($value);

return $value;
}
}

if (!function_exists('is_running_in_console')) {
function is_running_in_console(): bool
{
return in_array(\PHP_SAPI, ['cli', 'phpdbg'], true);
}
}

if (!function_exists('validate_protocol')) {
function validate_protocol(string $value): true
{
$validProtocols = ['http', 'https'];

if (!in_array($value, $validProtocols, true)) {
throw InvalidArgumentException::create(sprintf(
'Invalid protocol: %s. Valid protocols: %s',
$value,
implode(', ', $validProtocols),
));
}

return true;
}
}

if (!function_exists('str_camel_to_header')) {
function str_camel_to_header(string $value): string
{
return ucwords(preg_replace('/([a-z])([A-Z])/', '$1-$2', $value) ?? '', '-');
}
}

if (!function_exists('to_boolean')) {
function to_boolean(mixed $value): bool
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}

if (!function_exists('convert_to_RFC1123')) {
function convert_to_RFC1123(DateTime $dateTime): string
{
return (clone $dateTime)->setTimezone(new DateTimeZone('GMT'))->format('D, d M Y H:i:s') . ' GMT';
}
}

if (!function_exists('convert_to_RFC3339_micro')) {
function convert_to_RFC3339_micro(DateTime $dateTime): string
{
$utcDateTime = $dateTime->setTimezone(new DateTimeZone('UTC'));

$microseconds = $dateTime->format('u');
$microseconds = str_pad($microseconds, 7, '0', STR_PAD_RIGHT);

return $utcDateTime->format('Y-m-d\TH:i:s.') . $microseconds . 'Z';
}
}

if (!function_exists('convert_to_ISO')) {
function convert_to_ISO(DateTimeImmutable|string $dateTime): string
{
if (is_string($dateTime)) {
$dateTime = new DateTimeImmutable($dateTime);
}

$dateTime = $dateTime->setTimezone(new DateTimeZone('UTC'));

return str_replace('+00:00', 'Z', $dateTime->format('c'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Xray\AzureStoragePhpSdk\Exceptions\InvalidResourceTypeException;
use Xray\AzureStoragePhpSdk\Fakes\Http\{RequestFake, ResponseFake};

use function Xray\AzureStoragePhpSdk\Support\convert_to_ISO;

pest()->group('authentications', 'shared-access-signatures');
covers(UserDelegationSas::class);

Expand Down
Loading

0 comments on commit 46c590d

Please sign in to comment.