diff --git a/src/Package/ContentPackage.php b/src/Package/ContentPackage.php new file mode 100644 index 0000000..e2c28e2 --- /dev/null +++ b/src/Package/ContentPackage.php @@ -0,0 +1,98 @@ + */ + private array $data; + + /** + * @param array $data Package data. + */ + private function __construct( array $data ) { + $this->data = $data; + } + + /** + * @param array $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 + */ + public function source(): array { + return $this->data['source']; + } + + /** + * @return array + */ + public function destination(): array { + return $this->data['destination']; + } + + /** + * @return array + */ + public function manifest(): array { + return $this->data['manifest']; + } + + /** + * @return array + */ + public function records(): array { + return $this->data['records']; + } + + /** + * @return array + */ + public function checksums(): array { + return $this->data['checksums']; + } + + /** + * @return array + */ + public function toArray(): array { + return $this->data; + } + + /** + * @param mixed $value Value to normalize. + * + * @return array + */ + private static function arrayValue( $value ): array { + return is_array( $value ) ? $value : array(); + } +} diff --git a/tests/Unit/Package/ContentPackageTest.php b/tests/Unit/Package/ContentPackageTest.php new file mode 100644 index 0000000..dba721c --- /dev/null +++ b/tests/Unit/Package/ContentPackageTest.php @@ -0,0 +1,54 @@ + '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() ); + } +}