Advertisement
Guest User

Untitled

a guest
Dec 21st, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. class EntityPatcher {
  2.  
  3.     /**
  4.      * @since 0.4
  5.      *
  6.      * @param string $entityType
  7.      *
  8.      * @return EntityDiffer
  9.      */
  10.     public static function newForType( $entityType ) {
  11.         if ( $entityType === Item::ENTITY_TYPE ) {
  12.             $class = '\Wikibase\ItemPatcher';
  13.         }
  14.         else {
  15.             $class = __CLASS__;
  16.         }
  17.  
  18.         return new $class( new MapPatcher( true ) );
  19.     }
  20.  
  21.     /**
  22.      * @since 0.4
  23.      *
  24.      * @var MapPatcher
  25.      */
  26.     protected $mapPatcher;
  27.  
  28.     /**
  29.      * @since 0.4
  30.      *
  31.      * @param boolean $throwErrors
  32.      */
  33.     public function __construct( MapPatcher $mapPatcher ) {
  34.         $this->mapPatcher = $mapPatcher;
  35.     }
  36.  
  37.     /**
  38.      * @since 0.4
  39.      *
  40.      * @param Entity $oldEntity
  41.      * @param EntityDiff $patch
  42.      *
  43.      * @return Entity
  44.      * @throws MWException
  45.      */
  46.     public final function getPatchedEntity( Entity $entity, EntityDiff $patch ) {
  47.         $newEntity = $this->entityToArray( $entity );
  48.         $newEntity = $this->mapPatcher->patch( $newEntity, $patch );
  49.  
  50.         return $this->entityFromArray( $newEntity, $entity );
  51.     }
  52.  
  53.     /**
  54.      * @since 0.4
  55.      *
  56.      * @param array $entity
  57.      * @param Entity $originalEntity
  58.      *
  59.      * @return Entity
  60.      */
  61.     protected final function entityFromArray( array $entity, Entity $originalEntity ) {
  62.         $originalEntity->clear();
  63.  
  64.         foreach ( $entity['aliases'] as $langCode => $aliases ) {
  65.             $originalEntity->setAliases( $langCode, $aliases );
  66.         }
  67.  
  68.         foreach ( $entity['label'] as $langCode => $label ) {
  69.             $originalEntity->setLabel( $langCode, $label );
  70.         }
  71.  
  72.         foreach ( $entity['description'] as $langCode => $description ) {
  73.             $originalEntity->setDescription( $langCode, $description );
  74.         }
  75.  
  76.         // TODO: claims
  77.  
  78.         $this->setSpecificObjectFields( $entity, $originalEntity );
  79.  
  80.         return $originalEntity;
  81.     }
  82.  
  83.     /**
  84.      * @since 0.4
  85.      *
  86.      * @param array $entityArray
  87.      * @param Entity $entityObject
  88.      */
  89.     protected function setSpecificObjectFields( array $entityArray, Entity &$entityObject ) {
  90.         // No-op, meant to be overridden in deriving classes to add specific behaviour
  91.     }
  92.  
  93.     /**
  94.      * @since 0.4
  95.      *
  96.      * @param Entity $entity
  97.      *
  98.      * @return array
  99.      */
  100.     protected final function entityToArray( Entity $entity ) {
  101.         $array = array();
  102.  
  103.         $array['aliases'] = $entity->getAllAliases();
  104.         $array['label'] = $entity->getLabels();
  105.         $array['description'] = $entity->getDescriptions();
  106.  
  107.         // TODO: claims
  108.  
  109.         $this->setSpecificArrayFields( $array, $entity );
  110.  
  111.         return $array;
  112.     }
  113.  
  114.     /**
  115.      * @since 0.4
  116.      *
  117.      * @param array $entityArray
  118.      * @param Entity $entityObject
  119.      */
  120.     protected function setSpecificArrayFields( array &$entityArray, Entity $entityObject ) {
  121.         // No-op, meant to be overridden in deriving classes to add specific behaviour
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement