Skip to content

Commit 189c5d4

Browse files
authored
Merge pull request #3 from byjg/4.9
Allow to change Property Index in the Iterator
2 parents 9deb0da + 94733c8 commit 189c5d4

File tree

7 files changed

+84
-39
lines changed

7 files changed

+84
-39
lines changed

.github/workflows/phpunit.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19+
- "8.2"
1920
- "8.1"
2021
- "8.0"
2122
- "7.4"
22-
- "7.3"
23-
- "7.2"
2423

2524
steps:
26-
- uses: actions/checkout@v3
25+
- uses: actions/checkout@v4
2726
- run: composer install
2827
- run: ./vendor/bin/phpunit
2928

3029
Documentation:
31-
runs-on: 'ubuntu-latest'
32-
needs: Build
3330
if: github.ref == 'refs/heads/master'
34-
env:
35-
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
36-
steps:
37-
- uses: actions/checkout@v3
38-
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php anydataset-array
31+
needs: Build
32+
uses: byjg/byjg.github.io/.github/workflows/add-doc.yaml@master
33+
with:
34+
folder: php
35+
project: ${{ github.event.repository.name }}
36+
secrets: inherit
37+

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# AnyDataset-Array
22

3-
[![Build Status](https://github.com/byjg/anydataset-array/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/anydataset-array/actions/workflows/phpunit.yml)
3+
[![Build Status](https://github.com/byjg/php-anydataset-array/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-anydataset-array/actions/workflows/phpunit.yml)
44
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
5-
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/anydataset-array/)
6-
[![GitHub license](https://img.shields.io/github/license/byjg/anydataset-array.svg)](https://opensource.byjg.com/opensource/licensing.html)
7-
[![GitHub release](https://img.shields.io/github/release/byjg/anydataset-array.svg)](https://github.com/byjg/anydataset-array/releases/)
5+
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-anydataset-array/)
6+
[![GitHub license](https://img.shields.io/github/license/byjg/php-anydataset-array.svg)](https://opensource.byjg.com/opensource/licensing.html)
7+
[![GitHub release](https://img.shields.io/github/release/byjg/php-anydataset-array.svg)](https://github.com/byjg/php-anydataset-array/releases/)
88

9-
10-
Array abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.
9+
Array abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.
1110

1211
See more about Anydataset [here](https://opensource.byjg.com/php/anydataset).
1312

@@ -111,10 +110,10 @@ foreach ($iterator as $row) {
111110

112111
## Install
113112

114-
Just type:
113+
Just type:
115114

116115
```bash
117-
composer require "byjg/anydataset-array=4.0.*"
116+
composer require "byjg/anydataset-array"
118117
```
119118

120119
## Running Unit tests
@@ -123,5 +122,12 @@ composer require "byjg/anydataset-array=4.0.*"
123122
vendor/bin/phpunit
124123
```
125124

125+
## Dependencies
126+
127+
```mermaid
128+
flowchart TD
129+
byjg/anydataset-array --> byjg/anydataset
130+
```
131+
126132
----
127133
[Open source ByJG](http://opensource.byjg.com)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"prefer-stable": true,
1010
"minimum-stability": "dev",
1111
"require": {
12-
"php": ">=5.6.0",
12+
"php": ">=7.4",
1313
"byjg/anydataset": "4.9.*"
1414
},
1515
"require-dev": {

phpunit.xml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ and open the template in the editor.
2020
<ini name="display_startup_errors" value="On" />
2121
<ini name="error_reporting" value="E_ALL" />
2222
</php>
23-
23+
2424
<filter>
2525
<whitelist>
2626
<directory>./src</directory>
2727
</whitelist>
2828
</filter>
29-
29+
3030
<testsuites>
3131
<testsuite name="Test Suite">
3232
<directory>./tests</directory>
3333
</testsuite>
3434
</testsuites>
35-
35+
3636
</phpunit>

src/ArrayDataset.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,33 @@ class ArrayDataset
1313
* @var array
1414
*/
1515
protected $array;
16+
/**
17+
* @var mixed|string
18+
*/
19+
protected $propertyIndexName;
20+
/**
21+
* @var mixed|string
22+
*/
23+
protected $propertyKeyName;
1624

1725
/**
1826
* Constructor Method
1927
*
2028
* @param array $array
21-
* @param string $fieldName
29+
* @param string $property The name of the field if the item is not an array or object
2230
*/
23-
public function __construct($array, $fieldName = "value")
31+
public function __construct(array $array, string $property = "value", $propertyIndexName = "__id", $propertyKeyName = "__key")
2432
{
25-
26-
if (!is_array($array)) {
27-
throw new UnexpectedValueException("You need to pass an array");
28-
}
33+
$this->propertyIndexName = $propertyIndexName;
34+
$this->propertyKeyName = $propertyKeyName;
2935

3036
$this->array = [];
3137

3238
foreach ($array as $key => $value) {
3339
if (is_array($value)) {
3440
$this->array[$key] = $value;
3541
} elseif (!is_object($value)) {
36-
$this->array[$key] = array($fieldName => $value);
42+
$this->array[$key] = array($property => $value);
3743
} else {
3844
$result = array("__class" => get_class($value));
3945
$methods = get_class_methods($value);
@@ -57,6 +63,6 @@ public function __construct($array, $fieldName = "value")
5763
*/
5864
public function getIterator($filter = null)
5965
{
60-
return new ArrayDatasetIterator($this->array, $filter);
66+
return new ArrayDatasetIterator($this->array, $filter, $this->propertyIndexName, $this->propertyKeyName);
6167
}
6268
}

src/ArrayDatasetIterator.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ class ArrayDatasetIterator extends GenericIterator
3636
* @var IteratorFilter
3737
*/
3838
protected $filter;
39+
/**
40+
* @var mixed|string
41+
*/
42+
protected $propertyIndexName;
43+
/**
44+
* @var mixed|string
45+
*/
46+
protected $propertyKeyName;
3947

4048
/**
4149
* @param array $rows
4250
* @param IteratorFilter $filter
4351
*/
44-
public function __construct($rows, $filter)
52+
public function __construct($rows, $filter, $propertyIndexName = "__id", $propertyKeyName = "__key")
4553
{
4654
if (!is_array($rows)) {
4755
throw new InvalidArgumentException("ArrayDatasetIterator must receive an array");
@@ -51,6 +59,10 @@ public function __construct($rows, $filter)
5159
$this->rows = $rows;
5260
$this->keys = array_keys($rows);
5361
$this->filter = $filter;
62+
63+
$this->propertyIndexName = $propertyIndexName;
64+
$this->propertyKeyName = $propertyKeyName;
65+
5466
}
5567

5668
/**
@@ -81,8 +93,12 @@ public function hasNext()
8193
$cols = $this->rows[$key];
8294

8395
$arr = [];
84-
$arr["__id"] = $ix;
85-
$arr["__key"] = $key;
96+
if (!empty($this->propertyIndexName)) {
97+
$arr[$this->propertyIndexName] = $ix;
98+
}
99+
if (!empty($this->propertyKeyName)) {
100+
$arr[$this->propertyKeyName] = $key;
101+
}
86102
foreach ($cols as $key => $value) {
87103
$arr[strtolower($key)] = $value;
88104
}

tests/ArrayDatasetTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ByJG\AnyDataset\Core\Enum\Relation;
66
use ByJG\AnyDataset\Core\IteratorFilter;
77
use ByJG\AnyDataset\Core\IteratorInterface;
8+
use ByJG\AnyDataset\Core\Row;
89
use ByJG\AnyDataset\Lists\ArrayDataset;
910
use PHPUnit\Framework\TestCase;
1011
use Tests\AnyDataset\Sample\ModelGetter;
@@ -24,12 +25,6 @@ class ArrayDatasetTest extends TestCase
2425
"B" => array('code' => 1001, 'name' => "ProdB"),
2526
"C" => array('code' => 1002, 'name' => "ProdC"));
2627

27-
public function testInvalidConstructor()
28-
{
29-
$this->expectException(UnexpectedValueException::class);
30-
new ArrayDataset('aaa');
31-
}
32-
3328
public function testcreateArrayIteratorSample1()
3429
{
3530
$arrayDataset = new ArrayDataset($this->SAMPLE1);
@@ -356,5 +351,28 @@ public function testEmptyArray()
356351

357352
$this->assertEquals(0, $iterator->count());
358353
}
354+
355+
public function testPropertyKeyName()
356+
{
357+
$dataset = new ArrayDataset($this->SAMPLE1, "value", "id", "name");
358+
359+
$iterator = $dataset->getIterator();
360+
361+
$row = $iterator->moveNext();
362+
$this->assertField($row, "id", 0);
363+
$this->assertField($row, "name", 0);
364+
$this->assertField($row, "value", "ProdA");
365+
366+
}
367+
368+
public function testPropertyKeyNameEmpty()
369+
{
370+
$dataset = new ArrayDataset($this->SAMPLE1, "value", null, null);
371+
372+
$iterator = $dataset->getIterator();
373+
374+
$row = $iterator->moveNext();
375+
$this->assertEquals(["value" => 'ProdA'], $row->toArray());
376+
}
359377
}
360378

0 commit comments

Comments
 (0)