33namespace Vitorccs \LaravelCsv \Services ;
44
55use Illuminate \Database \Eloquent \Model ;
6- use Illuminate \Support \Collection ;
7- use Throwable ;
86use Vitorccs \LaravelCsv \Concerns \FromArray ;
97use Vitorccs \LaravelCsv \Concerns \FromCollection ;
108use Vitorccs \LaravelCsv \Concerns \FromQuery ;
11- use Vitorccs \LaravelCsv \Concerns \FromQueryCursor ;
129use Vitorccs \LaravelCsv \Concerns \WithColumnFormatting ;
1310use Vitorccs \LaravelCsv \Concerns \WithHeadings ;
1411use Vitorccs \LaravelCsv \Concerns \WithMapping ;
@@ -54,11 +51,17 @@ public function generate(object $exportable)
5451
5552 if ($ exportable instanceof FromArray) {
5653 $ rows = $ exportable ->array ();
54+ if ($ exportable ->limit ()) {
55+ $ rows = array_splice ($ rows , 0 , $ exportable ->limit ());
56+ }
5757 $ this ->iterateRows ($ exportable , $ rows );
5858 }
5959
6060 if ($ exportable instanceof FromCollection) {
6161 $ rows = $ exportable ->collection ();
62+ if ($ exportable ->limit ()) {
63+ $ rows = $ rows ->take ($ exportable ->limit ());
64+ }
6265 $ this ->iterateRows ($ exportable , $ rows );
6366 }
6467
@@ -72,20 +75,17 @@ public function generate(object $exportable)
7275 );
7376 }
7477
75- if ($ exportable instanceof FromQueryCursor) {
76- $ this ->iterateRows ($ exportable , $ exportable ->query ()->cursor ());
77- }
78-
7978 return $ this ->handler ->getResource ();
8079 }
8180
8281 /**
8382 * @param object $exportable
84- * @param Collection|array $rows
83+ * @param iterable $rows
8584 * @return void
8685 * @throws InvalidCellValueException
8786 */
88- private function iterateRows (object $ exportable , $ rows ): void
87+ private function iterateRows (object $ exportable ,
88+ iterable $ rows ): void
8989 {
9090 $ formats = $ exportable instanceof WithColumnFormatting ? $ exportable ->columnFormats () : [];
9191 $ withMapping = $ exportable instanceof WithMapping;
@@ -98,7 +98,11 @@ private function iterateRows(object $exportable, $rows): void
9898 }
9999 }
100100
101- private function normalizeRow ($ row ): array
101+ /**
102+ * @param mixed $row
103+ * @return array
104+ */
105+ private function normalizeRow (mixed $ row ): array
102106 {
103107 if ($ row instanceof Model) {
104108 $ row = ModelHelper::toArrayValues ($ row );
@@ -113,12 +117,18 @@ private function normalizeRow($row): array
113117 }
114118
115119 /**
120+ * @param array $row
121+ * @param array $formats
122+ * @param int $rowIndex
123+ * @return array
116124 * @throws InvalidCellValueException
117125 */
118- private function applyFormatting (array $ row , array $ formats , int $ rowIndex ): array
126+ private function applyFormatting (array $ row ,
127+ array $ formats ,
128+ int $ rowIndex ): array
119129 {
120130 return array_map (
121- fn ($ value , int $ columnIndex ) => $ this ->formatCellValue ($ value , $ formats , $ rowIndex , $ columnIndex ),
131+ fn ($ value , int $ columnIndex ) => $ this ->formatCellValue ($ value , $ formats , $ rowIndex , $ columnIndex ),
122132 $ row ,
123133 array_keys ($ row )
124134 );
@@ -127,7 +137,10 @@ private function applyFormatting(array $row, array $formats, int $rowIndex): arr
127137 /**
128138 * @throws InvalidCellValueException
129139 */
130- private function formatCellValue ($ value , array $ formats , int $ rowIndex , int $ columnIndex ): string
140+ private function formatCellValue (mixed $ value ,
141+ array $ formats ,
142+ int $ rowIndex ,
143+ int $ columnIndex ): string
131144 {
132145 $ columnLetter = CsvHelper::getColumnLetter ($ columnIndex + 1 );
133146 $ format = $ formats [$ columnLetter ] ?? null ;
@@ -149,10 +162,10 @@ private function formatCellValue($value, array $formats, int $rowIndex, int $col
149162 }
150163
151164 try {
152- if (! is_string ($ value )) {
165+ if (!is_string ($ value )) {
153166 return (string )$ value ;
154167 }
155- } catch (Throwable $ e ) {
168+ } catch (\ Throwable $ e ) {
156169 throw new InvalidCellValueException ("{$ columnLetter }{$ rowIndex }" );
157170 }
158171
0 commit comments