| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Writer\Common\Entity; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Writer\Common\Manager\SheetManager; |
| 6 |
use FluentCart\OpenSpout\Writer\XLSX\Entity\SheetView; |
| 7 |
/** |
| 8 |
* External representation of a worksheet. |
| 9 |
*/ |
| 10 |
class Sheet |
| 11 |
{ |
| 12 |
public const DEFAULT_SHEET_NAME_PREFIX = 'Sheet'; |
| 13 |
/** @var int Index of the sheet, based on order in the workbook (zero-based) */ |
| 14 |
private $index; |
| 15 |
/** @var string ID of the sheet's associated workbook. Used to restrict sheet name uniqueness enforcement to a single workbook */ |
| 16 |
private $associatedWorkbookId; |
| 17 |
/** @var string Name of the sheet */ |
| 18 |
private $name; |
| 19 |
/** @var bool Visibility of the sheet */ |
| 20 |
private $isVisible; |
| 21 |
/** @var SheetManager Sheet manager */ |
| 22 |
private $sheetManager; |
| 23 |
/** @var SheetView */ |
| 24 |
private $sheetView; |
| 25 |
/** |
| 26 |
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) |
| 27 |
* @param string $associatedWorkbookId ID of the sheet's associated workbook |
| 28 |
* @param SheetManager $sheetManager To manage sheets |
| 29 |
*/ |
| 30 |
public function __construct($sheetIndex, $associatedWorkbookId, SheetManager $sheetManager) |
| 31 |
{ |
| 32 |
$this->index = $sheetIndex; |
| 33 |
$this->associatedWorkbookId = $associatedWorkbookId; |
| 34 |
$this->sheetManager = $sheetManager; |
| 35 |
$this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId); |
| 36 |
$this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1)); |
| 37 |
$this->setIsVisible(\true); |
| 38 |
} |
| 39 |
/** |
| 40 |
* @return int Index of the sheet, based on order in the workbook (zero-based) |
| 41 |
*/ |
| 42 |
public function getIndex() |
| 43 |
{ |
| 44 |
return $this->index; |
| 45 |
} |
| 46 |
/** |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getAssociatedWorkbookId() |
| 50 |
{ |
| 51 |
return $this->associatedWorkbookId; |
| 52 |
} |
| 53 |
/** |
| 54 |
* @return string Name of the sheet |
| 55 |
*/ |
| 56 |
public function getName() |
| 57 |
{ |
| 58 |
return $this->name; |
| 59 |
} |
| 60 |
/** |
| 61 |
* Sets the name of the sheet. Note that Excel has some restrictions on the name: |
| 62 |
* - it should not be blank |
| 63 |
* - it should not exceed 31 characters |
| 64 |
* - it should not contain these characters: \ / ? * : [ or ] |
| 65 |
* - it should be unique. |
| 66 |
* |
| 67 |
* @param string $name Name of the sheet |
| 68 |
* |
| 69 |
* @throws \OpenSpout\Writer\Exception\InvalidSheetNameException if the sheet's name is invalid |
| 70 |
* |
| 71 |
* @return Sheet |
| 72 |
*/ |
| 73 |
public function setName($name) |
| 74 |
{ |
| 75 |
$this->sheetManager->throwIfNameIsInvalid($name, $this); |
| 76 |
$this->name = $name; |
| 77 |
$this->sheetManager->markSheetNameAsUsed($this); |
| 78 |
return $this; |
| 79 |
} |
| 80 |
/** |
| 81 |
* @return bool isVisible Visibility of the sheet |
| 82 |
*/ |
| 83 |
public function isVisible() |
| 84 |
{ |
| 85 |
return $this->isVisible; |
| 86 |
} |
| 87 |
/** |
| 88 |
* @param bool $isVisible Visibility of the sheet |
| 89 |
* |
| 90 |
* @return Sheet |
| 91 |
*/ |
| 92 |
public function setIsVisible($isVisible) |
| 93 |
{ |
| 94 |
$this->isVisible = $isVisible; |
| 95 |
return $this; |
| 96 |
} |
| 97 |
public function getSheetView() : ?SheetView |
| 98 |
{ |
| 99 |
return $this->sheetView; |
| 100 |
} |
| 101 |
/** |
| 102 |
* @return $this |
| 103 |
*/ |
| 104 |
public function setSheetView(SheetView $sheetView) |
| 105 |
{ |
| 106 |
$this->sheetView = $sheetView; |
| 107 |
return $this; |
| 108 |
} |
| 109 |
public function hasSheetView() : bool |
| 110 |
{ |
| 111 |
return $this->sheetView instanceof SheetView; |
| 112 |
} |
| 113 |
} |
| 114 |
|