1 | <?php |
||
16 | class ItemMerger { |
||
17 | |||
18 | private WikibaseApi $api; |
||
|
|||
19 | |||
20 | /** |
||
21 | * @param WikibaseApi $api |
||
22 | */ |
||
23 | public function __construct( WikibaseApi $api ) { |
||
24 | $this->api = $api; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @since 0.2 |
||
29 | * @param Item|ItemId|string $from |
||
30 | * @param Item|ItemId|string $to |
||
31 | * @param EditInfo|null $editInfo |
||
32 | */ |
||
33 | public function merge( $from, $to, EditInfo $editInfo = null ): bool { |
||
34 | $params = [ |
||
35 | 'fromid' => $this->getIdFromInput( $from ), |
||
36 | 'toid' => $this->getIdFromInput( $to ) |
||
37 | ]; |
||
38 | |||
39 | $this->api->postRequest( 'wbmergeitems', $params, $editInfo ); |
||
40 | return true; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param Item|ItemId|string $input |
||
45 | * |
||
46 | * @throws InvalidArgumentException |
||
47 | * @return string|void the ItemId Serialization |
||
48 | */ |
||
49 | private function getIdFromInput( $input ) { |
||
50 | if ( is_string( $input ) ) { |
||
51 | return $input; |
||
52 | } elseif ( $input instanceof ItemId ) { |
||
53 | return $input->getSerialization(); |
||
54 | } elseif ( $input instanceof Item ) { |
||
55 | return $input->getId()->getSerialization(); |
||
56 | } else { |
||
57 | throw new InvalidArgumentException( 'Merge target should be either string, Item or ItemId' ); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | } |
||
62 |