ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Address.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Address
9 {
10  public const ADDRESS_ABSOLUTE = 1;
11  public const ADDRESS_COLUMN_RELATIVE = 2;
12  public const ADDRESS_ROW_RELATIVE = 3;
13  public const ADDRESS_RELATIVE = 4;
14 
15  public const REFERENCE_STYLE_A1 = true;
16  public const REFERENCE_STYLE_R1C1 = false;
17 
40  public static function cell($row, $column, $relativity = 1, $referenceStyle = true, $sheetName = '')
41  {
43  $column = Functions::flattenSingleValue($column);
44  $relativity = ($relativity === null) ? 1 : Functions::flattenSingleValue($relativity);
45  $referenceStyle = ($referenceStyle === null) ? true : Functions::flattenSingleValue($referenceStyle);
46  $sheetName = Functions::flattenSingleValue($sheetName);
47 
48  if (($row < 1) || ($column < 1)) {
49  return Functions::VALUE();
50  }
51 
52  $sheetName = self::sheetName($sheetName);
53 
54  if ((!is_bool($referenceStyle)) || $referenceStyle === self::REFERENCE_STYLE_A1) {
55  return self::formatAsA1($row, $column, $relativity, $sheetName);
56  }
57 
58  return self::formatAsR1C1($row, $column, $relativity, $sheetName);
59  }
60 
61  private static function sheetName(string $sheetName)
62  {
63  if ($sheetName > '') {
64  if (strpos($sheetName, ' ') !== false || strpos($sheetName, '[') !== false) {
65  $sheetName = "'{$sheetName}'";
66  }
67  $sheetName .= '!';
68  }
69 
70  return $sheetName;
71  }
72 
73  private static function formatAsA1(int $row, int $column, int $relativity, string $sheetName): string
74  {
75  $rowRelative = $columnRelative = '$';
76  if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
77  $columnRelative = '';
78  }
79  if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
80  $rowRelative = '';
81  }
82  $column = Coordinate::stringFromColumnIndex($column);
83 
84  return "{$sheetName}{$columnRelative}{$column}{$rowRelative}{$row}";
85  }
86 
87  private static function formatAsR1C1(int $row, int $column, int $relativity, string $sheetName): string
88  {
89  if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
90  $column = "[{$column}]";
91  }
92  if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
93  $row = "[{$row}]";
94  }
95 
96  return "{$sheetName}R{$row}C{$column}";
97  }
98 }
static formatAsA1(int $row, int $column, int $relativity, string $sheetName)
Definition: Address.php:73
static cell($row, $column, $relativity=1, $referenceStyle=true, $sheetName='')
ADDRESS.
Definition: Address.php:40
static formatAsR1C1(int $row, int $column, int $relativity, string $sheetName)
Definition: Address.php:87
$row
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static stringFromColumnIndex($columnIndex)
String from column index.
Definition: Coordinate.php:313