Skip to content

# PHP AnyDataset 6.0.0 Release #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ jobs:
strategy:
matrix:
php-version:
- "8.4"
- "8.3"
- "8.2"
- "8.1"

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

Documentation:
if: github.ref == 'refs/heads/master'
Expand Down
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current Script in Console",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "PHPUnit Debug",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/vendor/bin/phpunit",
"cwd": "${workspaceFolder}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
}
]
}
201 changes: 57 additions & 144 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,188 +1,101 @@
# AnyDataset

[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![Build Status](https://github.com/byjg/php-anydataset/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-anydataset/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/php-anydataset/)
[![GitHub license](https://img.shields.io/github/license/byjg/anydataset.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/anydataset.svg)](https://github.com/byjg/php-anydataset/releases/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-anydataset.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-anydataset.svg)](https://github.com/byjg/php-anydataset/releases/)

Anydataset Core Module. Anydataset is an agnostic data source abstraction layer in PHP.

## Features

- Access different data sources using the same interface.
- Iterable results
- Convert results to array
# AnyDataset

## Current Implementations
## Overview

| Object | Data Source | Read | Write | Reference |
|------------------------|-----------------------|:----:|:-----:|-----------------------------------------------------|
| DbDriverInterface | Relational DB | yes | yes | [Github](https://github.com/byjg/anydataset-db) |
| AnyDataSet | Anydataset | yes | yes | [Github](https://github.com/byjg/anydataset) |
| ArrayDataSet | Array | yes | no | [Github](https://github.com/byjg/anydataset-array) |
| TextFileDataSet | Delimited Fields | yes | no | [Github](https://github.com/byjg/anydataset-text) |
| FixedTextFileDataSet | Fixed Size fields | yes | no | [Github](https://github.com/byjg/anydataset-text) |
| XmlDataSet | Xml | yes | no | [Github](https://github.com/byjg/anydataset-xml) |
| JSONDataSet | Json | yes | no | [Github](https://github.com/byjg/anydataset-json) |
| SparQLDataSet | SparQl Repositories | yes | no | [Github](https://github.com/byjg/anydataset-sparql) |
| NoSqlDocumentInterface | NoSql Document Based | yes | yes | [Github](https://github.com/byjg/anydataset-nosql) |
| KeyValueInterface | NoSql Key/Value Based | yes | yes | [Github](https://github.com/byjg/anydataset-nosql) |
AnyDataset is a powerful data source abstraction layer for PHP that provides a **simple and consistent interface** to access different data sources.
With AnyDataset, you can work with various data formats and storage systems using the same programming interface.

## Examples
It is the core component of the [Anydataset project](https://packagist.org/providers/byjg/anydataset-implementation), an agnostic data source abstraction layer for PHP.

### Iterating with foreach
## Key Features

```php
<?php
$dataset = new \ByJG\AnyDataset\Core\AnyDataset("example");
- **Unified Interface**: Access different data sources (databases, arrays, XML, JSON, etc.) using the same interface
- **Flexible Iteration**: Multiple ways to iterate through your data
- **Powerful Filtering**: Filter your data using a SQL-like syntax
- **Data Transformation**: Convert between different formats (JSON, XML, arrays)
- **Validation**: Validate your data against rules
- **Extensible**: Create your own data source implementations

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
print $row->toArray();
}
```
## Quick Start

### Filtering results
### Installation

```php
<?php
$filter = new \ByJG\AnyDataset\Core\IteratorFilter();
$filter->addRelation("field1", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, 10);
$iterator2 = $dataset->getIterator($filter);
```bash
composer require "byjg/anydataset"
```

### Converting to Array
### Basic Usage

```php
<?php
$iterator = $dataset->getIterator();
print_r($iterator->toArray());
```

### Iterating with While
# Create a dataset
$dataset = new \ByJG\AnyDataset\Core\AnyDataset("example");

```php
<?php
# Get an iterator
$iterator = $dataset->getIterator();
while ($iterator->hasNext()) {
$row = $iterator->moveNext();

print_r($row->get("field1"));
}
```

or

```php
# Iterate over the results
foreach ($iterator as $row) {
print_r($row->get("field1"));
print_r($row->toArray());
}
```

## Additional Classes
## Documentation

### RowOutpout - Format Field Output
### Core Concepts
- [AnyDataset Overview](docs/anydataset.md) - Core component overview and usage
- [The Row Object](docs/row.md) - Working with data rows
- [Iterators](docs/iterators.md) - Different ways to iterate through data

This class defines custom format for the field output.
### Advanced Features
- [Filtering Results](docs/iteratorfilter.md) - How to filter your data
- [Formatting Output](docs/rowoutput.md) - Transform your data into different formats
- [Field Validation](docs/rowvalidator.md) - Validate your data against rules
- [Data Population](docs/populate.md) - Populate objects with data

```php
<?php
$output = RowOutput::getInstance()
->addFormat("field1", "Test {field1}")
->addFormat("field2", "Showing {} and {field3}");
->addCustomFormat("field3", function ($row, $field, $value) {
// return the formatted output.
// $row: The row object with all values
// $field: The field has been processed
// $value: The field value
});

// This will output the field1 formatted:
echo $output->print($row, "field1");

// This will apply the format defintion to all fields at once:
$ouput->apply($row);
```
## Available Implementations

Notes about the format pattern:
| Data Source Type | Implementation | Read/Write | Repository |
|-----------------------|------------------------|:----------:|-----------------------------------------------------|
| **Core** | AnyDataSet | R/W | [Github](https://github.com/byjg/anydataset) |
| **Databases** | DbDriverInterface | R/W | [Github](https://github.com/byjg/anydataset-db) |
| **Arrays** | ArrayDataSet | R | [Github](https://github.com/byjg/anydataset-array) |
| **Text Files** | TextFileDataSet | R | [Github](https://github.com/byjg/anydataset-text) |
| **Fixed Width Files** | FixedTextFileDataSet | R | [Github](https://github.com/byjg/anydataset-text) |
| **XML** | XmlDataSet | R | [Github](https://github.com/byjg/anydataset-xml) |
| **JSON** | JSONDataSet | R | [Github](https://github.com/byjg/anydataset-json) |
| **SparQL** | SparQLDataSet | R | [Github](https://github.com/byjg/anydataset-sparql) |
| **NoSQL Document** | NoSqlDocumentInterface | R/W | [Github](https://github.com/byjg/anydataset-nosql) |
| **NoSQL Key/Value** | KeyValueInterface | R/W | [Github](https://github.com/byjg/anydataset-nosql) |

- `{}` represents the current value
- `{.}` represents the field name
- `{field_name}` return the value of $row->get(field_name)
## Example Code Snippets

### RowValidator - Validate Field contents
### Filtering Data

```php
<?php
$validator = RowValidator::getInstance()
->requiredFields(["field1", "field2"])
->numericFields(['field1', 'field3'])
->regexValidation("field4", '/\d{4}-\d{2}-\d{2}/')
->customValidation("field3", function($value) {
// Return any string containing the error message if validation FAILS
// otherwise, just return null and the valition will pass.
});

$validator->validate($row) // Will return an array with the error messages. Empty array if not errors.
```

## Formatters

AnyDataset comes with an extensible set to format the AnyDataset. The interface is:

```php
namespace ByJG\AnyDataset\Core\Formatter;

interface FormatterInterface
{
/**
* Return the object in your original format, normally as object
*
* @return mixed
*/
public function raw();

/**
* Return the object transformed to string.
*
* @return string
*/
public function toText();

/**
* Save the contents to a file
*
* @param string $filename
* @return void
*/
public function saveToFile($filename);
}
$filter = new \ByJG\AnyDataset\Core\IteratorFilter();
$filter->addRelation("field1", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, 10);
$iterator = $dataset->getIterator($filter);
```

AnyDataset implements two formatters:

- JsonFormatter
- XmlFormatter

Example:
### Using Formatters

```php
<?php
$formatter = new XmlFormatter($anydataset->getIterator());
$formatter->raw(); // Return a DOM object
$formatter->toText(); // Return the XML as a text
$formatter->saveToFile("/path/to/file.xml"); // Save the XML Text to a file.
```

## Install

```bash
composer require "byjg/anydataset"
$formatter->raw(); // Return a DOM object
$formatter->toText(); // Return the XML as a text
$formatter->saveToFile("/path/to/file.xml"); // Save the XML Text to a file
```

## Running Unit tests
## Running Unit Tests

```bash
vendor/bin/phpunit
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=8.1 <8.4",
"php": ">=8.1 <8.5",
"ext-dom": "*",
"byjg/xmlutil": "^5.0",
"byjg/serializer": "^5.0"
"byjg/xmlutil": "^6.0",
"byjg/serializer": "^6.0"
},
"suggest": {
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^4.24"
"phpunit/phpunit": "^10.5|^11.5",
"vimeo/psalm": "^5.9|^6.2"
},
"provide": {
"byjg/anydataset-implementation": "1.0"
},
"license": "MIT"
}
Loading
Loading