PHPExcel   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 111
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 16 3
A __construct() 0 6 1
A key() 0 3 1
A rewind() 0 3 1
A valid() 0 3 1
A next() 0 3 1
A add() 0 10 2
A close() 0 2 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/licenses/lgpl.html
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MW
8
 * @subpackage Container
9
 */
10
11
12
namespace Aimeos\MW\Container\Content;
13
14
15
/**
16
 * Implementation of the PHPExcel content object.
17
 *
18
 * @package MW
19
 * @subpackage Container
20
 */
21
class PHPExcel
22
	extends \Aimeos\MW\Container\Content\Base
23
	implements \Aimeos\MW\Container\Content\Iface
24
{
25
	private $sheet;
26
	private $iterator;
27
28
29
	/**
30
	 * Initializes the PHPExcel content object.
31
	 *
32
	 * @param \PHPExcel_Worksheet $sheet PHPExcel sheet
33
	 * @param array $options Associative list of key/value pairs for configuration
34
	 */
35
	public function __construct( \PHPExcel_Worksheet $sheet, $name, array $options = [] )
36
	{
37
		parent::__construct( $sheet, $name, $options );
38
39
		$this->sheet = $sheet;
40
		$this->iterator = $sheet->getRowIterator();
41
	}
42
43
44
	/**
45
	 * Cleans up and saves the content.
46
	 * Does nothing for PHPExcel sheets.
47
	 */
48
	public function close()
49
	{
50
	}
51
52
53
	/**
54
	 * Adds a row to the content object.
55
	 *
56
	 * @param mixed $data Data to add
57
	 */
58
	public function add( $data )
59
	{
60
		$columnNum = 0;
61
		$rowNum = $this->iterator->current()->getRowIndex();
62
63
		foreach( (array) $data as $value ) {
64
			$this->sheet->setCellValueByColumnAndRow( $columnNum++, $rowNum, $value );
65
		}
66
67
		$this->iterator->next();
68
	}
69
70
71
	/**
72
	 * Return the current row.
73
	 *
74
	 * @return array List of values
75
	 */
76
	public function current()
77
	{
78
		if( $this->iterator->valid() === false ) {
79
			return null;
80
		}
81
82
		$iterator = $this->iterator->current()->getCellIterator();
83
		$iterator->setIterateOnlyExistingCells( false );
84
85
		$result = [];
86
87
		foreach( $iterator as $cell ) {
88
			$result[] = $cell->getValue();
89
		}
90
91
		return $result;
92
	}
93
94
95
	/**
96
	 * Returns the key of the current row.
97
	 *
98
	 * @return integer Position within the PHPExcel sheet
99
	 */
100
	public function key()
101
	{
102
		return $this->iterator->key();
103
	}
104
105
106
	/**
107
	 * Moves forward to next row.
108
	 */
109
	public function next()
110
	{
111
		$this->iterator->next();
112
	}
113
114
115
	/**
116
	 * Resets the current row to the beginning of the sheet.
117
	 */
118
	public function rewind()
119
	{
120
		$this->iterator->rewind();
121
	}
122
123
124
	/**
125
	 * Checks if the current position is valid.
126
	 *
127
	 * @return boolean True on success or false on failure
128
	 */
129
	public function valid()
130
	{
131
		return $this->iterator->valid();
132
	}
133
}
134