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 non-scalar meta data #56
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaber-de committed Feb 14, 2016
1 parent 85a608b commit 0b28991
Show file tree
Hide file tree
Showing 2 changed files with 604 additions and 0 deletions.
124 changes: 124 additions & 0 deletions inc/Import/Filter/RecursiveIdMetaValueFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php # -*- coding: utf-8 -*-

namespace W2M\Import\Filter;

use
W2M\Import\Data;

/**
* Class RecursiveIdMetaValueFilter
*
* Filter traversable (meta) value and treat given key(s) as object ID of a given type.
* Looks up the ID in a given MultiTypeIdMapperInterface.
*
*
* @package W2M\Import\Filter
*/
class RecursiveIdMetaValueFilter implements ValueFilterableInterface {

/**
* @var Data\MultiTypeIdMapperInterface
*/
private $id_map;

/**
* @var array
*/
private $key_type_map;

/**
* @param array $key_type_map
* @param Data\MultiTypeIdMapperInterface $id_map
*/
public function __construct( Array $key_type_map, Data\MultiTypeIdMapperInterface $id_map ) {

$this->key_type_map = $key_type_map;
$this->id_map = $id_map;
}

/**
* @param mixed $value
* @param int $object_id (Unused)
*
* @return mixed (Same type as $value)
*/
public function filter( $value, $object_id ) {

if ( ! $this->is_filterable( $value, $object_id ) )
return $value;

if ( ! is_array( $value ) && ! is_object( $value ) )
return $value;

return $this->recursive_replacement( $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 (Unused)
*
* @return bool
*/
public function is_filterable( $value, $object_id ) {

if ( ! is_array( $value ) && ! is_object( $value ) ) {
return TRUE; // Note, we don't want to get called twice
}

return $this->recursive_lookup( $value );
}

/**
* @param $value
*
* @return mixed
*/
public function recursive_replacement( $value ) {

foreach ( $value as $key => &$record ) {
if ( ! is_scalar( $record ) ) {
$record = $this->recursive_replacement( $record );
continue;
}
if ( ! isset( $this->key_type_map[ $key ] ) )
continue;
$type = $this->key_type_map[ $key ];
$record = $this->id_map->local_id( $type, (int) $record );
}

return $value;
}


/**
* @param $value
*
* @return bool
*/
public function recursive_lookup( $value ) {

/**
* This is not really DRY as it copies the iteration logic from recursive_replacement.
* A simple comparison between the original and replaced value is not sufficient as it
* might be possible that original and mapped IDs are equivalent. The iteration should
* be refactored with recursive iterators maybe.
*/
$filterable = FALSE;
foreach ( $value as $key => $record ) {
if ( ! is_scalar( $record ) ) {
$filterable = $this->recursive_lookup( $record );
continue;
}
if ( ! isset( $this->key_type_map[ $key ] ) ) {
continue;
}
$type = $this->key_type_map[ $key ];
$filterable = (bool) $this->id_map->local_id( $type, (int) $record );
}

return $filterable;
}
}
Loading

0 comments on commit 0b28991

Please sign in to comment.