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
|
|
|
/** |
27
|
|
|
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) |
28
|
|
|
* @param string $associatedWorkbookId ID of the sheet's associated workbook |
29
|
|
|
* @param SheetManager $sheetManager |
30
|
|
|
*/ |
31
|
107 |
|
public function __construct($sheetIndex, $associatedWorkbookId, SheetManager $sheetManager) |
32
|
|
|
{ |
33
|
107 |
|
$this->index = $sheetIndex; |
34
|
107 |
|
$this->associatedWorkbookId = $associatedWorkbookId; |
35
|
|
|
|
36
|
107 |
|
$this->sheetManager = $sheetManager; |
|
|
|
|
37
|
107 |
|
$this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId); |
38
|
|
|
|
39
|
107 |
|
$this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1)); |
40
|
107 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @api |
44
|
|
|
* @return int Index of the sheet, based on order in the workbook (zero-based) |
45
|
|
|
*/ |
46
|
107 |
|
public function getIndex() |
47
|
|
|
{ |
48
|
107 |
|
return $this->index; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
107 |
|
public function getAssociatedWorkbookId() |
55
|
|
|
{ |
56
|
107 |
|
return $this->associatedWorkbookId; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @api |
61
|
|
|
* @return string Name of the sheet |
62
|
|
|
*/ |
63
|
107 |
|
public function getName() |
64
|
|
|
{ |
65
|
107 |
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the name of the sheet. Note that Excel has some restrictions on the name: |
70
|
|
|
* - it should not be blank |
71
|
|
|
* - it should not exceed 31 characters |
72
|
|
|
* - it should not contain these characters: \ / ? * : [ or ] |
73
|
|
|
* - it should be unique |
74
|
|
|
* |
75
|
|
|
* @api |
76
|
|
|
* @param string $name Name of the sheet |
77
|
|
|
* @return Sheet |
78
|
|
|
* @throws \Box\Spout\Writer\Exception\InvalidSheetNameException If the sheet's name is invalid. |
79
|
|
|
*/ |
80
|
107 |
|
public function setName($name) |
81
|
|
|
{ |
82
|
107 |
|
$this->sheetManager->throwIfNameIsInvalid($name, $this); |
83
|
|
|
|
84
|
107 |
|
$this->name = $name; |
85
|
|
|
|
86
|
107 |
|
$this->sheetManager->markSheetNameAsUsed($this); |
87
|
|
|
|
88
|
107 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: