This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce filter for single id meta values #56
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php # -*- coding: utf-8 -*- | ||
|
||
namespace W2M\Import\Filter; | ||
|
||
use | ||
W2M\Import\Data; | ||
|
||
/** | ||
* Class SingleIdMetaValueFilter | ||
* | ||
* Filter scalar (meta) value (e.g. _thumbnail) and treat the value as object ID of a given type. | ||
* Looks up the ID in a given MultiTypeIdMapperInterface. | ||
* | ||
* @package W2M\Import\Filter | ||
*/ | ||
class SingleIdMetaValueFilter implements ValueFilterableInterface { | ||
|
||
/** | ||
* @var Data\MultiTypeIdMapperInterface | ||
*/ | ||
private $id_map; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $type; | ||
|
||
/** | ||
* @param $type | ||
* @param Data\MultiTypeIdMapperInterface $id_map | ||
*/ | ||
public function __construct( $type, Data\MultiTypeIdMapperInterface $id_map ) { | ||
|
||
$this->type = (string) $type; | ||
$this->id_map = $id_map; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param int $object_id | ||
* | ||
* @return mixed (Same type as $value) | ||
*/ | ||
public function filter( $value, $object_id ) { | ||
|
||
if ( ! $this->is_filterable( (int) $value, $object_id ) ) { | ||
return $value; | ||
} | ||
|
||
return $this->id_map->local_id( $this->type, (int) $value ); | ||
} | ||
|
||
/** | ||
* Return FALSE when the filter is not filterable due to missing data. E.g. a post | ||
* ID refers to a post which was not imported yet. | ||
* | ||
* @param mixed $value | ||
* @param int $object_id | ||
* | ||
* @return bool | ||
*/ | ||
public function is_filterable( $value, $object_id ) { | ||
|
||
return (bool) $this->id_map->local_id( $this->type, (int) $value ); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
tests/phpunit/Unit/Import/Filter/SingleIdMetaValueFilterTest.php
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,22 @@ | ||
<?php # -*- coding: utf-8 -*- | ||
|
||
namespace W2M\Test\Unit\Import\Filter; | ||
|
||
use | ||
W2M\Import\Filter, | ||
W2M\Test\Helper; | ||
|
||
class SingleIdMetaValueFilterTest extends Helper\MonkeyTestCase { | ||
|
||
public function test_filter() { | ||
|
||
//Todo: Write tests | ||
$this->markTestIncomplete( "Under construction" ); | ||
} | ||
|
||
public function test_is_filterable() { | ||
|
||
//Todo: Write tests | ||
$this->markTestIncomplete( "Under construction" ); | ||
} | ||
} |