status(), $license->status()->value, $siteActive, $productSlug, $license->expiresAt(), $license->activationLimit(), $license->activationCount(), $checkedAt ); } /** * Build a cached result from a last-known-good payload. * * @since 1.0.0 * * @param array $payload The payload {@see toArray()} produced earlier. * @param DateTimeImmutable $checkedAt When this (failed) validation ran. * @return self The cached result, flagged from_cache. */ public static function fromCachedPayload( array $payload, DateTimeImmutable $checkedAt ): self { $expiresAt = null; if ( isset( $payload['expires_at'] ) && is_string( $payload['expires_at'] ) ) { $expiresAt = new DateTimeImmutable( $payload['expires_at'], new DateTimeZone( 'UTC' ) ); } return new self( (bool) ( $payload['valid'] ?? false ), (string) ( $payload['status'] ?? 'invalid' ), isset( $payload['site_active'] ) ? (bool) $payload['site_active'] : null, isset( $payload['product_slug'] ) ? (string) $payload['product_slug'] : null, $expiresAt, isset( $payload['activation_limit'] ) ? (int) $payload['activation_limit'] : null, isset( $payload['activation_count'] ) ? (int) $payload['activation_count'] : null, $checkedAt, true ); } /** * Whether the license is presently valid (status 'active'). * * @since 1.0.0 * * @return bool True when valid. */ public function isValid(): bool { return $this->valid; } /** * The status word. * * @since 1.0.0 * * @return string One of active, expired, disabled, invalid, key_mismatch. */ public function status(): string { return $this->status; } /** * Whether the result came from the last-known-good cache. * * @since 1.0.0 * * @return bool True for a grace-window answer. */ public function isFromCache(): bool { return $this->fromCache; } /** * The REST response payload. * * @since 1.0.0 * * @return array The canonical payload. */ public function toArray(): array { $payload = array( 'valid' => $this->valid, 'status' => $this->status, 'site_active' => $this->siteActive, 'product_slug' => $this->productSlug, 'expires_at' => null === $this->expiresAt ? null : $this->expiresAt->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d\TH:i:s\Z' ), 'activation_limit' => $this->activationLimit, 'activation_count' => $this->activationCount, 'checked_at' => $this->checkedAt->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d\TH:i:s\Z' ), ); if ( $this->fromCache ) { $payload['from_cache'] = true; } return $payload; } }