<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


abstract class EncodingBase
{
  abstract public function encode($string);

  abstract public function decode($string);

  private function codeIfString($value, $function)
  {
    if (is_string($value)) {
      return call_user_func($function, $value);
    } else {
      return $value;
    }
  }

  public function encodeRecursive($array)
  {
    return ArrayOperations::mapArrayAndObjectRecursive(function($traversable) {
      return $this->codeIfString($traversable, array($this, 'encode'));
    }, $array);
  }

  public function decodeRecursive($array)
  {
    return ArrayOperations::mapArrayAndObjectRecursive(function($traversable) {
      return $this->codeIfString($traversable, array($this, 'decode'));
    }, $array);
  }
}