Files
wc-licensed-product-client/src/Dto/UpdateInfo.php
magdev 760e1e752a Add update-check endpoint support (v0.2.1)
Implement /update-check endpoint aligned with remote OpenAPI spec:
- Add checkForUpdates() method to LicenseClientInterface
- Add UpdateInfo DTO for update check responses
- Add ProductNotFoundException for product_not_found error
- Update local openapi.json to v0.4.0

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 20:52:12 +01:00

111 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Magdev\WcLicensedProductClient\Dto;
/**
* Represents update information returned by the update-check endpoint.
*/
final readonly class UpdateInfo
{
/**
* @param bool $updateAvailable Whether an update is available
* @param string|null $version Latest available version
* @param string|null $slug Plugin slug for WordPress
* @param string|null $plugin Plugin basename (slug/slug.php)
* @param string|null $downloadUrl Secure download URL for the update package
* @param \DateTimeImmutable|null $lastUpdated Date of the latest release
* @param string|null $tested Highest WordPress version tested with
* @param string|null $requires Minimum required WordPress version
* @param string|null $requiresPhp Minimum required PHP version
* @param string|null $changelog Release notes/changelog for the update
* @param string|null $packageHash SHA256 hash of the package for integrity verification
* @param string|null $name Product name
* @param string|null $homepage Product homepage URL
* @param array<string, string>|null $icons Plugin icons for WordPress admin
* @param array<string, string>|null $sections Content sections for plugin info modal
*/
public function __construct(
public bool $updateAvailable,
public ?string $version = null,
public ?string $slug = null,
public ?string $plugin = null,
public ?string $downloadUrl = null,
public ?\DateTimeImmutable $lastUpdated = null,
public ?string $tested = null,
public ?string $requires = null,
public ?string $requiresPhp = null,
public ?string $changelog = null,
public ?string $packageHash = null,
public ?string $name = null,
public ?string $homepage = null,
public ?array $icons = null,
public ?array $sections = null,
) {
}
public static function fromArray(array $data): self
{
if (!isset($data['update_available']) || !is_bool($data['update_available'])) {
throw new \InvalidArgumentException('Invalid response: missing or invalid update_available');
}
$lastUpdated = null;
if (isset($data['last_updated']) && $data['last_updated'] !== null) {
try {
$lastUpdated = new \DateTimeImmutable($data['last_updated']);
} catch (\Exception $e) {
throw new \InvalidArgumentException(
'Invalid response: invalid date format for last_updated',
0,
$e
);
}
}
$icons = null;
if (isset($data['icons']) && is_array($data['icons'])) {
$icons = $data['icons'];
}
$sections = null;
if (isset($data['sections']) && is_array($data['sections'])) {
$sections = $data['sections'];
}
return new self(
updateAvailable: $data['update_available'],
version: isset($data['version']) && is_string($data['version']) ? $data['version'] : null,
slug: isset($data['slug']) && is_string($data['slug']) ? $data['slug'] : null,
plugin: isset($data['plugin']) && is_string($data['plugin']) ? $data['plugin'] : null,
downloadUrl: isset($data['download_url']) && is_string($data['download_url']) ? $data['download_url'] : (
isset($data['package']) && is_string($data['package']) ? $data['package'] : null
),
lastUpdated: $lastUpdated,
tested: isset($data['tested']) && is_string($data['tested']) ? $data['tested'] : null,
requires: isset($data['requires']) && is_string($data['requires']) ? $data['requires'] : null,
requiresPhp: isset($data['requires_php']) && is_string($data['requires_php']) ? $data['requires_php'] : null,
changelog: isset($data['changelog']) && is_string($data['changelog']) ? $data['changelog'] : null,
packageHash: isset($data['package_hash']) && is_string($data['package_hash']) ? $data['package_hash'] : null,
name: isset($data['name']) && is_string($data['name']) ? $data['name'] : null,
homepage: isset($data['homepage']) && is_string($data['homepage']) ? $data['homepage'] : null,
icons: $icons,
sections: $sections,
);
}
/**
* Check if the package hash is valid against the expected format.
*/
public function hasValidPackageHash(): bool
{
if ($this->packageHash === null) {
return false;
}
// Package hash format: "sha256:hexstring"
return preg_match('/^sha256:[a-f0-9]{64}$/i', $this->packageHash) === 1;
}
}