2016-12-09 2 views
0

今日、私はZend Hydratorクラスで遊んでいましたが、入力キーをオンザフライで変換するためのネーミング戦略が見つかりました。しかし、ObjectPropertyハイドレーターと一緒にMapNamingStrategyを試してみると、入力配列にオブジェクトが含まれていれば、オブジェクトに最初に存在しなかったプロパティが追加されているようです。Zend 2ハイドレーター戦略制限キー

新しいプロパティを追加したり、入力オブジェクトに既存のプロパティを設定したり水分のみを入力することを制限する方法はありますか?

+0

私はデータベースからアドレスレコードを取り出して別のライブラリのAddressクラスを作成しようとしています。 'address_1'と 'address'のようないくつかのキー名に加えて、データベースには第三者のライブラリにない余分なフィールドも含まれています。私は、既存のプロパティに一致するキーを設定する必要があります。 – Scott

答えて

0

私がやったことは、2つのシナリオのいずれかを使用していましたが、まだ理想的ではありません。最初に、Class Reflectionを使用して、アクセス可能なキーのリストを取得したり、同じ名前の標準的な名前のアクセサを検索したりします。 2番目は、不一致のキー - >プロパティマッピングを含むだけでなく、1対1の(一致した)すべてのキーマッピングを含むマップを事前に定義することでした。 >プロパティマッピングは、マップのキーと値のペアを使用して水和を実行する前に、PHPの配列関数を使用して入力をフィルタリングします。しかし、この種の水分補給の目的は、その時点では使用できなくなってしまいます。代わりにforeachループを使用することもできます。そして、潜在的なすべての入出力キーとプロパティの関係を事前に知る必要があるという点で、抽象的な送り先を使用する能力を排除します。

標準的なラクダのsetPropertyName()/ getPropertyName()メソッドに適合するパブリックプロパティおよび/またはパブリックアクセサリを探す最初のメソッド(これは必ずしもマジックメソッドアクセサを処理するわけではありません)アクセサーメソッド。:

<?php 
/** 
* simple object hydrator using class reflection to find publicly accessible properties and/or methods 
* 
* Created by PhpStorm. 
* User: scottw 
* Date: 12/12/16 
* Time: 12:06 PM 
*/ 

namespace Finao\Util; 

class SimpleHydrator 
{ 
    /** 
    * whether to reset the keyMap following each hydration to clear the hydrator for other data/object pairs 
    * 
    * @var bool $resetMap 
    */ 
    private static $resetMap = true; 

    /** 
    * associative array of key mappings between incoming data and object property names/accessors 
    * @var array $keyMap 
    */ 
    private static $keyMap = array(); 
    public static function setKeyMap($map) { 
     if(self::is_assoc($map)) 
      static::$keyMap = $map; 
    } 
    public static function populateObject(&$targetObject, $dataArray) 
    { 
     if (self::is_assoc($dataArray) && is_object($targetObject)) { 
      // step through array elements and see if there are matching properties or methods 
      try { 
       foreach ($dataArray as $k => $v) { 
        $key = $k; 
        if(self::is_assoc(static::$keyMap) && array_key_exists($k)) 
         $key = static::$keyMap[$k]; 
        // if original value contains an object, try populating it if the associated value is also array 
        $origVal = self::getObjectPropertyValue($targetObject, $key); 
        if (is_object($origVal) && self::is_assoc($v)) { 
         self::populateObject($origVal, $v); 
         $v = $origVal; 
        } 

        $accessor = 'set' . ucfirst($key); 
        if (in_array($key, self::getObjectPublicProperties($targetObject))) 
         $targetObject->$key = $v; 
        elseif (in_array($accessor, self::getObjectPublicMethods($targetObject))) 
         $targetObject->$accessor($v); 

       } 
      } catch (\Exception $d) { 
       // do something with failures 
      } 

      if(static::$resetMap) static::$keyMap = array(); 
     } 

     return $targetObject; 
    } 

    public static function getObjectPropertyValue($object, $property) 
    { 
     $objectReflection = new \ReflectionClass($object); 
     if ($objectReflection->hasProperty($property) && $objectReflection->getProperty($property)->isPublic()) 
      return $object->$property; 
     else { 
      $accessor = 'get' . ucfirst($property); 
      if ($objectReflection->hasProperty($accessor) && $objectReflection->getMethod($accessor)->isPublic()) 
       return $object->$accessor(); 
     } 
    } 

    public static function getObjectPublicProperties($object) 
    { 
     if (is_object($object)) { 
      $publicProperties = array(); 
      $objectReflection = new \ReflectionClass($object); 
      foreach ($objectReflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $p) 
       array_push($publicProperties, $p->name); 
      return $publicProperties; 
     } 
    } 

    public static function getObjectPublicMethods($object) 
    { 
     if (is_object($object)) { 
      $publicMethods = array(); 
      $objectReflection = new \ReflectionClass($object); 
      foreach ($objectReflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $p) 
       array_push($publicMethods, $p->name); 
      return $publicMethods; 
     } 
    } 

    /** 
    * Determine if a variable is an associative array. 
    * 
    * @param mixed Input variable 
    * @return boolean If the input variable is an associative array. 
    * @see http://us2.php.net/manual/en/function.is-array.php 
    */ 
    public static function is_assoc($array) { 
     return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array))))); 
    } 
} 

私は最終的に単純なキーマッピング機能を追加しました。 (これは厳密にはテストされておらず、その名前が示すように単純な解決策に過ぎないことに注意してください)。