Skip to content

Commit

Permalink
Introduce an interface so all schemas implement the same
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Apr 8, 2021
1 parent 9e28f88 commit 78fc146
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
17 changes: 9 additions & 8 deletions lib/class-wp-theme-json-schema-v0.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
/**
* Processes structures that adhere to the theme.json schema V0.
* Class that implements a WP_Theme_JSON_Schema to convert
* a given structure in v0 schema to the latest one.
*
* @package gutenberg
*/

/**
* Class that encapsulates the processing of
* structures that adhere to the theme.json V0.
* Class that implements a WP_Theme_JSON_Schema to convert
* a given structure in v0 schema to the latest one.
*/
class WP_Theme_JSON_Schema_V0 {
class WP_Theme_JSON_Schema_V0 implements WP_Theme_JSON_Schema {

/**
* How to address all the blocks
Expand Down Expand Up @@ -121,13 +122,13 @@ class WP_Theme_JSON_Schema_V0 {
);

/**
* Utility to convert v0 schemas into v1.
* Converts a v0 schema into the latest.
*
* @param array $old A theme.json in v0 format.
* @param array $old Data in v0 schema.
*
* @return array The new theme.json in the v1 format.
* @return array Data in the latest schema.
*/
public static function to_v1( $old ) {
public static function parse( $old ) {
// Copy everything.
$new = $old;

Expand Down
18 changes: 18 additions & 0 deletions lib/interface-wp-theme-json-schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Schema interface for theme.json structures.
*
* @package gutenberg
*/

interface WP_Theme_JSON_Schema {
/**
* Parses an array that follows an old theme.json schema
* into the latest theme.json schema.
*
* @param array $theme_json Old data to convert.
*
* @return array The data in the latest theme.json schema.
*/
public static function parse( $theme_json );
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// These are used by some FSE features
// as well as global styles.
require __DIR__ . '/interface-wp-theme-json-schema.php';
require __DIR__ . '/class-wp-theme-json-schema-v0.php';
require __DIR__ . '/class-wp-theme-json.php';
require __DIR__ . '/class-wp-theme-json-resolver.php';
Expand Down
7 changes: 4 additions & 3 deletions phpunit/class-wp-theme-json-schema-v0-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class WP_Theme_JSON_Schema_V0_Test extends WP_UnitTestCase {

function test_schema_to_v1() {
function test_parse() {
$theme_json_v0 = array(
'settings' => array(
'defaults' => array(
Expand Down Expand Up @@ -85,7 +85,8 @@ function test_schema_to_v1() {
),
),
);
$theme_json_v1 = WP_Theme_JSON_Schema_V0::to_v1( $theme_json_v0 );

$actual = WP_Theme_JSON_Schema_V0::parse( $theme_json_v0 );

$expected = array(
'version' => 1,
Expand Down Expand Up @@ -150,6 +151,6 @@ function test_schema_to_v1() {
),
);

$this->assertEqualSetsWithIndex( $expected, $theme_json_v1 );
$this->assertEqualSetsWithIndex( $expected, $actual );
}
}

0 comments on commit 78fc146

Please sign in to comment.