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