ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DefinedName.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 abstract class DefinedName
8 {
9  protected const REGEXP_IDENTIFY_FORMULA = '[^_\p{N}\p{L}:, \$\'!]';
10 
16  protected $name;
17 
23  protected $worksheet;
24 
30  protected $value;
31 
37  protected $localOnly;
38 
44  protected $scope;
45 
51  protected $isFormula;
52 
56  public function __construct(
57  string $name,
58  ?Worksheet $worksheet = null,
59  ?string $value = null,
60  bool $localOnly = false,
61  ?Worksheet $scope = null
62  ) {
63  if ($worksheet === null) {
65  }
66 
67  // Set local members
68  $this->name = $name;
69  $this->worksheet = $worksheet;
70  $this->value = (string) $value;
71  $this->localOnly = $localOnly;
72  // If local only, then the scope will be set to worksheet unless a scope is explicitly set
73  $this->scope = ($localOnly === true) ? (($scope === null) ? $worksheet : $scope) : null;
74  // If the range string contains characters that aren't associated with the range definition (A-Z,1-9
75  // for cell references, and $, or the range operators (colon comma or space), quotes and ! for
76  // worksheet names
77  // then this is treated as a named formula, and not a named range
78  $this->isFormula = self::testIfFormula($this->value);
79  }
80 
84  public static function createInstance(
85  string $name,
86  ?Worksheet $worksheet = null,
87  ?string $value = null,
88  bool $localOnly = false,
89  ?Worksheet $scope = null
90  ): self {
91  $value = (string) $value;
92  $isFormula = self::testIfFormula($value);
93  if ($isFormula) {
94  return new NamedFormula($name, $worksheet, $value, $localOnly, $scope);
95  }
96 
97  return new NamedRange($name, $worksheet, $value, $localOnly, $scope);
98  }
99 
100  public static function testIfFormula(string $value): bool
101  {
102  if (substr($value, 0, 1) === '=') {
103  $value = substr($value, 1);
104  }
105 
106  if (is_numeric($value)) {
107  return true;
108  }
109 
110  $segMatcher = false;
111  foreach (explode("'", $value) as $subVal) {
112  // Only test in alternate array entries (the non-quoted blocks)
113  if (
114  ($segMatcher = !$segMatcher) &&
115  (preg_match('/' . self::REGEXP_IDENTIFY_FORMULA . '/miu', $subVal))
116  ) {
117  return true;
118  }
119  }
120 
121  return false;
122  }
123 
127  public function getName(): string
128  {
129  return $this->name;
130  }
131 
135  public function setName(string $name): self
136  {
137  if (!empty($name)) {
138  // Old title
139  $oldTitle = $this->name;
140 
141  // Re-attach
142  if ($this->worksheet !== null) {
143  $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
144  }
145  $this->name = $name;
146 
147  if ($this->worksheet !== null) {
148  $this->worksheet->getParent()->addNamedRange($this);
149  }
150 
151  // New title
152  $newTitle = $this->name;
153  ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
154  }
155 
156  return $this;
157  }
158 
162  public function getWorksheet(): ?Worksheet
163  {
164  return $this->worksheet;
165  }
166 
170  public function setWorksheet(?Worksheet $value): self
171  {
172  $this->worksheet = $value;
173 
174  return $this;
175  }
176 
180  public function getValue(): string
181  {
182  return $this->value;
183  }
184 
188  public function setValue(string $value): self
189  {
190  $this->value = $value;
191 
192  return $this;
193  }
194 
198  public function getLocalOnly(): bool
199  {
200  return $this->localOnly;
201  }
202 
206  public function setLocalOnly(bool $value): self
207  {
208  $this->localOnly = $value;
209  $this->scope = $value ? $this->worksheet : null;
210 
211  return $this;
212  }
213 
217  public function getScope(): ?Worksheet
218  {
219  return $this->scope;
220  }
221 
225  public function setScope(?Worksheet $value): self
226  {
227  $this->scope = $value;
228  $this->localOnly = $value !== null;
229 
230  return $this;
231  }
232 
236  public function isFormula(): bool
237  {
238  return $this->isFormula;
239  }
240 
244  public static function resolveName(string $pDefinedName, Worksheet $pSheet, string $sheetName = ''): ?self
245  {
246  if ($sheetName === '') {
247  $pSheet2 = $pSheet;
248  } else {
249  $pSheet2 = $pSheet->getParent()->getSheetByName($sheetName);
250  if ($pSheet2 === null) {
251  return null;
252  }
253  }
254 
255  return $pSheet->getParent()->getDefinedName($pDefinedName, $pSheet2);
256  }
257 
261  public function __clone()
262  {
263  $vars = get_object_vars($this);
264  foreach ($vars as $key => $value) {
265  if (is_object($value)) {
266  $this->$key = clone $value;
267  } else {
268  $this->$key = $value;
269  }
270  }
271  }
272 }
static createInstance(string $name, ?Worksheet $worksheet=null, ?string $value=null, bool $localOnly=false, ?Worksheet $scope=null)
Create a new defined name, either a range or a formula.
Definition: DefinedName.php:84
static testIfFormula(string $value)
setValue(string $value)
Set range or formula value.
getValue()
Get range or formula value.
setScope(?Worksheet $value)
Set scope.
setName(string $name)
Set name.
setLocalOnly(bool $value)
Set localOnly.
static resolveName(string $pDefinedName, Worksheet $pSheet, string $sheetName='')
Resolve a named range to a regular cell range or formula.
isFormula()
Identify whether this is a named range or a named formula.
static getInstance()
Get an instance of this class.
__clone()
Implement PHP __clone to create a deep clone, not just a shallow copy.
$key
Definition: croninfo.php:18
setWorksheet(?Worksheet $value)
Set worksheet.
__construct(string $name, ?Worksheet $worksheet=null, ?string $value=null, bool $localOnly=false, ?Worksheet $scope=null)
Create a new Defined Name.
Definition: DefinedName.php:56