Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
Introduce filter for single id meta values #56
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaber-de committed Feb 14, 2016
1 parent 0052e7a commit 85a608b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
67 changes: 67 additions & 0 deletions inc/Import/Filter/SingleIdMetaValueFilter.php
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 tests/phpunit/Unit/Import/Filter/SingleIdMetaValueFilterTest.php
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" );
}
}

0 comments on commit 85a608b

Please sign in to comment.