Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23f82f3
commit d7b8ba1
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace Keboola\StorageApi\S3Uploader; | ||
|
||
class Chunker | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected $size; | ||
|
||
/** | ||
* Chunker constructor. | ||
* @param int $size | ||
*/ | ||
public function __construct($size = 50) | ||
{ | ||
$this->setSize($size); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getSize() | ||
{ | ||
return $this->size; | ||
} | ||
|
||
/** | ||
* @param int $size | ||
* @return $this | ||
*/ | ||
public function setSize($size) | ||
{ | ||
$this->size = (int) $size; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $items | ||
* @return array | ||
*/ | ||
public function makeChunks($items) | ||
{ | ||
$chunkCount = ceil(count($items) / $this->getSize()); | ||
$chunks = []; | ||
for ($i = 0; $i < $chunkCount; $i++) { | ||
$currentChunk = array_slice( | ||
$items, | ||
$i * $this->getSize(), | ||
$this->getSize() | ||
); | ||
$chunks[] = $currentChunk; | ||
} | ||
return $chunks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Keboola\Test\S3Uploader; | ||
|
||
use Keboola\StorageApi\S3Uploader\Chunker; | ||
use Keboola\Test\StorageApiTestCase; | ||
|
||
class ChunkerTest extends StorageApiTestCase | ||
{ | ||
|
||
public function testSettersAndGetters() | ||
{ | ||
$chunker = new Chunker(); | ||
$this->assertEquals(50, $chunker->getSize()); | ||
$chunker->setSize(10); | ||
$this->assertEquals(10, $chunker->getSize()); | ||
$chunker = new Chunker(5); | ||
$this->assertEquals(5, $chunker->getSize()); | ||
} | ||
|
||
public function testSimpleChunker() | ||
{ | ||
$chunker = new Chunker(3); | ||
$items = [1, 2, 3, 4, 5]; | ||
$expected = [[1, 2, 3], [4, 5]]; | ||
$this->assertEquals($expected, $chunker->makeChunks($items)); | ||
} | ||
|
||
public function testAssociativeArrayChunker() | ||
{ | ||
$chunker = new Chunker(3); | ||
$items = [ | ||
"item1" => 1, | ||
"item2" => 2, | ||
"item3" => 3, | ||
"item4" => 4, | ||
"item5" => 5 | ||
]; | ||
$expected = [ | ||
[ | ||
"item1" => 1, | ||
"item2" => 2, | ||
"item3" => 3 | ||
], | ||
[ | ||
"item4" => 4, | ||
"item5" => 5 | ||
] | ||
]; | ||
$this->assertEquals($expected, $chunker->makeChunks($items)); | ||
} | ||
} |