feat: add content package schema validator
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Package validation result.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Package;
|
||||
|
||||
final class PackageValidationResult {
|
||||
/** @var array<int, string> */
|
||||
private array $errors;
|
||||
|
||||
/**
|
||||
* @param array<int, string> $errors Validation errors.
|
||||
*/
|
||||
private function __construct( array $errors ) {
|
||||
$this->errors = array_values( $errors );
|
||||
}
|
||||
|
||||
public static function valid(): self {
|
||||
return new self( array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $errors Validation errors.
|
||||
*/
|
||||
public static function invalid( array $errors ): self {
|
||||
return new self( $errors );
|
||||
}
|
||||
|
||||
public function isValid(): bool {
|
||||
return array() === $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function errors(): array {
|
||||
return $this->errors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Versioned package schema validator.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Package;
|
||||
|
||||
final class PackageValidator {
|
||||
private const RECORD_BUCKETS = array(
|
||||
'posts',
|
||||
'terms',
|
||||
'media',
|
||||
'custom_post_types',
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data Decoded package data.
|
||||
*/
|
||||
public function validate( array $data ): PackageValidationResult {
|
||||
$errors = array();
|
||||
|
||||
foreach ( array( 'schema_version', 'generated_at', 'source', 'destination', 'manifest', 'records', 'checksums' ) as $field ) {
|
||||
if ( ! array_key_exists( $field, $data ) ) {
|
||||
$errors[] = $field . ' is required.';
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $data['schema_version'] ) && ContentPackage::SCHEMA_VERSION !== $data['schema_version'] ) {
|
||||
$errors[] = 'schema_version must be ' . ContentPackage::SCHEMA_VERSION . '.';
|
||||
}
|
||||
|
||||
if ( isset( $data['source'] ) && ! is_array( $data['source'] ) ) {
|
||||
$errors[] = 'source must be an object.';
|
||||
}
|
||||
|
||||
if ( isset( $data['destination'] ) && ! is_array( $data['destination'] ) ) {
|
||||
$errors[] = 'destination must be an object.';
|
||||
}
|
||||
|
||||
if ( isset( $data['manifest'] ) && ! is_array( $data['manifest'] ) ) {
|
||||
$errors[] = 'manifest must be an object.';
|
||||
}
|
||||
|
||||
if ( isset( $data['records'] ) && ! is_array( $data['records'] ) ) {
|
||||
$errors[] = 'records must be an object.';
|
||||
}
|
||||
|
||||
if ( isset( $data['checksums'] ) && ! is_array( $data['checksums'] ) ) {
|
||||
$errors[] = 'checksums must be an object.';
|
||||
}
|
||||
|
||||
if ( isset( $data['manifest'], $data['records'] ) && is_array( $data['manifest'] ) && is_array( $data['records'] ) ) {
|
||||
$errors = array_merge( $errors, $this->validateRecordBuckets( $data['manifest'], $data['records'] ) );
|
||||
}
|
||||
|
||||
return array() === $errors ? PackageValidationResult::valid() : PackageValidationResult::invalid( $errors );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $manifest Package manifest.
|
||||
* @param array<string, mixed> $records Package records.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function validateRecordBuckets( array $manifest, array $records ): array {
|
||||
$errors = array();
|
||||
|
||||
foreach ( self::RECORD_BUCKETS as $bucket ) {
|
||||
if ( ! isset( $records[ $bucket ] ) || ! is_array( $records[ $bucket ] ) ) {
|
||||
$errors[] = 'records.' . $bucket . ' is required and must be an array.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! isset( $manifest[ $bucket ] ) || ! is_int( $manifest[ $bucket ] ) ) {
|
||||
$errors[] = 'manifest.' . $bucket . ' is required and must be an integer.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( count( $records[ $bucket ] ) !== $manifest[ $bucket ] ) {
|
||||
$errors[] = 'manifest.' . $bucket . ' must match records.' . $bucket . ' count.';
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user