Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EntityPatcher {
- /**
- * @since 0.4
- *
- * @param string $entityType
- *
- * @return EntityDiffer
- */
- public static function newForType( $entityType ) {
- if ( $entityType === Item::ENTITY_TYPE ) {
- $class = '\Wikibase\ItemPatcher';
- }
- else {
- $class = __CLASS__;
- }
- return new $class( new MapPatcher( true ) );
- }
- /**
- * @since 0.4
- *
- * @var MapPatcher
- */
- protected $mapPatcher;
- /**
- * @since 0.4
- *
- * @param boolean $throwErrors
- */
- public function __construct( MapPatcher $mapPatcher ) {
- $this->mapPatcher = $mapPatcher;
- }
- /**
- * @since 0.4
- *
- * @param Entity $oldEntity
- * @param EntityDiff $patch
- *
- * @return Entity
- * @throws MWException
- */
- public final function getPatchedEntity( Entity $entity, EntityDiff $patch ) {
- $newEntity = $this->entityToArray( $entity );
- $newEntity = $this->mapPatcher->patch( $newEntity, $patch );
- return $this->entityFromArray( $newEntity, $entity );
- }
- /**
- * @since 0.4
- *
- * @param array $entity
- * @param Entity $originalEntity
- *
- * @return Entity
- */
- protected final function entityFromArray( array $entity, Entity $originalEntity ) {
- $originalEntity->clear();
- foreach ( $entity['aliases'] as $langCode => $aliases ) {
- $originalEntity->setAliases( $langCode, $aliases );
- }
- foreach ( $entity['label'] as $langCode => $label ) {
- $originalEntity->setLabel( $langCode, $label );
- }
- foreach ( $entity['description'] as $langCode => $description ) {
- $originalEntity->setDescription( $langCode, $description );
- }
- // TODO: claims
- $this->setSpecificObjectFields( $entity, $originalEntity );
- return $originalEntity;
- }
- /**
- * @since 0.4
- *
- * @param array $entityArray
- * @param Entity $entityObject
- */
- protected function setSpecificObjectFields( array $entityArray, Entity &$entityObject ) {
- // No-op, meant to be overridden in deriving classes to add specific behaviour
- }
- /**
- * @since 0.4
- *
- * @param Entity $entity
- *
- * @return array
- */
- protected final function entityToArray( Entity $entity ) {
- $array = array();
- $array['aliases'] = $entity->getAllAliases();
- $array['label'] = $entity->getLabels();
- $array['description'] = $entity->getDescriptions();
- // TODO: claims
- $this->setSpecificArrayFields( $array, $entity );
- return $array;
- }
- /**
- * @since 0.4
- *
- * @param array $entityArray
- * @param Entity $entityObject
- */
- protected function setSpecificArrayFields( array &$entityArray, Entity $entityObject ) {
- // No-op, meant to be overridden in deriving classes to add specific behaviour
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement