Skip to content

Commit

Permalink
Merge pull request #3 from byjg/4.9
Browse files Browse the repository at this point in the history
Allow to change Property Index in the Iterator
  • Loading branch information
byjg authored Jan 4, 2024
2 parents 9deb0da + 94733c8 commit 189c5d4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 39 deletions.
19 changes: 9 additions & 10 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ jobs:
strategy:
matrix:
php-version:
- "8.2"
- "8.1"
- "8.0"
- "7.4"
- "7.3"
- "7.2"

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/phpunit

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

22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# AnyDataset-Array

[![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)
[![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)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/anydataset-array/)
[![GitHub license](https://img.shields.io/github/license/byjg/anydataset-array.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/anydataset-array.svg)](https://github.com/byjg/anydataset-array/releases/)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-anydataset-array/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-anydataset-array.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-anydataset-array.svg)](https://github.com/byjg/php-anydataset-array/releases/)


Array abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.
Array abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.

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

Expand Down Expand Up @@ -111,10 +110,10 @@ foreach ($iterator as $row) {

## Install

Just type:
Just type:

```bash
composer require "byjg/anydataset-array=4.0.*"
composer require "byjg/anydataset-array"
```

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

## Dependencies

```mermaid
flowchart TD
byjg/anydataset-array --> byjg/anydataset
```

----
[Open source ByJG](http://opensource.byjg.com)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=5.6.0",
"php": ">=7.4",
"byjg/anydataset": "4.9.*"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ and open the template in the editor.
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

</phpunit>
22 changes: 14 additions & 8 deletions src/ArrayDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,33 @@ class ArrayDataset
* @var array
*/
protected $array;
/**
* @var mixed|string
*/
protected $propertyIndexName;
/**
* @var mixed|string
*/
protected $propertyKeyName;

/**
* Constructor Method
*
* @param array $array
* @param string $fieldName
* @param string $property The name of the field if the item is not an array or object
*/
public function __construct($array, $fieldName = "value")
public function __construct(array $array, string $property = "value", $propertyIndexName = "__id", $propertyKeyName = "__key")
{

if (!is_array($array)) {
throw new UnexpectedValueException("You need to pass an array");
}
$this->propertyIndexName = $propertyIndexName;
$this->propertyKeyName = $propertyKeyName;

$this->array = [];

foreach ($array as $key => $value) {
if (is_array($value)) {
$this->array[$key] = $value;
} elseif (!is_object($value)) {
$this->array[$key] = array($fieldName => $value);
$this->array[$key] = array($property => $value);
} else {
$result = array("__class" => get_class($value));
$methods = get_class_methods($value);
Expand All @@ -57,6 +63,6 @@ public function __construct($array, $fieldName = "value")
*/
public function getIterator($filter = null)
{
return new ArrayDatasetIterator($this->array, $filter);
return new ArrayDatasetIterator($this->array, $filter, $this->propertyIndexName, $this->propertyKeyName);
}
}
22 changes: 19 additions & 3 deletions src/ArrayDatasetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ class ArrayDatasetIterator extends GenericIterator
* @var IteratorFilter
*/
protected $filter;
/**
* @var mixed|string
*/
protected $propertyIndexName;
/**
* @var mixed|string
*/
protected $propertyKeyName;

/**
* @param array $rows
* @param IteratorFilter $filter
*/
public function __construct($rows, $filter)
public function __construct($rows, $filter, $propertyIndexName = "__id", $propertyKeyName = "__key")
{
if (!is_array($rows)) {
throw new InvalidArgumentException("ArrayDatasetIterator must receive an array");
Expand All @@ -51,6 +59,10 @@ public function __construct($rows, $filter)
$this->rows = $rows;
$this->keys = array_keys($rows);
$this->filter = $filter;

$this->propertyIndexName = $propertyIndexName;
$this->propertyKeyName = $propertyKeyName;

}

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

$arr = [];
$arr["__id"] = $ix;
$arr["__key"] = $key;
if (!empty($this->propertyIndexName)) {
$arr[$this->propertyIndexName] = $ix;
}
if (!empty($this->propertyKeyName)) {
$arr[$this->propertyKeyName] = $key;
}
foreach ($cols as $key => $value) {
$arr[strtolower($key)] = $value;
}
Expand Down
30 changes: 24 additions & 6 deletions tests/ArrayDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ByJG\AnyDataset\Core\Enum\Relation;
use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Core\IteratorInterface;
use ByJG\AnyDataset\Core\Row;
use ByJG\AnyDataset\Lists\ArrayDataset;
use PHPUnit\Framework\TestCase;
use Tests\AnyDataset\Sample\ModelGetter;
Expand All @@ -24,12 +25,6 @@ class ArrayDatasetTest extends TestCase
"B" => array('code' => 1001, 'name' => "ProdB"),
"C" => array('code' => 1002, 'name' => "ProdC"));

public function testInvalidConstructor()
{
$this->expectException(UnexpectedValueException::class);
new ArrayDataset('aaa');
}

public function testcreateArrayIteratorSample1()
{
$arrayDataset = new ArrayDataset($this->SAMPLE1);
Expand Down Expand Up @@ -356,5 +351,28 @@ public function testEmptyArray()

$this->assertEquals(0, $iterator->count());
}

public function testPropertyKeyName()
{
$dataset = new ArrayDataset($this->SAMPLE1, "value", "id", "name");

$iterator = $dataset->getIterator();

$row = $iterator->moveNext();
$this->assertField($row, "id", 0);
$this->assertField($row, "name", 0);
$this->assertField($row, "value", "ProdA");

}

public function testPropertyKeyNameEmpty()
{
$dataset = new ArrayDataset($this->SAMPLE1, "value", null, null);

$iterator = $dataset->getIterator();

$row = $iterator->moveNext();
$this->assertEquals(["value" => 'ProdA'], $row->toArray());
}
}

0 comments on commit 189c5d4

Please sign in to comment.