-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data view repository collection.
- Loading branch information
Showing
4 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace DataKit\Plugin\DataView; | ||
|
||
use DataKit\DataViews\DataView\ArrayDataViewRepository; | ||
use DataKit\DataViews\DataView\DataView; | ||
use DataKit\DataViews\DataView\DataViewNotFoundException; | ||
use DataKit\DataViews\DataView\DataViewRepository; | ||
|
||
/** | ||
* A DataView repository that manages multiple registered {@see DataViewRepository} instances. | ||
* | ||
* @since $ver$ | ||
*/ | ||
final class WordPressDataViewRepository implements DataViewRepository { | ||
/** | ||
* The collection of repositories. | ||
* | ||
* @since $ver$ | ||
* @var DataViewRepository[] | ||
*/ | ||
private array $repositories; | ||
|
||
/** | ||
* Repository used to register extra DataViews. | ||
* | ||
* @since $ver$ | ||
* @var ArrayDataViewRepository | ||
*/ | ||
private ArrayDataViewRepository $inner; | ||
|
||
/** | ||
* Creates the repository. | ||
* | ||
* @since $ver$ | ||
* | ||
* @param DataViewRepository ...$repositories The collection of repositories. | ||
*/ | ||
public function __construct( DataViewRepository ...$repositories ) { | ||
$this->repositories = (array) apply_filters( 'datakit/repositories', $repositories ); | ||
$this->inner = new ArrayDataViewRepository(); | ||
} | ||
|
||
/** | ||
* Returns the registered repositories and the inner repository last. | ||
* | ||
* @since $ver$ | ||
* | ||
* @return DataViewRepository[] The repositories. | ||
*/ | ||
private function repositories(): array { | ||
return [ ...$this->repositories, $this->inner ]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since $ver$ | ||
*/ | ||
public function all(): array { | ||
$result = []; | ||
foreach ( $this->repositories() as $repository ) { | ||
$result[] = $repository->all(); | ||
} | ||
|
||
return array_merge( [], ...$result ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since $ver$ | ||
*/ | ||
public function get( string $id ): DataView { | ||
foreach ( $this->repositories() as $repository ) { | ||
if ( $repository->has( $id ) ) { | ||
return $repository->get( $id ); | ||
} | ||
} | ||
|
||
throw new DataViewNotFoundException(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since $ver$ | ||
*/ | ||
public function has( string $id ): bool { | ||
foreach ( $this->repositories() as $repository ) { | ||
if ( $repository->has( $id ) ) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since $ver$ | ||
*/ | ||
public function save( DataView $data_view ): void { | ||
$this->inner->save( $data_view ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since $ver$ | ||
*/ | ||
public function delete( DataView $data_view ): void { | ||
foreach ( $this->repositories() as $repository ) { | ||
if ( $this->has( $data_view->id() ) ) { | ||
$repository->delete( $data_view ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace DataKit\Plugin\Tests\DataView; | ||
|
||
use DataKit\DataViews\Data\ArrayDataSource; | ||
use DataKit\DataViews\DataView\ArrayDataViewRepository; | ||
use DataKit\DataViews\DataView\DataView; | ||
use DataKit\DataViews\DataView\DataViewNotFoundException; | ||
use DataKit\Plugin\DataView\WordPressDataViewRepository; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for {@see WordPressDataViewRepository} | ||
* | ||
* @since $ver$ | ||
*/ | ||
final class WordPressDataViewRepositoryTest extends TestCase { | ||
/** | ||
* Test case for {@see WordPressDataViewRepository}. | ||
* | ||
* @since $ver$ | ||
*/ | ||
public function test_collection(): void { | ||
$data = new ArrayDataSource( 'data', [] ); | ||
$repository_1 = new ArrayDataViewRepository( [ $table_1 = DataView::table( 'table_1', $data, [] ) ] ); | ||
$repository_2 = new ArrayDataViewRepository( [ $table_2 = DataView::table( 'table_2', $data, [] ) ] ); | ||
|
||
$wordpress_repository = new WordPressDataViewRepository( $repository_1, $repository_2 ); | ||
$wordpress_repository->save( $table_3 = DataView::table( 'table_3', $data, [] ) ); | ||
|
||
self::assertTrue( $wordpress_repository->has( 'table_1' ) ); | ||
self::assertTrue( $wordpress_repository->has( 'table_2' ) ); | ||
self::assertTrue( $wordpress_repository->has( 'table_3' ) ); | ||
|
||
self::assertFalse( $wordpress_repository->has( 'missing' ) ); | ||
self::assertEquals( $data, $wordpress_repository->get( 'table_1' )->data_source() ); | ||
self::assertSame( compact( 'table_1', 'table_2', 'table_3' ), $wordpress_repository->all() ); | ||
|
||
$wordpress_repository->delete( $table_1 ); | ||
self::assertFalse( $wordpress_repository->has( 'table_1' ) ); | ||
self::assertFalse( $repository_1->has( 'table_1' ) ); | ||
|
||
$this->expectException( DataViewNotFoundException::class ); | ||
$wordpress_repository->get( 'missing' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
<?php | ||
|
||
require dirname( __DIR__ ) . '/vendor/autoload.php'; | ||
|
||
// Polyfill for unit tests. | ||
if ( ! function_exists( 'apply_filters' ) ) { | ||
function apply_filters( ...$args ) { | ||
return $args[1]; | ||
} | ||
} |