feat: add content package value object
This commit is contained in:
@@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Immutable sync package value object.
|
||||||
|
*
|
||||||
|
* @package WPContentSync
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace WPContentSync\Package;
|
||||||
|
|
||||||
|
final class ContentPackage {
|
||||||
|
public const SCHEMA_VERSION = '1.0';
|
||||||
|
|
||||||
|
/** @var array<string, mixed> */
|
||||||
|
private array $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $data Package data.
|
||||||
|
*/
|
||||||
|
private function __construct( array $data ) {
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $data Package data.
|
||||||
|
*/
|
||||||
|
public static function fromArray( array $data ): self {
|
||||||
|
return new self(
|
||||||
|
array(
|
||||||
|
'schema_version' => (string) ( $data['schema_version'] ?? self::SCHEMA_VERSION ),
|
||||||
|
'generated_at' => (string) ( $data['generated_at'] ?? '' ),
|
||||||
|
'source' => self::arrayValue( $data['source'] ?? array() ),
|
||||||
|
'destination' => self::arrayValue( $data['destination'] ?? array() ),
|
||||||
|
'manifest' => self::arrayValue( $data['manifest'] ?? array() ),
|
||||||
|
'records' => self::arrayValue( $data['records'] ?? array() ),
|
||||||
|
'checksums' => self::arrayValue( $data['checksums'] ?? array() ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function schemaVersion(): string {
|
||||||
|
return $this->data['schema_version'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generatedAt(): string {
|
||||||
|
return $this->data['generated_at'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function source(): array {
|
||||||
|
return $this->data['source'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function destination(): array {
|
||||||
|
return $this->data['destination'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function manifest(): array {
|
||||||
|
return $this->data['manifest'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function records(): array {
|
||||||
|
return $this->data['records'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function checksums(): array {
|
||||||
|
return $this->data['checksums'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(): array {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value Value to normalize.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private static function arrayValue( $value ): array {
|
||||||
|
return is_array( $value ) ? $value : array();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WPContentSync\Tests\Unit\Package;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use WPContentSync\Package\ContentPackage;
|
||||||
|
|
||||||
|
class ContentPackageTest extends TestCase {
|
||||||
|
public function test_it_normalizes_package_arrays(): void {
|
||||||
|
$package = ContentPackage::fromArray(
|
||||||
|
array(
|
||||||
|
'schema_version' => '1.0',
|
||||||
|
'generated_at' => '2026-04-26T20:30:00+00:00',
|
||||||
|
'source' => array(
|
||||||
|
'site_url' => 'https://example.test',
|
||||||
|
'name' => 'Example Production',
|
||||||
|
),
|
||||||
|
'destination' => array(
|
||||||
|
'site_url' => 'https://staging.example.test',
|
||||||
|
'name' => 'Example Staging',
|
||||||
|
),
|
||||||
|
'manifest' => array(
|
||||||
|
'posts' => 1,
|
||||||
|
'terms' => 0,
|
||||||
|
'media' => 0,
|
||||||
|
'custom_post_types' => 0,
|
||||||
|
),
|
||||||
|
'records' => array(
|
||||||
|
'posts' => array(
|
||||||
|
array(
|
||||||
|
'id' => 123,
|
||||||
|
'type' => 'post',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'terms' => array(),
|
||||||
|
'media' => array(),
|
||||||
|
'custom_post_types' => array(),
|
||||||
|
),
|
||||||
|
'checksums' => array(
|
||||||
|
'records' => 'sha256:abc123',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame( '1.0', $package->schemaVersion() );
|
||||||
|
self::assertSame( '2026-04-26T20:30:00+00:00', $package->generatedAt() );
|
||||||
|
self::assertSame( 'https://example.test', $package->source()['site_url'] );
|
||||||
|
self::assertSame( 'https://staging.example.test', $package->destination()['site_url'] );
|
||||||
|
self::assertSame( 1, $package->manifest()['posts'] );
|
||||||
|
self::assertSame( 123, $package->records()['posts'][0]['id'] );
|
||||||
|
self::assertSame( 'sha256:abc123', $package->checksums()['records'] );
|
||||||
|
self::assertSame( $package->toArray(), ContentPackage::fromArray( $package->toArray() )->toArray() );
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user