1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MisterIcy\ExcelWriter\Properties; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use MisterIcy\ExcelWriter\Exceptions\PropertyException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PropertyCollection |
12
|
|
|
* @package MisterIcy\ExcelWriter\Properties |
13
|
|
|
*/ |
14
|
|
|
final class PropertyCollection |
15
|
|
|
{ |
16
|
|
|
/** @var ArrayCollection */ |
17
|
|
|
private $collection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PropertyCollection constructor. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->collection = new ArrayCollection(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PropertyInterface $property |
29
|
|
|
* @return self |
30
|
|
|
*/ |
31
|
|
|
public function addProperty(PropertyInterface $property) |
32
|
|
|
{ |
33
|
|
|
$this->collection->add($property); |
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param PropertyInterface $property |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public function removeProperty(PropertyInterface $property) |
42
|
|
|
{ |
43
|
|
|
if ($this->collection->contains($property)) { |
44
|
|
|
$this->collection->removeElement($property); |
45
|
|
|
} |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ArrayCollection |
51
|
|
|
*/ |
52
|
|
|
public function getProperties() |
53
|
|
|
{ |
54
|
|
|
return $this->collection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param PropertyInterface $property |
59
|
|
|
* @return int |
60
|
|
|
* @throws PropertyException |
61
|
|
|
*/ |
62
|
|
|
public function getColumnOfProperty(PropertyInterface $property) : int |
63
|
|
|
{ |
64
|
|
|
if ($this->getProperties()->contains($property)) { |
65
|
|
|
return $this->getProperties()->indexOf($property) +1; |
66
|
|
|
} |
67
|
|
|
throw new PropertyException("The specified property is not a part of the collection"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param PropertyInterface $property |
72
|
|
|
* @return string |
73
|
|
|
* @throws PropertyException |
74
|
|
|
*/ |
75
|
|
|
public function getExcelColumnOfProperty(PropertyInterface $property) : string |
76
|
|
|
{ |
77
|
|
|
return Coordinate::stringFromColumnIndex($this->getColumnOfProperty($property)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $propertyPath |
82
|
|
|
* @return int |
83
|
|
|
* @throws PropertyException |
84
|
|
|
*/ |
85
|
|
|
public function getColumnOfPropertyPath(string $propertyPath) : int |
86
|
|
|
{ |
87
|
|
|
/** |
88
|
|
|
* @var int $key |
89
|
|
|
* @var AbstractProperty $property |
90
|
|
|
*/ |
91
|
|
|
foreach ($this->getProperties()->toArray() as $key => $property) { |
92
|
|
|
if (is_null($property->getPath())) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
if (strcmp($propertyPath, $property->getPath()) === 0) { |
96
|
|
|
return $key + 1; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
throw new PropertyException("The specified property path is not found in any property of the collection"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $propertyPath |
104
|
|
|
* @return string |
105
|
|
|
* @throws PropertyException |
106
|
|
|
*/ |
107
|
|
|
public function getExcelColumnOfPropertyPath(string $propertyPath) : string |
108
|
|
|
{ |
109
|
|
|
return Coordinate::stringFromColumnIndex($this->getColumnOfPropertyPath($propertyPath)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|