1<?php 2 3namespace FINDOLOGIC\Export; 4 5use FINDOLOGIC\Export\CSV\CSVExporter; 6use FINDOLOGIC\Export\Data\Item; 7use FINDOLOGIC\Export\XML\XMLExporter; 8 9abstract class Exporter 10{ 11 /** 12 * XML-based export format. 13 * 14 * @see https://docs.findologic.com/doku.php?id=export_patterns:xml 15 */ 16 const TYPE_XML = 0; 17 18 /** 19 * CSV-based export format. Does not support usergroups. 20 * 21 * @see https://docs.findologic.com/doku.php?id=export_patterns:csv 22 */ 23 const TYPE_CSV = 1; 24 25 /** 26 * Creates an exporter for the desired output format. 27 * 28 * @param self::TYPE_XML|self::TYPE_CSV $type The type of export format to choose. 29 * @param int $itemsPerPage Number of items being exported at once. Respecting this parameter is at the exporter 30 * implementation's discretion. 31 * @return Exporter The exporter for the desired output format. 32 */ 33 public static function create($type, $itemsPerPage = 20) 34 { 35 if ($itemsPerPage < 1) { 36 throw new \InvalidArgumentException('At least one item must be exported per page.'); 37 } 38 39 switch ($type) { 40 case self::TYPE_XML: 41 $exporter = new XMLExporter($itemsPerPage); 42 break; 43 case self::TYPE_CSV: 44 $exporter = new CSVExporter($itemsPerPage); 45 break; 46 default: 47 throw new \InvalidArgumentException('Unsupported exporter type.'); 48 } 49 50 return $exporter; 51 } 52 53 protected $itemsPerPage; 54 55 protected function __construct($itemsPerPage) 56 { 57 $this->itemsPerPage = $itemsPerPage; 58 } 59 60 /** 61 * Turns the provided items into their serialized form. 62 * 63 * @param array $items Array of items to serialize. All of them are serialized, regardless of $start and $total. 64 * @param int $start Assuming that $items is a fragment of the total, this is the global index of the first item in 65 * $items. 66 * @param int $count The number of items requested for this export step. Actual number of items can be smaller due 67 * to errors, and can not be greater than the requested count, because that would indicate that the requested 68 * count is ignored when generating items. This value is ignored when using CSV exporter. 69 * @param int $total The global total of items that could be exported. This value is ignored when using CSV exporter. 70 * @return string The items in serialized form. 71 */ 72 abstract public function serializeItems($items, $start, $count, $total); 73 74 /** 75 * Like serializeItems(), but the output is written to filesystem instead of being returned. 76 * 77 * @param string $targetDirectory The directory to which the file is written. The filename is at the exporter 78 * implementation's discretion. 79 * @param array $items Array of items to serialize. All of them are serialized, regardless of $start and $total. 80 * @param int $start Assuming that $items is a fragment of the total, this is the global index of the first item in 81 * $items. 82 * @param int $count The number of items requested for this export step. Actual number of items can be smaller due 83 * to errors, and can not be greater than the requested count, because that would indicate that the requested 84 * count is ignored when generating items. This value is ignored when using CSV exporter. 85 * @param int $total The global total of items that could be exported. This value is ignored when using CSV exporter. 86 * @return string Full path of the written file. 87 */ 88 abstract public function serializeItemsToFile($targetDirectory, $items, $start, $count, $total); 89 90 /** 91 * Creates an export format-specific item instance. 92 * 93 * @param string $id Unique ID of the item. 94 * @return Item The newly generated item. 95 */ 96 abstract public function createItem($id); 97} 98