Marks fork as 2.0.0.

This commit is contained in:
Pierre Lannoy 2024-03-30 16:44:38 +01:00
commit 8d8de4a2b0
Signed by: Pierre Lannoy
GPG key ID: D27231EF87D53F31
81 changed files with 1921 additions and 3954 deletions

View file

@ -1,45 +0,0 @@
name: Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-18.04
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "7.2"
coverage: "none"
- uses: actions/checkout@v3
- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md
- name: Run test suite
run: composer run-script test
- name: Check coding style according to PSR-12
run: composer run-script check

View file

@ -1,6 +1,8 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2022 - 2023 David Arendsen Copyright (c)
* 2022 - 2023 David Arendsen
* 2024 Hosterra / Pierre Lannoy
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

View file

@ -1,20 +1,14 @@
# Flux Query Builder # Flux Query Builder
With this query builder you can build queries for Flux. Flux is a powerful open source query language created by the makers of InfluxDB. See https://www.influxdata.com/products/flux/ Flux Query Builder is a package to build [Flux](https://www.influxdata.com/products/flux/) queries in PHP.
[![Latest Stable Version](http://poser.pugx.org/arendsen/fluxquerybuilder/v)](https://packagist.org/packages/arendsen/fluxquerybuilder) >Note: this package is a fork of the David Arendsen [fluxquerybuilder](https://github.com/davidarendsen/fluxquerybuilder) package; original credits go to him.
[![License](http://poser.pugx.org/arendsen/fluxquerybuilder/license)](https://packagist.org/packages/arendsen/fluxquerybuilder)
[![PHP Version Require](http://poser.pugx.org/arendsen/fluxquerybuilder/require/php)](https://packagist.org/packages/arendsen/fluxquerybuilder)
[![Tests](https://github.com/davidarendsen/fluxquerybuilder/actions/workflows/php.yml/badge.svg)](https://github.com/davidarendsen/fluxquerybuilder/actions/workflows/php.yml)
## Documentation ## Documentation
The documentation of this project is available in the [docs folder](docs/00-index.md). The documentation of this project is available in the [docs folder](docs/00-index.md).
Go directly to our [Getting started page](docs/01-getting-started.md)
## Contributing ## Contributing
You're welcome to contribute to this project, but please add tests and follow the coding style. You're welcome to contribute to this project, but please add tests.
### Testing ### Testing
To execute the testing suite: To execute the testing suite:
@ -22,16 +16,3 @@ To execute the testing suite:
``` ```
composer test composer test
``` ```
### Coding style
Run the following commands to check and fix the coding style. We're using the PSR12 standard.
```
composer check
composer format
```
## License
This software is released under the [MIT license](LICENSE).

View file

@ -1,16 +1,16 @@
{ {
"name": "hosterra/fluxbuilder", "name": "hosterra/fluxbuilder",
"description": "A package to build Flux queries in PHP.", "description": "A package to build Flux queries with PHP.",
"keywords": [ "influxdb", "flux" ], "keywords": [ "influxdb", "flux" ],
"license": "MIT", "license": "MIT",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Arendsen\\FluxQueryBuilder\\": "src/", "Hosterra\\FluxBuilder\\": "src/",
"Tests\\": "tests/" "Tests\\": "tests/"
} }
}, },
"require": { "require": {
"php": ">=7.2" "php": ">=8.2"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^8", "phpunit/phpunit": "^8",

1490
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,22 +0,0 @@
version: '3'
services:
code:
image: busybox
command: /bin/true
container_name: fluxquerybuilder
networks:
- api
php:
build: ./docker/php
container_name: fluxquerybuilder_php
volumes:
- ./:/var/www
networks:
- api
networks:
api:
driver: bridge

View file

@ -1,11 +0,0 @@
FROM php:7.2-cli
COPY . /var/www
WORKDIR /var/www
RUN apt-get update && \
apt-get install -y \
curl \
git \
gnupg \
unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View file

@ -4,7 +4,7 @@
Install the package with composer: Install the package with composer:
``` ```
composer require arendsen/fluxquerybuilder composer require hosterra/fluxbuilder
``` ```
The most basic Flux query can be made with the following code. It has filters for fields and tags. The most basic Flux query can be made with the following code. It has filters for fields and tags.
@ -12,8 +12,8 @@ The most basic Flux query can be made with the following code. It has filters fo
```php ```php
<?php <?php
use Arendsen\FluxQueryBuilder\QueryBuilder; use Hosterra\FluxBuilder\QueryBuilder;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter; use Hosterra\FluxBuilder\Expression\KeyFilter;
$queryBuilder = new QueryBuilder(); $queryBuilder = new QueryBuilder();
$queryBuilder->fromBucket('test_bucket') $queryBuilder->fromBucket('test_bucket')
@ -35,12 +35,8 @@ Which will result in the following Flux query:
from(bucket: "test_bucket") from(bucket: "test_bucket")
|> range(start: time(v: 2022-12-28T13:21:11Z)) |> range(start: time(v: 2022-12-28T13:21:11Z))
|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => r._measurement == "test_measurement")
|> filter(fn: (r) => |> filter(fn: (r) => r._field == "username" or r._field == "email" )
r._field == "username" or r._field == "email" |> filter(fn: (r) => r.username == "David" and r.email == "david@example.com" )
)
|> filter(fn: (r) =>
r.username == "David" and r.email == "david@example.com"
)
|> limit(n: 50, offset: 100) |> limit(n: 50, offset: 100)
``` ```

View file

@ -8,9 +8,6 @@ Since InfluxDB2 they introduced this new Flux query language. Coming from a SQL
```js ```js
from(bucket: "example-bucket") from(bucket: "example-bucket")
|> range(start: -1h) |> range(start: -1h)
|> filter(fn: (r) => |> filter(fn: (r) => r._measurement == "example-measurement" and r.tag == "example-tag" )
r._measurement == "example-measurement"
and r.tag == "example-tag"
)
|> filter(fn: (r) => r._field == "example-field") |> filter(fn: (r) => r._field == "example-field")
``` ```

View file

@ -92,7 +92,7 @@ This will result in the following Flux function part:
### Advanced example ### Advanced example
```php ```php
use Arendsen\FluxQueryBuilder\Type\FnType; use Hosterra\FluxBuilder\Type\FnType;
->addAggregateWindow( ->addAggregateWindow(
'20s', '20s',

View file

@ -33,7 +33,7 @@
### Example ### Example
```php ```php
use Arendsen\FluxQueryBuilder\Type\MathType; use Hosterra\FluxBuilder\Type\MathType;
``` ```
```php ```php

View file

@ -1,118 +1,97 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Builder; namespace Hosterra\FluxBuilder\Builder;
use DateTime; use DateTime;
use Arendsen\FluxQueryBuilder\QueryBuilder; use Hosterra\FluxBuilder\QueryBuilder;
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface; use Hosterra\FluxBuilder\Builder\QueryBuilderInterface;
use Arendsen\FluxQueryBuilder\Expression\KeyValue; use Hosterra\FluxBuilder\Expression\KeyFilter;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter; use Hosterra\FluxBuilder\Functions\Filter;
use Arendsen\FluxQueryBuilder\Functions\Filter; use Hosterra\FluxBuilder\Functions\From;
use Arendsen\FluxQueryBuilder\Functions\From; use Hosterra\FluxBuilder\Functions\Measurement;
use Arendsen\FluxQueryBuilder\Functions\Measurement; use Hosterra\FluxBuilder\Functions\Range;
use Arendsen\FluxQueryBuilder\Functions\Range; use Hosterra\FluxBuilder\Functions\RawFunction;
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
trait Basics trait Basics {
{ public function from( array $from ): QueryBuilderInterface {
public function from(array $from): QueryBuilderInterface $this->setRequirements();
{
$this->setRequirements();
$this->addToQuery( $this->addToQuery(
new From($from) new From( $from )
); );
return $this; return $this;
} }
public function fromBucket(string $bucket): QueryBuilderInterface public function fromBucket( string $bucket ): QueryBuilderInterface {
{ return $this->from( [ 'bucket' => $bucket ] );
return $this->from(['bucket' => $bucket]); }
}
public function fromMeasurement(string $measurement): QueryBuilderInterface public function fromMeasurement( string $measurement ): QueryBuilderInterface {
{ $this->setRequirements();
$this->setRequirements();
$this->addToQuery( $this->addToQuery(
new Measurement($measurement) new Measurement( $measurement )
); );
return $this;
}
/** return $this;
* @deprecated }
*
* @param KeyValue $keyValue
* @return QueryBuilder
*/
public function addFilter(KeyValue $keyValue): QueryBuilderInterface
{
$this->setRequirements();
$this->addToQuery( public function addKeyFilter( KeyFilter $keyFilter ): QueryBuilderInterface {
new Filter($keyValue) $this->setRequirements();
);
return $this;
}
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilderInterface $this->addToQuery(
{ new Filter( $keyFilter )
$this->setRequirements(); );
$this->addToQuery( return $this;
new Filter($keyFilter) }
);
return $this;
}
public function addFieldFilter(array $fields): QueryBuilderInterface public function addFieldFilter( array $fields ): QueryBuilderInterface {
{ $this->setRequirements();
$this->setRequirements();
$this->addToQuery( $this->addToQuery(
new Filter($fields) new Filter( $fields )
); );
return $this;
}
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilderInterface return $this;
{ }
$this->setRequirements();
$this->addToQuery( public function addRange( DateTime $start, ?DateTime $stop = null ): QueryBuilderInterface {
new Range($start, $stop) $this->setRequirements();
);
return $this;
}
public function addRangeStart(DateTime $start): QueryBuilderInterface $this->addToQuery(
{ new Range( $start, $stop )
$this->addRange($start); );
return $this;
}
public function addRangeInBetween(DateTime $start, DateTime $stop): QueryBuilderInterface return $this;
{ }
$this->addRange($start, $stop);
return $this;
}
public function addRawFunction(string $input): QueryBuilderInterface public function addRangeStart( DateTime $start ): QueryBuilderInterface {
{ $this->addRange( $start );
$this->addToQuery(
new RawFunction($input)
);
return $this;
}
private function setRequirements() return $this;
{ }
$this->requiredFluxQueryParts = [
From::class, public function addRangeInBetween( DateTime $start, DateTime $stop ): QueryBuilderInterface {
Range::class, $this->addRange( $start, $stop );
Measurement::class,
]; return $this;
} }
public function addRawFunction( string $input ): QueryBuilderInterface {
$this->addToQuery(
new RawFunction( $input )
);
return $this;
}
private function setRequirements() {
$this->requiredFluxQueryParts = [
From::class,
Range::class,
Measurement::class,
];
}
} }

View file

@ -1,8 +1,7 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Builder; namespace Hosterra\FluxBuilder\Builder;
interface QueryBuilderInterface interface QueryBuilderInterface {
{ public function build(): string;
public function build(): string;
} }

View file

@ -1,167 +1,166 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Builder; namespace Hosterra\FluxBuilder\Builder;
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface; use Hosterra\FluxBuilder\Builder\QueryBuilderInterface;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow; use Hosterra\FluxBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Functions\Bottom; use Hosterra\FluxBuilder\Functions\Bottom;
use Arendsen\FluxQueryBuilder\Functions\Count; use Hosterra\FluxBuilder\Functions\Count;
use Arendsen\FluxQueryBuilder\Functions\Duplicate; use Hosterra\FluxBuilder\Functions\Duplicate;
use Arendsen\FluxQueryBuilder\Functions\First; use Hosterra\FluxBuilder\Functions\First;
use Arendsen\FluxQueryBuilder\Functions\Group; use Hosterra\FluxBuilder\Functions\Group;
use Arendsen\FluxQueryBuilder\Functions\Last; use Hosterra\FluxBuilder\Functions\Last;
use Arendsen\FluxQueryBuilder\Functions\Limit; use Hosterra\FluxBuilder\Functions\Limit;
use Arendsen\FluxQueryBuilder\Functions\Map; use Hosterra\FluxBuilder\Functions\Map;
use Arendsen\FluxQueryBuilder\Functions\Max; use Hosterra\FluxBuilder\Functions\Max;
use Arendsen\FluxQueryBuilder\Functions\Mean; use Hosterra\FluxBuilder\Functions\Mean;
use Arendsen\FluxQueryBuilder\Functions\Min; use Hosterra\FluxBuilder\Functions\Min;
use Arendsen\FluxQueryBuilder\Functions\Reduce; use Hosterra\FluxBuilder\Functions\Reduce;
use Arendsen\FluxQueryBuilder\Functions\Sort; use Hosterra\FluxBuilder\Functions\Sort;
use Arendsen\FluxQueryBuilder\Functions\Sum; use Hosterra\FluxBuilder\Functions\Sum;
use Arendsen\FluxQueryBuilder\Functions\Unique; use Hosterra\FluxBuilder\Functions\Unique;
use Arendsen\FluxQueryBuilder\Functions\Window; use Hosterra\FluxBuilder\Functions\Window;
trait Universe trait Universe {
{ public function addAggregateWindow( $every, $fn, array $options = [] ): QueryBuilderInterface {
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface $this->addToQuery(
{ new AggregateWindow( $every, $fn, $options )
$this->addToQuery( );
new AggregateWindow($every, $fn, $options)
);
return $this;
}
public function addBottom(int $n, array $columns = []): QueryBuilderInterface return $this;
{ }
$this->addToQuery(new Bottom($n, $columns));
return $this;
}
public function addCount(?string $column = null): QueryBuilderInterface public function addBottom( int $n, array $columns = [] ): QueryBuilderInterface {
{ $this->addToQuery( new Bottom( $n, $columns ) );
$this->addToQuery(
new Count($column)
);
return $this;
}
public function addDuplicate(string $column, string $as): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Duplicate($column, $as)
);
return $this;
}
public function addFirst(?string $column = null): QueryBuilderInterface public function addCount( ?string $column = null ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Count( $column )
new First($column) );
);
return $this;
}
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Group($columns, $mode)
);
return $this;
}
public function addLast(string $column = '_value'): QueryBuilderInterface public function addDuplicate( string $column, string $as ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Duplicate( $column, $as )
new Last($column) );
);
return $this;
}
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Limit($limit, $offset)
);
return $this;
}
public function addMap($query): QueryBuilderInterface public function addFirst( ?string $column = null ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new First( $column )
new Map($query) );
);
return $this;
}
public function addMax(?string $column = null): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Max($column)
);
return $this;
}
public function addMean(?string $column = ''): QueryBuilderInterface public function addGroup( array $columns, $mode = 'by' ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Group( $columns, $mode )
new Mean($column) );
);
return $this;
}
public function addMin(?string $column = null): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Min($column)
);
return $this;
}
public function addReduce(array $settings, array $identity): QueryBuilderInterface public function addLast( string $column = '_value' ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Last( $column )
new Reduce($settings, $identity) );
);
return $this;
}
public function addSort(array $columns = ['_value'], bool $desc = false): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Sort($columns, $desc)
);
return $this;
}
public function addSum(string $column): QueryBuilderInterface public function addLimit( int $limit, int $offset = 0 ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Limit( $limit, $offset )
new Sum($column) );
);
return $this;
}
public function addUnique(?string $column = null): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Unique($column)
);
return $this;
}
public function addUnWindow(): QueryBuilderInterface public function addMap( $query ): QueryBuilderInterface {
{ $this->addToQuery(
$this->addToQuery( new Map( $query )
new Window('inf') );
);
return $this;
}
public function addWindow($every, array $options = []): QueryBuilderInterface return $this;
{ }
$this->addToQuery(
new Window($every, $options) public function addMax( ?string $column = null ): QueryBuilderInterface {
); $this->addToQuery(
return $this; new Max( $column )
} );
return $this;
}
public function addMean( ?string $column = '' ): QueryBuilderInterface {
$this->addToQuery(
new Mean( $column )
);
return $this;
}
public function addMin( ?string $column = null ): QueryBuilderInterface {
$this->addToQuery(
new Min( $column )
);
return $this;
}
public function addReduce( array $settings, array $identity ): QueryBuilderInterface {
$this->addToQuery(
new Reduce( $settings, $identity )
);
return $this;
}
public function addSort( array $columns = [ '_value' ], bool $desc = false ): QueryBuilderInterface {
$this->addToQuery(
new Sort( $columns, $desc )
);
return $this;
}
public function addSum( string $column ): QueryBuilderInterface {
$this->addToQuery(
new Sum( $column )
);
return $this;
}
public function addUnique( ?string $column = null ): QueryBuilderInterface {
$this->addToQuery(
new Unique( $column )
);
return $this;
}
public function addUnWindow(): QueryBuilderInterface {
$this->addToQuery(
new Window( 'inf' )
);
return $this;
}
public function addWindow( $every, array $options = [] ): QueryBuilderInterface {
$this->addToQuery(
new Window( $every, $options )
);
return $this;
}
} }

View file

@ -1,9 +1,8 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Exception; namespace Hosterra\FluxBuilder\Exception;
use Exception; use Exception;
class ExpressionNotImplementedException extends Exception class ExpressionNotImplementedException extends Exception {
{
} }

View file

@ -1,9 +1,8 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Exception; namespace Hosterra\FluxBuilder\Exception;
use Exception; use Exception;
class FunctionInvalidInputException extends Exception class FunctionInvalidInputException extends Exception {
{
} }

View file

@ -1,9 +1,8 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Exception; namespace Hosterra\FluxBuilder\Exception;
use Exception; use Exception;
class FunctionNotImplementedException extends Exception class FunctionNotImplementedException extends Exception {
{
} }

View file

@ -1,13 +1,11 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Exception; namespace Hosterra\FluxBuilder\Exception;
use Exception; use Exception;
class FunctionRequiredSettingMissingException extends Exception class FunctionRequiredSettingMissingException extends Exception {
{ public function __construct( string $functionName, string $message ) {
public function __construct(string $functionName, string $message) parent::__construct( 'Function ' . $functionName . ' - ' . $message );
{ }
parent::__construct('Function ' . $functionName . ' - ' . $message);
}
} }

View file

@ -1,16 +1,14 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Expression; namespace Hosterra\FluxBuilder\Expression;
use Arendsen\FluxQueryBuilder\Exception\ExpressionNotImplementedException; use Hosterra\FluxBuilder\Exception\ExpressionNotImplementedException;
abstract class Base abstract class Base {
{ /**
/** * @throws ExpressionNotImplementedException
* @throws ExpressionNotImplementedException */
*/ public function __toString() {
public function __toString() throw new ExpressionNotImplementedException( '__toString', get_class( $this ) );
{ }
throw new ExpressionNotImplementedException('__toString', get_class($this));
}
} }

View file

@ -1,207 +1,194 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Expression; namespace Hosterra\FluxBuilder\Expression;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Exception; use Exception;
class KeyFilter extends Base class KeyFilter extends Base {
{ public const EQUAL_TO = '==';
public const EQUAL_TO = '=='; public const NOT_EQUAL_TO = '!=';
public const NOT_EQUAL_TO = '!='; public const GREATER_THAN = '>';
public const GREATER_THAN = '>'; public const GREATER_EQUAL_TO = '>=';
public const GREATER_EQUAL_TO = '>='; public const LESS_THAN = '<';
public const LESS_THAN = '<'; public const LESS_EQUAL_TO = '<=';
public const LESS_EQUAL_TO = '<='; public const EQUAL_TO_REGEX = '=~';
public const EQUAL_TO_REGEX = '=~'; public const NOT_EQUAL_TO_REGEX = '!~';
public const NOT_EQUAL_TO_REGEX = '!~';
public const COMPARISON_OPERATORS = [ public const COMPARISON_OPERATORS = [
self::EQUAL_TO, self::EQUAL_TO,
self::NOT_EQUAL_TO, self::NOT_EQUAL_TO,
self::GREATER_THAN, self::GREATER_THAN,
self::GREATER_EQUAL_TO, self::GREATER_EQUAL_TO,
self::LESS_THAN, self::LESS_THAN,
self::LESS_EQUAL_TO, self::LESS_EQUAL_TO,
self::EQUAL_TO_REGEX, self::EQUAL_TO_REGEX,
self::NOT_EQUAL_TO_REGEX, self::NOT_EQUAL_TO_REGEX,
]; ];
/** /**
* @var array $expressions * @var array $expressions
*/ */
private $expressions; private $expressions;
private function __construct(string $key, string $operator, $value) private function __construct( string $key, string $operator, $value ) {
{ $this->checkOperator( $operator );
$this->checkOperator($operator); $this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . new Type( $value );
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . new Type($value); }
}
public static function set(string $key, string $operator, $value): KeyFilter public static function set( string $key, string $operator, $value ): KeyFilter {
{ return new self( $key, $operator, $value );
return new self($key, $operator, $value); }
}
public static function setEqualTo(string $key, string $value): KeyFilter public static function setEqualTo( string $key, string $value ): KeyFilter {
{ return self::set( $key, self::EQUAL_TO, $value );
return self::set($key, self::EQUAL_TO, $value); }
}
public static function setNotEqualTo(string $key, string $value): KeyFilter public static function setNotEqualTo( string $key, string $value ): KeyFilter {
{ return self::set( $key, self::NOT_EQUAL_TO, $value );
return self::set($key, self::NOT_EQUAL_TO, $value); }
}
public static function setGreaterThan(string $key, int $value): KeyFilter public static function setGreaterThan( string $key, int $value ): KeyFilter {
{ return self::set( $key, self::GREATER_THAN, $value );
return self::set($key, self::GREATER_THAN, $value); }
}
public static function setGreaterOrEqualTo(string $key, int $value): KeyFilter public static function setGreaterOrEqualTo( string $key, int $value ): KeyFilter {
{ return self::set( $key, self::GREATER_EQUAL_TO, $value );
return self::set($key, self::GREATER_EQUAL_TO, $value); }
}
public static function setLessThan(string $key, int $value): KeyFilter public static function setLessThan( string $key, int $value ): KeyFilter {
{ return self::set( $key, self::LESS_THAN, $value );
return self::set($key, self::LESS_THAN, $value); }
}
public static function setLessOrEqualTo(string $key, int $value): KeyFilter public static function setLessOrEqualTo( string $key, int $value ): KeyFilter {
{ return self::set( $key, self::LESS_EQUAL_TO, $value );
return self::set($key, self::LESS_EQUAL_TO, $value); }
}
public static function setEqualToRegex(string $key, string $value): KeyFilter public static function setEqualToRegex( string $key, string $value ): KeyFilter {
{ return self::set( $key, self::EQUAL_TO_REGEX, $value );
return self::set($key, self::EQUAL_TO_REGEX, $value); }
}
public static function setNotEqualToRegex(string $key, string $value): KeyFilter public static function setNotEqualToRegex( string $key, string $value ): KeyFilter {
{ return self::set( $key, self::NOT_EQUAL_TO_REGEX, $value );
return self::set($key, self::NOT_EQUAL_TO_REGEX, $value); }
}
public function and(string $key, string $operator, $value): KeyFilter public function and( string $key, string $operator, $value ): KeyFilter {
{ $this->checkOperator( $operator );
$this->checkOperator($operator); $this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . new Type( $value );
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this;
}
public function andEqualTo(string $key, string $value): KeyFilter return $this;
{ }
$this->and($key, self::EQUAL_TO, $value);
return $this;
}
public function andNotEqualTo(string $key, string $value): KeyFilter public function andEqualTo( string $key, string $value ): KeyFilter {
{ $this->and( $key, self::EQUAL_TO, $value );
$this->and($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function andGreaterThan(string $key, int $value): KeyFilter return $this;
{ }
$this->and($key, self::GREATER_THAN, $value);
return $this;
}
public function andGreaterOrEqualTo(string $key, int $value): KeyFilter public function andNotEqualTo( string $key, string $value ): KeyFilter {
{ $this->and( $key, self::NOT_EQUAL_TO, $value );
$this->and($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function andLessThan(string $key, int $value): KeyFilter return $this;
{ }
$this->and($key, self::LESS_THAN, $value);
return $this;
}
public function andLessOrEqualTo(string $key, int $value): KeyFilter public function andGreaterThan( string $key, int $value ): KeyFilter {
{ $this->and( $key, self::GREATER_THAN, $value );
$this->and($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function andEqualToRegex(string $key, string $value): KeyFilter return $this;
{ }
$this->and($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function andNotEqualToRegex(string $key, string $value): KeyFilter public function andGreaterOrEqualTo( string $key, int $value ): KeyFilter {
{ $this->and( $key, self::GREATER_EQUAL_TO, $value );
$this->and($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}
public function or(string $key, string $operator, $value): KeyFilter return $this;
{ }
$this->checkOperator($operator);
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this;
}
public function orEqualTo(string $key, string $value): KeyFilter public function andLessThan( string $key, int $value ): KeyFilter {
{ $this->and( $key, self::LESS_THAN, $value );
$this->or($key, self::EQUAL_TO, $value);
return $this;
}
public function orNotEqualTo(string $key, string $value): KeyFilter return $this;
{ }
$this->or($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function orGreaterThan(string $key, int $value): KeyFilter public function andLessOrEqualTo( string $key, int $value ): KeyFilter {
{ $this->and( $key, self::LESS_EQUAL_TO, $value );
$this->or($key, self::GREATER_THAN, $value);
return $this;
}
public function orGreaterOrEqualTo(string $key, int $value): KeyFilter return $this;
{ }
$this->or($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function orLessThan(string $key, int $value): KeyFilter public function andEqualToRegex( string $key, string $value ): KeyFilter {
{ $this->and( $key, self::EQUAL_TO_REGEX, $value );
$this->or($key, self::LESS_THAN, $value);
return $this;
}
public function orLessOrEqualTo(string $key, int $value): KeyFilter return $this;
{ }
$this->or($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function orEqualToRegex(string $key, string $value): KeyFilter public function andNotEqualToRegex( string $key, string $value ): KeyFilter {
{ $this->and( $key, self::NOT_EQUAL_TO_REGEX, $value );
$this->or($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function orNotEqualToRegex(string $key, string $value): KeyFilter return $this;
{ }
$this->or($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}
public function __toString() public function or( string $key, string $operator, $value ): KeyFilter {
{ $this->checkOperator( $operator );
return implode(' ', $this->expressions); $this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . new Type( $value );
}
protected function checkOperator(string $operator) return $this;
{ }
if (!in_array($operator, self::COMPARISON_OPERATORS)) {
throw new Exception('Operator "' . $operator . '" is not supported!'); public function orEqualTo( string $key, string $value ): KeyFilter {
} $this->or( $key, self::EQUAL_TO, $value );
}
return $this;
}
public function orNotEqualTo( string $key, string $value ): KeyFilter {
$this->or( $key, self::NOT_EQUAL_TO, $value );
return $this;
}
public function orGreaterThan( string $key, int $value ): KeyFilter {
$this->or( $key, self::GREATER_THAN, $value );
return $this;
}
public function orGreaterOrEqualTo( string $key, int $value ): KeyFilter {
$this->or( $key, self::GREATER_EQUAL_TO, $value );
return $this;
}
public function orLessThan( string $key, int $value ): KeyFilter {
$this->or( $key, self::LESS_THAN, $value );
return $this;
}
public function orLessOrEqualTo( string $key, int $value ): KeyFilter {
$this->or( $key, self::LESS_EQUAL_TO, $value );
return $this;
}
public function orEqualToRegex( string $key, string $value ): KeyFilter {
$this->or( $key, self::EQUAL_TO_REGEX, $value );
return $this;
}
public function orNotEqualToRegex( string $key, string $value ): KeyFilter {
$this->or( $key, self::NOT_EQUAL_TO_REGEX, $value );
return $this;
}
public function __toString() {
return implode( ' ', $this->expressions );
}
protected function checkOperator( string $operator ) {
if ( ! in_array( $operator, self::COMPARISON_OPERATORS ) ) {
throw new Exception( 'Operator "' . $operator . '" is not supported!' );
}
}
} }

View file

@ -1,210 +0,0 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression;
use Arendsen\FluxQueryBuilder\Type;
use Exception;
/**
* @deprecated
*/
class KeyValue extends Base
{
public const EQUAL_TO = '==';
public const NOT_EQUAL_TO = '!=';
public const GREATER_THAN = '>';
public const GREATER_EQUAL_TO = '>=';
public const LESS_THAN = '<';
public const LESS_EQUAL_TO = '<=';
public const EQUAL_TO_REGEX = '=~';
public const NOT_EQUAL_TO_REGEX = '!~';
public const COMPARISON_OPERATORS = [
self::EQUAL_TO,
self::NOT_EQUAL_TO,
self::GREATER_THAN,
self::GREATER_EQUAL_TO,
self::LESS_THAN,
self::LESS_EQUAL_TO,
self::EQUAL_TO_REGEX,
self::NOT_EQUAL_TO_REGEX,
];
/**
* @var array $expressions
*/
private $expressions;
private function __construct(string $key, string $operator, $value)
{
$this->checkOperator($operator);
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . new Type($value);
}
public static function set(string $key, string $operator, $value): KeyValue
{
return new self($key, $operator, $value);
}
public static function setEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::EQUAL_TO, $value);
}
public static function setNotEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::NOT_EQUAL_TO, $value);
}
public static function setGreaterThan(string $key, int $value): KeyValue
{
return self::set($key, self::GREATER_THAN, $value);
}
public static function setGreaterOrEqualTo(string $key, int $value): KeyValue
{
return self::set($key, self::GREATER_EQUAL_TO, $value);
}
public static function setLessThan(string $key, int $value): KeyValue
{
return self::set($key, self::LESS_THAN, $value);
}
public static function setLessOrEqualTo(string $key, int $value): KeyValue
{
return self::set($key, self::LESS_EQUAL_TO, $value);
}
public static function setEqualToRegex(string $key, string $value): KeyValue
{
return self::set($key, self::EQUAL_TO_REGEX, $value);
}
public static function setNotEqualToRegex(string $key, string $value): KeyValue
{
return self::set($key, self::NOT_EQUAL_TO_REGEX, $value);
}
public function and(string $key, string $operator, $value): KeyValue
{
$this->checkOperator($operator);
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this;
}
public function andEqualTo(string $key, string $value): KeyValue
{
$this->and($key, self::EQUAL_TO, $value);
return $this;
}
public function andNotEqualTo(string $key, string $value): KeyValue
{
$this->and($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function andGreaterThan(string $key, int $value): KeyValue
{
$this->and($key, self::GREATER_THAN, $value);
return $this;
}
public function andGreaterOrEqualTo(string $key, int $value): KeyValue
{
$this->and($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function andLessThan(string $key, int $value): KeyValue
{
$this->and($key, self::LESS_THAN, $value);
return $this;
}
public function andLessOrEqualTo(string $key, int $value): KeyValue
{
$this->and($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function andEqualToRegex(string $key, string $value): KeyValue
{
$this->and($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function andNotEqualToRegex(string $key, string $value): KeyValue
{
$this->and($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}
public function or(string $key, string $operator, $value): KeyValue
{
$this->checkOperator($operator);
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this;
}
public function orEqualTo(string $key, string $value): KeyValue
{
$this->or($key, self::EQUAL_TO, $value);
return $this;
}
public function orNotEqualTo(string $key, string $value): KeyValue
{
$this->or($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function orGreaterThan(string $key, int $value): KeyValue
{
$this->or($key, self::GREATER_THAN, $value);
return $this;
}
public function orGreaterOrEqualTo(string $key, int $value): KeyValue
{
$this->or($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function orLessThan(string $key, int $value): KeyValue
{
$this->or($key, self::LESS_THAN, $value);
return $this;
}
public function orLessOrEqualTo(string $key, int $value): KeyValue
{
$this->or($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function orEqualToRegex(string $key, string $value): KeyValue
{
$this->or($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function orNotEqualToRegex(string $key, string $value): KeyValue
{
$this->or($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}
public function __toString()
{
return implode(' ', $this->expressions);
}
protected function checkOperator(string $operator)
{
if (!in_array($operator, self::COMPARISON_OPERATORS)) {
throw new Exception('Operator "' . $operator . '" is not supported!');
}
}
}

View file

@ -1,29 +1,27 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Expression; namespace Hosterra\FluxBuilder\Expression;
use Arendsen\FluxQueryBuilder\Type\FieldRecordType; use Hosterra\FluxBuilder\Type\FieldRecordType;
class Map extends Base class Map extends Base {
{ private static $string;
private static $string;
public static function with(string $name, string $content): Map public static function with( string $name, string $content ): Map {
{ $object = new self();
$object = new self(); $object::$string = 'r with ' . $name . ': ' . $content;
$object::$string = 'r with ' . $name . ': ' . $content;
return $object;
}
public static function columns(array $columns) return $object;
{ }
$object = new self();
$object::$string = new FieldRecordType($columns);
return $object;
}
public function __toString() public static function columns( array $columns ) {
{ $object = new self();
return self::$string; $object::$string = new FieldRecordType( $columns );
}
return $object;
}
public function __toString() {
return self::$string;
}
} }

View file

@ -1,37 +1,33 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Expression; namespace Hosterra\FluxBuilder\Expression;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Selection extends Base class Selection extends Base {
{ private $string;
private $string;
private function __construct(string $string) private function __construct( string $string ) {
{ $this->string = $string;
$this->string = $string; }
}
public static function if(string $value): Selection public static function if( string $value ): Selection {
{ return new self( 'if ' . $value );
return new self('if ' . $value); }
}
public function then($then): Selection public function then( $then ): Selection {
{ $this->string .= ' then ' . new Type( $then );
$this->string .= ' then ' . new Type($then);
return $this;
}
public function else($else): Selection return $this;
{ }
$this->string .= ' else ' . new Type($else);
return $this;
}
public function __toString() public function else( $else ): Selection {
{ $this->string .= ' else ' . new Type( $else );
return $this->string;
} return $this;
}
public function __toString() {
return $this->string;
}
} }

View file

@ -1,51 +1,49 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
use Arendsen\FluxQueryBuilder\Type\CustomType; use Hosterra\FluxBuilder\Type\CustomType;
use Arendsen\FluxQueryBuilder\Type\DurationType; use Hosterra\FluxBuilder\Type\DurationType;
use Arendsen\FluxQueryBuilder\Type\FnType; use Hosterra\FluxBuilder\Type\FnType;
class AggregateWindow extends Base class AggregateWindow extends Base {
{ /**
/** * @var string $every
* @var string $every */
*/ private $every;
private $every;
/** /**
* @var string $fn * @var string $fn
*/ */
private $fn; private $fn;
/** /**
* @var array $options * @var array $options
*/ */
private $options; private $options;
public function __construct($every, $fn, array $options = []) public function __construct( $every, $fn, array $options = [] ) {
{ $this->every = $every;
$this->every = $every; $this->fn = $fn;
$this->fn = $fn; $this->options = $options;
$this->options = $options; }
}
public function __toString() public function __toString() {
{ $input = new ArrayType( array_filter( [
$input = new ArrayType(array_filter([ 'every' => new DurationType( $this->every ),
'every' => new DurationType($this->every), 'period' => isset( $this->options['period'] ) ? new DurationType( $this->options['period'] ) : null,
'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null, 'offset' => isset( $this->options['offset'] ) ? new DurationType( $this->options['offset'] ) : null,
'offset' => isset($this->options['offset']) ? new DurationType($this->options['offset']) : null, 'fn' => new CustomType( $this->fn ),
'fn' => new CustomType($this->fn), 'location' => isset( $this->options['location'] ) ? new Type( $this->options['location'] ) : null,
'location' => isset($this->options['location']) ? new Type($this->options['location']) : null, 'column' => isset( $this->options['column'] ) ? new Type( $this->options['column'] ) : null,
'column' => isset($this->options['column']) ? new Type($this->options['column']) : null, 'timeSrc' => isset( $this->options['timeSrc'] ) ? new Type( $this->options['timeSrc'] ) : null,
'timeSrc' => isset($this->options['timeSrc']) ? new Type($this->options['timeSrc']) : null, 'timeDst' => isset( $this->options['timeDst'] ) ? new Type( $this->options['timeDst'] ) : null,
'timeDst' => isset($this->options['timeDst']) ? new Type($this->options['timeDst']) : null, 'createEmpty' => isset( $this->options['createEmpty'] ) && ! $this->options['createEmpty'] ?
'createEmpty' => isset($this->options['createEmpty']) && !$this->options['createEmpty'] ? new Type( $this->options['createEmpty'] ) : null,
new Type($this->options['createEmpty']) : null, ] ) );
]));
return '|> aggregateWindow(' . $input . ') '; return '|> aggregateWindow(' . $input . ') ';
} }
} }

View file

@ -1,16 +1,14 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Exception\FunctionNotImplementedException; use Hosterra\FluxBuilder\Exception\FunctionNotImplementedException;
abstract class Base abstract class Base {
{ /**
/** * @throws FunctionNotImplementedException
* @throws FunctionNotImplementedException */
*/ public function __toString() {
public function __toString() throw new FunctionNotImplementedException( '__toString', get_class( $this ) );
{ }
throw new FunctionNotImplementedException('__toString', get_class($this));
}
} }

View file

@ -1,33 +1,31 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
class Bottom extends Base class Bottom extends Base {
{ /**
/** * @var int $n
* @var int $n */
*/ private $n;
private $n;
/** /**
* @var array $columns * @var array $columns
*/ */
private $columns; private $columns;
public function __construct($n, array $columns = []) public function __construct( $n, array $columns = [] ) {
{ $this->n = $n;
$this->n = $n; $this->columns = $columns;
$this->columns = $columns; }
}
public function __toString() public function __toString() {
{ $input = new ArrayType( array_filter( [
$input = new ArrayType(array_filter([ 'n' => $this->n,
'n' => $this->n, 'columns' => $this->columns ?: null,
'columns' => $this->columns ?: null, ] ) );
]));
return '|> bottom(' . $input . ') '; return '|> bottom(' . $input . ') ';
} }
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Count extends Base class Count extends Base {
{ /**
/** * @var string|null $column
* @var string|null $column */
*/ private $column;
private $column;
public function __construct(?string $column = null) public function __construct( ?string $column = null ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $params = new Type( array_filter( [
$params = new Type(array_filter([ 'column' => $this->column
'column' => $this->column ] ) );
]));
return '|> count(' . $params . ') '; return '|> count(' . $params . ') ';
} }
} }

View file

@ -1,35 +1,32 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
class Duplicate extends Base class Duplicate extends Base {
{ /**
/** * @var string $column
* @var string $column */
*/ private $column;
private $column;
/** /**
* @var string $as * @var string $as
*/ */
private $as; private $as;
public function __construct(string $column, string $as) public function __construct( string $column, string $as ) {
{ $this->column = $column;
$this->column = $column; $this->as = $as;
$this->as = $as; }
}
public function __toString() public function __toString() {
{ $input = new ArrayType( [
$input = new ArrayType([ 'column' => $this->column,
'column' => $this->column, 'as' => $this->as
'as' => $this->as ] );
]);
return '|> duplicate(' . $input . ') '; return '|> duplicate(' . $input . ') ';
} }
} }

View file

@ -1,51 +1,47 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Exception\FunctionInvalidInputException; use Hosterra\FluxBuilder\Exception\FunctionInvalidInputException;
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
class Filter extends Base use Hosterra\FluxBuilder\Expression\KeyFilter;
{
/**
* @var mixed $filter
*/
private $filter;
public function __construct($filter) class Filter extends Base {
{ /**
$this->filter = $filter; * @var mixed $filter
} */
private $filter;
public function __toString() public function __construct( $filter ) {
{ $this->filter = $filter;
return '|> filter(fn: (r) => ' . $this->format() . ') '; }
}
/** public function __toString() {
* @throws FunctionInvalidInputException return '|> filter(fn: (r) => ' . $this->format() . ') ';
*/ }
protected function format(): string
{
if ($this->filter instanceof KeyValue || $this->filter instanceof KeyFilter) {
return $this->filter->__toString();
} elseif (is_array($this->filter)) {
$filterCounter = 0;
$filterString = '';
foreach ($this->filter as $filter) {
if ($filterCounter > 0) {
$filterString .= ' or ';
}
$filterString .= 'r._field == "' . $filter . '"'; /**
* @throws FunctionInvalidInputException
*/
protected function format(): string {
if ( $this->filter instanceof KeyValue || $this->filter instanceof KeyFilter ) {
return $this->filter->__toString();
} elseif ( is_array( $this->filter ) ) {
$filterCounter = 0;
$filterString = '';
foreach ( $this->filter as $filter ) {
if ( $filterCounter > 0 ) {
$filterString .= ' or ';
}
$filterCounter++; $filterString .= 'r._field == "' . $filter . '"';
}
return $filterString; $filterCounter ++;
} }
throw new FunctionInvalidInputException('Filter input is incorrect.'); return $filterString;
} }
throw new FunctionInvalidInputException( 'Filter input is incorrect.' );
}
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class First extends Base class First extends Base {
{ /**
/** * @var string|null $column
* @var string|null $column */
*/ private $column;
private $column;
public function __construct(?string $column = null) public function __construct( ?string $column = null ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $params = new Type( array_filter( [
$params = new Type(array_filter([ 'column' => $this->column
'column' => $this->column ] ) );
]));
return '|> first(' . $params . ') '; return '|> first(' . $params . ') ';
} }
} }

View file

@ -1,23 +1,20 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class From extends Base class From extends Base {
{ /**
/** * @var array $settings
* @var array $settings */
*/ private $settings;
private $settings;
public function __construct(array $settings) public function __construct( array $settings ) {
{ $this->settings = $settings;
$this->settings = $settings; }
}
public function __toString() public function __toString() {
{ return 'from(' . new Type( $this->settings ) . ') ';
return 'from(' . new Type($this->settings) . ') '; }
}
} }

View file

@ -1,34 +1,31 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Group extends Base class Group extends Base {
{ /**
/** * @var array $columns
* @var array $columns */
*/ private $columns;
private $columns;
/** /**
* @var string $mode * @var string $mode
*/ */
private $mode; private $mode;
public function __construct(array $columns, string $mode = 'by') public function __construct( array $columns, string $mode = 'by' ) {
{ $this->columns = $columns;
$this->columns = $columns; $this->mode = $mode;
$this->mode = $mode; }
}
public function __toString() public function __toString() {
{ $array = new Type( [
$array = new Type([ 'columns' => $this->columns,
'columns' => $this->columns, 'mode' => $this->mode,
'mode' => $this->mode, ] );
]);
return '|> group(' . $array . ') '; return '|> group(' . $array . ') ';
} }
} }

View file

@ -1,21 +1,18 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
class Last extends Base class Last extends Base {
{ /**
/** * @var int $column
* @var int $column */
*/ private $column;
private $column;
public function __construct(string $column = '_value') public function __construct( string $column = '_value' ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ return '|> last(column: "' . (string) $this->column . '") ';
return '|> last(column: "' . (string)$this->column . '") '; }
}
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
class Limit extends Base class Limit extends Base {
{ /**
/** * @var int $limit
* @var int $limit */
*/ private $limit;
private $limit;
/** /**
* @var int $offset * @var int $offset
*/ */
private $offset; private $offset;
public function __construct(int $limit, int $offset = 0) public function __construct( int $limit, int $offset = 0 ) {
{ $this->limit = $limit;
$this->limit = $limit; $this->offset = $offset;
$this->offset = $offset; }
}
public function __toString() public function __toString() {
{ return '|> limit(n: ' . (string) $this->limit . ', offset: ' . (string) $this->offset . ') ';
return '|> limit(n: ' . (string)$this->limit . ', offset: ' . (string)$this->offset . ') '; }
}
} }

View file

@ -1,23 +1,20 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Expression\Map as MapExpression; use Hosterra\FluxBuilder\Expression\Map as MapExpression;
class Map extends Base class Map extends Base {
{ /**
/** * @var mixed $query
* @var mixed $query */
*/ private $query;
private $query;
public function __construct($query) public function __construct( $query ) {
{ $this->query = $query;
$this->query = $query; }
}
public function __toString() public function __toString() {
{ return '|> map(fn: (r) => ({ ' . $this->query . ' })) ';
return '|> map(fn: (r) => ({ ' . $this->query . ' })) '; }
}
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Max extends Base class Max extends Base {
{ /**
/** * @var string|null $column
* @var string|null $column */
*/ private $column;
private $column;
public function __construct(?string $column = null) public function __construct( ?string $column = null ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $params = new Type( array_filter( [
$params = new Type(array_filter([ 'column' => $this->column
'column' => $this->column ] ) );
]));
return '|> max(' . $params . ') '; return '|> max(' . $params . ') ';
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
class Mean extends Base class Mean extends Base {
{ /**
/** * @var string $column
* @var string $column */
*/ private $column;
private $column;
public function __construct(string $column = '_value') public function __construct( string $column = '_value' ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $input = new ArrayType( array_filter( [
$input = new ArrayType(array_filter([ 'column' => ! empty( $this->column ) && $this->column !== '_value' ?
'column' => !empty($this->column) && $this->column !== '_value' ? new Type( $this->column ) : null
new Type($this->column) : null ] ) );
]));
return '|> mean(' . $input . ') '; return '|> mean(' . $input . ') ';
} }
} }

View file

@ -1,21 +1,18 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
class Measurement extends Base class Measurement extends Base {
{ /**
/** * @var string $measurement
* @var string $measurement */
*/ private $measurement;
private $measurement;
public function __construct(string $measurement) public function __construct( string $measurement ) {
{ $this->measurement = $measurement;
$this->measurement = $measurement; }
}
public function __toString() public function __toString() {
{ return '|> filter(fn: (r) => r._measurement == "' . (string) $this->measurement . '") ';
return '|> filter(fn: (r) => r._measurement == "' . (string)$this->measurement . '") '; }
}
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Min extends Base class Min extends Base {
{ /**
/** * @var string|null $column
* @var string|null $column */
*/ private $column;
private $column;
public function __construct(?string $column = null) public function __construct( ?string $column = null ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $params = new Type( array_filter( [
$params = new Type(array_filter([ 'column' => $this->column
'column' => $this->column ] ) );
]));
return '|> min(' . $params . ') '; return '|> min(' . $params . ') ';
} }
} }

View file

@ -1,40 +1,37 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Exception\FunctionRequiredSettingMissingException; use Hosterra\FluxBuilder\Exception\FunctionRequiredSettingMissingException;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use DateTime; use DateTime;
class Range extends Base class Range extends Base {
{ /**
/** * @var mixed $start
* @var mixed $start */
*/ private $start;
private $start;
/** /**
* @var mixed $stop * @var mixed $stop
*/ */
private $stop; private $stop;
public function __construct(DateTime $start, ?DateTime $stop = null) public function __construct( DateTime $start, ?DateTime $stop = null ) {
{ $this->start = new Type( $start );
$this->start = new Type($start); $this->stop = $stop ? new Type( $stop ) : null;
$this->stop = $stop ? new Type($stop) : null; }
}
public function __toString() public function __toString() {
{ if ( ! $this->start ) {
if (!$this->start) { throw new FunctionRequiredSettingMissingException( 'Range', 'Start setting is required!' );
throw new FunctionRequiredSettingMissingException('Range', 'Start setting is required!'); }
}
$settingsString = 'start: ' . $this->start; $settingsString = 'start: ' . $this->start;
if ($this->stop) { if ( $this->stop ) {
$settingsString .= ', stop: ' . $this->stop; $settingsString .= ', stop: ' . $this->stop;
} }
return '|> range(' . $settingsString . ') '; return '|> range(' . $settingsString . ') ';
} }
} }

View file

@ -1,21 +1,18 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
class RawFunction extends Base class RawFunction extends Base {
{ /**
/** * @var string $input
* @var string $input */
*/ private $input;
private $input;
public function __construct(string $input) public function __construct( string $input ) {
{ $this->input = $input;
$this->input = $input; }
}
public function __toString() public function __toString() {
{ return $this->input . ' ';
return $this->input . ' '; }
}
} }

View file

@ -1,30 +1,27 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type\RecordType; use Hosterra\FluxBuilder\Type\RecordType;
class Reduce extends Base class Reduce extends Base {
{ /**
/** * @var array $settings
* @var array $settings */
*/ private $settings;
private $settings;
/** /**
* @var array $identity * @var array $identity
*/ */
private $identity; private $identity;
public function __construct(array $settings, array $identity) public function __construct( array $settings, array $identity ) {
{ $this->settings = $settings;
$this->settings = $settings; $this->identity = $identity;
$this->identity = $identity; }
}
public function __toString() public function __toString() {
{ return '|> reduce(fn: (r, accumulator) => (' . new RecordType( $this->settings ) . '), ' .
return '|> reduce(fn: (r, accumulator) => (' . new RecordType($this->settings) . '), ' . 'identity: ' . new RecordType( $this->identity ) . ') ';
'identity: ' . new RecordType($this->identity) . ') '; }
}
} }

View file

@ -1,30 +1,27 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Sort extends Base class Sort extends Base {
{ /**
/** * @var array $columns
* @var array $columns */
*/ private $columns;
private $columns;
/** /**
* @var bool $desc * @var bool $desc
*/ */
private $desc; private $desc;
public function __construct(array $columns = ['_value'], bool $desc = false) public function __construct( array $columns = [ '_value' ], bool $desc = false ) {
{ $this->columns = $columns;
$this->columns = $columns; $this->desc = $desc;
$this->desc = $desc; }
}
public function __toString() public function __toString() {
{ return '|> sort(columns: [' . new Type( $this->columns ) .
return '|> sort(columns: [' . new Type($this->columns) . '], desc: ' . new Type( $this->desc ) . ') ';
'], desc: ' . new Type($this->desc) . ') '; }
}
} }

View file

@ -1,21 +1,18 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
class Sum extends Base class Sum extends Base {
{ /**
/** * @var string $column
* @var string $column */
*/ private $column;
private $column;
public function __construct(string $column) public function __construct( string $column ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ return '|> sum(column: "' . (string) $this->column . '") ';
return '|> sum(column: "' . (string)$this->column . '") '; }
}
} }

View file

@ -1,27 +1,24 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class Unique extends Base class Unique extends Base {
{ /**
/** * @var string|null $column
* @var string|null $column */
*/ private $column;
private $column;
public function __construct(?string $column = null) public function __construct( ?string $column = null ) {
{ $this->column = $column;
$this->column = $column; }
}
public function __toString() public function __toString() {
{ $params = new Type( array_filter( [
$params = new Type(array_filter([ 'column' => $this->column
'column' => $this->column ] ) );
]));
return '|> unique(' . $params . ') '; return '|> unique(' . $params . ') ';
} }
} }

View file

@ -1,42 +1,40 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Functions; namespace Hosterra\FluxBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
use Arendsen\FluxQueryBuilder\Type\DurationType; use Hosterra\FluxBuilder\Type\DurationType;
class Window extends Base class Window extends Base {
{ /**
/** * @var string $every
* @var string $every */
*/ private $every;
private $every;
/** /**
* @var array $options * @var array $options
*/ */
private $options; private $options;
public function __construct($every, array $options = []) public function __construct( $every, array $options = [] ) {
{ $this->every = $every;
$this->every = $every; $this->options = $options;
$this->options = $options; }
}
public function __toString() public function __toString() {
{ $input = new ArrayType( array_filter( [
$input = new ArrayType(array_filter([ 'every' => new DurationType( $this->every ),
'every' => new DurationType($this->every), 'period' => isset( $this->options['period'] ) ? new DurationType( $this->options['period'] ) : null,
'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null, 'offset' => isset( $this->options['offset'] ) ? new DurationType( $this->options['offset'] ) : null,
'offset' => isset($this->options['offset']) ? new DurationType($this->options['offset']) : null, 'location' => isset( $this->options['location'] ) ? new Type( $this->options['location'] ) : null,
'location' => isset($this->options['location']) ? new Type($this->options['location']) : null, 'timeColumn' => isset( $this->options['timeColumn'] ) ? new Type( $this->options['timeColumn'] ) : null,
'timeColumn' => isset($this->options['timeColumn']) ? new Type($this->options['timeColumn']) : null, 'startColumn' => isset( $this->options['startColumn'] ) ? new Type( $this->options['startColumn'] ) : null,
'startColumn' => isset($this->options['startColumn']) ? new Type($this->options['startColumn']) : null, 'stopColumn' => isset( $this->options['stopColumn'] ) ? new Type( $this->options['stopColumn'] ) : null,
'stopColumn' => isset($this->options['stopColumn']) ? new Type($this->options['stopColumn']) : null, 'createEmpty' => isset( $this->options['createEmpty'] ) && $this->options['createEmpty'] ?
'createEmpty' => isset($this->options['createEmpty']) && $this->options['createEmpty'] ? new Type( $this->options['createEmpty'] ) : null,
new Type($this->options['createEmpty']) : null, ] ) );
]));
return '|> window(' . $input . ') '; return '|> window(' . $input . ') ';
} }
} }

View file

@ -1,64 +1,62 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder; namespace Hosterra\FluxBuilder;
use Exception; use Exception;
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface; use Hosterra\FluxBuilder\Builder\QueryBuilderInterface;
use Arendsen\FluxQueryBuilder\Builder\Basics; use Hosterra\FluxBuilder\Builder\Basics;
use Arendsen\FluxQueryBuilder\Builder\Universe; use Hosterra\FluxBuilder\Builder\Universe;
class QueryBuilder implements QueryBuilderInterface class QueryBuilder implements QueryBuilderInterface {
{
use Basics;
use Universe;
/** const VERSION = '2.0.0';
* @var array $requiredFluxQueryParts use Basics;
*/ use Universe;
private $requiredFluxQueryParts = [];
/** /**
* @var array $fluxQueryParts * @var array $requiredFluxQueryParts
*/ */
private $fluxQueryParts = []; private $requiredFluxQueryParts = [];
/** /**
* @var array $requiredData * @var array $fluxQueryParts
*/ */
private $requiredData = []; private $fluxQueryParts = [];
protected function addToQuery($query) /**
{ * @var array $requiredData
$this->fluxQueryParts[] = $query; */
private $requiredData = [];
foreach ($this->requiredFluxQueryParts as $input) { protected function addToQuery( $query ) {
if ($query instanceof $input) { $this->fluxQueryParts[] = $query;
$this->requiredData[] = $query;
}
}
}
public function build(): string foreach ( $this->requiredFluxQueryParts as $input ) {
{ if ( $query instanceof $input ) {
$this->checkRequired(); $this->requiredData[] = $query;
}
}
}
$query = ''; public function build(): string {
$this->checkRequired();
foreach ($this->fluxQueryParts as $part) { $query = '';
$query .= $part;
}
return $query; foreach ( $this->fluxQueryParts as $part ) {
} $query .= $part;
}
protected function checkRequired() return $query;
{ }
foreach ($this->requiredFluxQueryParts as $key => $input) {
if (isset($this->requiredData[$key]) && !$this->requiredData[$key] instanceof $input) { protected function checkRequired() {
throw new Exception('You need to put the "' . $input . '" part of the query in the correct order!'); foreach ( $this->requiredFluxQueryParts as $key => $input ) {
} elseif (!isset($this->requiredData[$key])) { if ( isset( $this->requiredData[ $key ] ) && ! $this->requiredData[ $key ] instanceof $input ) {
throw new Exception('You need to define the "' . $input . '" part of the query!'); throw new Exception( 'You need to put the "' . $input . '" part of the query in the correct order!' );
} } elseif ( ! isset( $this->requiredData[ $key ] ) ) {
} throw new Exception( 'You need to define the "' . $input . '" part of the query!' );
} }
}
}
} }

View file

@ -1,26 +1,22 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder; namespace Hosterra\FluxBuilder;
class Settings class Settings {
{ /**
/** * @var array $settings
* @var array $settings */
*/ protected $settings;
protected $settings;
public function __construct(array $settings = []) public function __construct( array $settings = [] ) {
{ $this->settings = $settings;
$this->settings = $settings; }
}
public static function set(array $settings = []) public static function set( array $settings = [] ) {
{ return new self( $settings );
return new self($settings); }
}
public function get(string $key) public function get( string $key ) {
{ return isset( $this->settings[ $key ] ) ? $this->settings[ $key ] : null;
return isset($this->settings[$key]) ? $this->settings[$key] : null; }
}
} }

View file

@ -1,43 +1,41 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder; namespace Hosterra\FluxBuilder;
use Arendsen\FluxQueryBuilder\Type\TypeInterface; use Hosterra\FluxBuilder\Type\TypeInterface;
use Arendsen\FluxQueryBuilder\Type\ArrayType; use Hosterra\FluxBuilder\Type\ArrayType;
use Arendsen\FluxQueryBuilder\Type\BooleanType; use Hosterra\FluxBuilder\Type\BooleanType;
use Arendsen\FluxQueryBuilder\Type\TimeType; use Hosterra\FluxBuilder\Type\TimeType;
use DateTime; use DateTime;
class Type class Type {
{ /**
/** * @var mixed $value
* @var mixed $value */
*/ protected $value;
protected $value;
public function __construct($value) public function __construct( $value ) {
{ $this->value = $value;
$this->value = $value; }
}
public function __toString(): string public function __toString(): string {
{ switch ( gettype( $this->value ) ) {
switch (gettype($this->value)) { case 'object':
case 'object': if ( $this->value instanceof DateTime ) {
if ($this->value instanceof DateTime) { return new TimeType( $this->value );
return new TimeType($this->value); }
}
return $this->value->__toString(); return $this->value->__toString();
case 'string': case 'string':
return '"' . $this->value . '"'; return '"' . $this->value . '"';
case 'boolean': case 'boolean':
return new BooleanType($this->value); return new BooleanType( $this->value );
case 'array': case 'array':
return new ArrayType($this->value); return new ArrayType( $this->value );
case 'NULL': case 'NULL':
return 'null'; return 'null';
default: default:
return (string)$this->value; return (string) $this->value;
} }
} }
} }

View file

@ -1,47 +1,41 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Settings; use Hosterra\FluxBuilder\Settings;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class ArrayType implements TypeInterface class ArrayType implements TypeInterface {
{ /**
/** * @var array $value
* @var array $value */
*/ protected $value;
protected $value;
public function __construct(array $value) public function __construct( array $value ) {
{ $this->value = $value;
$this->value = $value; }
}
public function __toString(): string public function __toString(): string {
{ array_walk( $this->value, function ( &$value, $key ) {
array_walk($this->value, function (&$value, $key) { if ( $this->isAssociativeArray( $key ) ) {
if ($this->isAssociativeArray($key)) { $value = $key . ': ' . $this->getPrefix( $value ) . new Type( $value ) . $this->getSuffix( $value );
$value = $key . ': ' . $this->getPrefix($value) . new Type($value) . $this->getSuffix($value); } else {
} else { $value = $this->getPrefix( $value ) . new Type( $value ) . $this->getSuffix( $value );
$value = $this->getPrefix($value) . new Type($value) . $this->getSuffix($value); }
} } );
});
return implode(', ', $this->value); return implode( ', ', $this->value );
} }
protected function isAssociativeArray($key): bool protected function isAssociativeArray( $key ): bool {
{ return is_string( $key );
return is_string($key); }
}
protected function getPrefix($value): string protected function getPrefix( $value ): string {
{ return is_array( $value ) ? '[' : '';
return is_array($value) ? '[' : ''; }
}
protected function getSuffix($value): string protected function getSuffix( $value ): string {
{ return is_array( $value ) ? ']' : '';
return is_array($value) ? ']' : ''; }
}
} }

View file

@ -1,16 +1,13 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
class BooleanType implements TypeInterface class BooleanType implements TypeInterface {
{ public function __construct( $value ) {
public function __construct($value) $this->value = $value;
{ }
$this->value = $value;
}
public function __toString(): string public function __toString(): string {
{ return $this->value ? 'true' : 'false';
return $this->value ? 'true' : 'false'; }
}
} }

View file

@ -1,16 +1,13 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
class CustomType implements TypeInterface class CustomType implements TypeInterface {
{ public function __construct( string $value ) {
public function __construct(string $value) $this->value = $value;
{ }
$this->value = $value;
}
public function __toString(): string public function __toString(): string {
{ return $this->value;
return $this->value; }
}
} }

View file

@ -1,16 +1,13 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
class DurationType implements TypeInterface class DurationType implements TypeInterface {
{ public function __construct( string $value ) {
public function __construct(string $value) $this->value = $value;
{ }
$this->value = $value;
}
public function __toString(): string public function __toString(): string {
{ return $this->value;
return $this->value; }
}
} }

View file

@ -1,32 +1,29 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Settings; use Hosterra\FluxBuilder\Settings;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class FieldRecordType implements TypeInterface class FieldRecordType implements TypeInterface {
{ public const SETTING_IS_RECORD = 'isRecord';
public const SETTING_IS_RECORD = 'isRecord';
/** /**
* @var array $value * @var array $value
*/ */
private $value; private $value;
public function __construct(array $value) public function __construct( array $value ) {
{ $this->value = $value;
$this->value = $value; }
}
public function __toString(): string public function __toString(): string {
{ array_walk( $this->value, function ( &$value, $key ) {
array_walk($this->value, function (&$value, $key) { if ( is_string( $key ) ) {
if (is_string($key)) { $value = $key . ': ' . $value;
$value = $key . ': ' . $value; }
} } );
});
return '{' . implode(', ', $this->value) . '}'; return '{' . implode( ', ', $this->value ) . '}';
} }
} }

View file

@ -1,51 +1,47 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class FnType implements TypeInterface class FnType implements TypeInterface {
{ /**
/** * @var array $params
* @var array $params */
*/ protected $params;
protected $params;
/** /**
* @var string $content * @var string $content
*/ */
protected $content; protected $content;
private function __construct(array $params) private function __construct( array $params ) {
{ $this->params = $params;
$this->params = $params; }
}
public static function params(array $params) public static function params( array $params ) {
{ return new self( $params );
return new self($params); }
}
public function withBody(string $content) public function withBody( string $content ) {
{ $this->content = $content;
$this->content = $content;
return $this;
}
public function withBlock(string $content) return $this;
{ }
$this->content = '{ ' . $content . ' }';
return $this;
}
public function __toString(): string public function withBlock( string $content ) {
{ $this->content = '{ ' . $content . ' }';
array_walk($this->params, function (&$value, $key) {
if (is_string($key)) {
$value = $key . ' = ' . new Type($value);
}
});
return '(' . implode(', ', $this->params) . ') => ' . $this->content; return $this;
} }
public function __toString(): string {
array_walk( $this->params, function ( &$value, $key ) {
if ( is_string( $key ) ) {
$value = $key . ' = ' . new Type( $value );
}
} );
return '(' . implode( ', ', $this->params ) . ') => ' . $this->content;
}
} }

View file

@ -1,16 +1,13 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
class MathType implements TypeInterface class MathType implements TypeInterface {
{ public function __construct( string $value ) {
public function __construct(string $value) $this->value = $value;
{ }
$this->value = $value;
}
public function __toString(): string public function __toString(): string {
{ return $this->value;
return $this->value; }
}
} }

View file

@ -1,36 +1,32 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
class RecordType implements TypeInterface class RecordType implements TypeInterface {
{ /**
/** * @var array $value
* @var array $value */
*/ private $value;
private $value;
public function __construct(array $value) public function __construct( array $value ) {
{ $this->value = $value;
$this->value = $value; }
}
public function __toString(): string public function __toString(): string {
{ array_walk( $this->value, function ( &$value, $key ) {
array_walk($this->value, function (&$value, $key) { if ( is_array( $value ) ) {
if (is_array($value)) { $value = $this->getPrefix( $key ) . new RecordType( $value );
$value = $this->getPrefix($key) . new RecordType($value); } else {
} else { $value = $this->getPrefix( $key ) . new Type( $value );
$value = $this->getPrefix($key) . new Type($value); }
} } );
});
return '{' . implode(', ', $this->value) . '}'; return '{' . implode( ', ', $this->value ) . '}';
} }
private function getPrefix($key): string private function getPrefix( $key ): string {
{ return is_string( $key ) ? $key . ': ' : '';
return is_string($key) ? $key . ': ' : ''; }
}
} }

View file

@ -1,18 +1,15 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
use DateTime; use DateTime;
class TimeType implements TypeInterface class TimeType implements TypeInterface {
{ public function __construct( DateTime $dateTime ) {
public function __construct(DateTime $dateTime) $this->dateTime = $dateTime;
{ }
$this->dateTime = $dateTime;
}
public function __toString(): string public function __toString(): string {
{ return 'time(v: ' . $this->dateTime->format( 'Y-m-d\TH:i:s\Z' ) . ')';
return 'time(v: ' . $this->dateTime->format('Y-m-d\TH:i:s\Z') . ')'; }
}
} }

View file

@ -1,8 +1,7 @@
<?php <?php
namespace Arendsen\FluxQueryBuilder\Type; namespace Hosterra\FluxBuilder\Type;
interface TypeInterface interface TypeInterface {
{ public function __toString(): string;
public function __toString(): string;
} }

View file

@ -1,36 +1,33 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Expression; namespace Tests\Expression;
use Exception; use Exception;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter; use Hosterra\FluxBuilder\Expression\KeyFilter;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class KeyFilterExpressionTest extends TestCase final class KeyFilterExpressionTest extends TestCase {
{ public function testSimpleKeyvalue() {
public function testSimpleKeyvalue() $keyFilter = KeyFilter::setEqualTo( '_measurement', 'test_measurement' )
{ ->andEqualTo( '_field', 'user' )
$keyFilter = KeyFilter::setEqualTo('_measurement', 'test_measurement') ->or( 'count', '>=', '1' )
->andEqualTo('_field', 'user') ->and( 'user', KeyFilter::EQUAL_TO, 'my_username' )
->or('count', '>=', '1') ->orNotEqualTo( 'test', 'world' );
->and('user', KeyFilter::EQUAL_TO, 'my_username')
->orNotEqualTo('test', 'world');
$query = 'r._measurement == "test_measurement" and r._field == "user" or ' . $query = 'r._measurement == "test_measurement" and r._field == "user" or ' .
'r.count >= "1" and r.user == "my_username" or r.test != "world"'; 'r.count >= "1" and r.user == "my_username" or r.test != "world"';
$this->assertEquals($keyFilter->__toString(), $query); $this->assertEquals( $keyFilter->__toString(), $query );
} }
public function testInvalidOperator() public function testInvalidOperator() {
{ $this->expectException( Exception::class );
$this->expectException(Exception::class);
KeyFilter::set('_measurement', '9dkda9e', 'test_measurement') KeyFilter::set( '_measurement', '9dkda9e', 'test_measurement' )
->andEqualTo('_field', 'user') ->andEqualTo( '_field', 'user' )
->or('_field', '==', 'field2') ->or( '_field', '==', 'field2' )
->andEqualTo('user', 'my_username'); ->andEqualTo( 'user', 'my_username' );
} }
} }

View file

@ -1,43 +1,40 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow; use Hosterra\FluxBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Type\FnType; use Hosterra\FluxBuilder\Type\FnType;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class AggregateWindowFunctionTest extends TestCase final class AggregateWindowFunctionTest extends TestCase {
{ public function testSimpleWindow() {
public function testSimpleWindow() $expression = new AggregateWindow( '20s', 'mean' );
{
$expression = new AggregateWindow('20s', 'mean');
$query = '|> aggregateWindow(every: 20s, fn: mean) '; $query = '|> aggregateWindow(every: 20s, fn: mean) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testAllParameters() public function testAllParameters() {
{ $expression = new AggregateWindow(
$expression = new AggregateWindow( '20s',
'20s', FnType::params( [ 'r' ] )->withBody( 'r._field == "test"' ),
FnType::params(['r'])->withBody('r._field == "test"'), [
[ 'period' => 'every',
'period' => 'every', 'offset' => '0s',
'offset' => '0s', 'location' => 'location',
'location' => 'location', 'column' => '_value',
'column' => '_value', 'timeSrc' => '_stop',
'timeSrc' => '_stop', 'timeDst' => '_time',
'timeDst' => '_time', 'createEmpty' => false
'createEmpty' => false ]
] );
);
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: (r) => r._field == "test", ' . $query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: (r) => r._field == "test", ' .
'location: "location", ' . 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) '; 'location: "location", ' . 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Count; use Hosterra\FluxBuilder\Functions\Count;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class CountFunctionTest extends TestCase final class CountFunctionTest extends TestCase {
{ public function testSimpleCount() {
public function testSimpleCount() $expression = new Count();
{
$expression = new Count();
$query = '|> count() '; $query = '|> count() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testCountWithColumn() public function testCountWithColumn() {
{ $expression = new Count( '_value' );
$expression = new Count('_value');
$query = '|> count(column: "_value") '; $query = '|> count(column: "_value") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Duplicate; use Hosterra\FluxBuilder\Functions\Duplicate;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class DuplicateFunctionTest extends TestCase final class DuplicateFunctionTest extends TestCase {
{ public function testSimpleDuplicate() {
public function testSimpleDuplicate() $expression = new Duplicate( 'tag', 'tag_dup' );
{
$expression = new Duplicate('tag', 'tag_dup');
$query = '|> duplicate(column: "tag", as: "tag_dup") '; $query = '|> duplicate(column: "tag", as: "tag_dup") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,48 +1,33 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter; use Hosterra\FluxBuilder\Expression\KeyFilter;
use Arendsen\FluxQueryBuilder\Functions\Filter; use Hosterra\FluxBuilder\Functions\Filter;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class FilterFunctionTest extends TestCase final class FilterFunctionTest extends TestCase {
{
public function testSimpleFilter()
{
$expression = new Filter(KeyValue::setEqualTo('_measurement', 'test_measurement')
->andEqualTo('_field', 'user')
->orEqualTo('_field', 'field2')
->andEqualTo('user', 'my_username'));
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' . public function testFieldFilter() {
'r._field == "field2" and r.user == "my_username") '; $expression = new Filter( [ 'user', 'field2', 'field3' ] );
$this->assertEquals($expression->__toString(), $query); $query = '|> filter(fn: (r) => r._field == "user" or r._field == "field2" or r._field == "field3") ';
}
public function testFieldFilter() $this->assertEquals( $expression->__toString(), $query );
{ }
$expression = new Filter(['user', 'field2', 'field3']);
$query = '|> filter(fn: (r) => r._field == "user" or r._field == "field2" or r._field == "field3") '; public function testKeyFilter() {
$expression = new Filter( KeyFilter::setEqualTo( '_measurement', 'test_measurement' )
->andEqualTo( '_field', 'user' )
->orEqualTo( '_field', 'field2' )
->andEqualTo( 'user', 'my_username' ) );
$this->assertEquals($expression->__toString(), $query); $query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
} 'r._field == "field2" and r.user == "my_username") ';
public function testKeyFilter() $this->assertEquals( $expression->__toString(), $query );
{ }
$expression = new Filter(KeyFilter::setEqualTo('_measurement', 'test_measurement')
->andEqualTo('_field', 'user')
->orEqualTo('_field', 'field2')
->andEqualTo('user', 'my_username'));
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
'r._field == "field2" and r.user == "my_username") ';
$this->assertEquals($expression->__toString(), $query);
}
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\First; use Hosterra\FluxBuilder\Functions\First;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class FirstFunctionTest extends TestCase final class FirstFunctionTest extends TestCase {
{ public function testSimpleFirst() {
public function testSimpleFirst() $expression = new First();
{
$expression = new First();
$query = '|> first() '; $query = '|> first() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testFirstWithColumn() public function testFirstWithColumn() {
{ $expression = new First( 'something' );
$expression = new First('something');
$query = '|> first(column: "something") '; $query = '|> first(column: "something") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,40 +1,37 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\From; use Hosterra\FluxBuilder\Functions\From;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class FromFunctionTest extends TestCase final class FromFunctionTest extends TestCase {
{ /**
/** * @dataProvider somethingProvider
* @dataProvider somethingProvider */
*/ public function testSomething( $settings, $query ) {
public function testSomething($settings, $query) $expression = new From( $settings );
{
$expression = new From($settings);
$this->assertEquals($expression->__toString(), $query); $this->assertEquals( $expression->__toString(), $query );
} }
public function somethingProvider(): array public function somethingProvider(): array {
{ return [
return [ 'from bucket' => [
'from bucket' => [ [
[ 'bucket' => 'test-bucket',
'bucket' => 'test-bucket', ],
], 'from(bucket: "test-bucket") '
'from(bucket: "test-bucket") ' ],
], 'from bucket and host' => [
'from bucket and host' => [ [
[ 'bucket' => 'test-bucket',
'bucket' => 'test-bucket', 'host' => 'test',
'host' => 'test', ],
], 'from(bucket: "test-bucket", host: "test") '
'from(bucket: "test-bucket", host: "test") ' ],
], ];
]; }
}
} }

View file

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Group; use Hosterra\FluxBuilder\Functions\Group;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class GroupFunctionTest extends TestCase final class GroupFunctionTest extends TestCase {
{ public function testSimpleGroup() {
public function testSimpleGroup() $expression = new Group( [ 'foo', 'bar' ], 'by' );
{
$expression = new Group(['foo', 'bar'], 'by');
$query = '|> group(columns: ["foo", "bar"], mode: "by") '; $query = '|> group(columns: ["foo", "bar"], mode: "by") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Last; use Hosterra\FluxBuilder\Functions\Last;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class LastFunctionTest extends TestCase final class LastFunctionTest extends TestCase {
{ public function testSimpleLast() {
public function testSimpleLast() $expression = new Last();
{
$expression = new Last();
$query = '|> last(column: "_value") '; $query = '|> last(column: "_value") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testLastWithValue() public function testLastWithValue() {
{ $expression = new Last( 'something' );
$expression = new Last('something');
$query = '|> last(column: "something") '; $query = '|> last(column: "something") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Limit; use Hosterra\FluxBuilder\Functions\Limit;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class LimitFunctionTest extends TestCase final class LimitFunctionTest extends TestCase {
{ public function testSimpleLimit() {
public function testSimpleLimit() $expression = new Limit( 1 );
{
$expression = new Limit(1);
$query = '|> limit(n: 1, offset: 0) '; $query = '|> limit(n: 1, offset: 0) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testLimitWithOffset() public function testLimitWithOffset() {
{ $expression = new Limit( 10, 2 );
$expression = new Limit(10, 2);
$query = '|> limit(n: 10, offset: 2) '; $query = '|> limit(n: 10, offset: 2) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,47 +1,43 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Map; use Hosterra\FluxBuilder\Functions\Map;
use Arendsen\FluxQueryBuilder\Expression\Map as MapExpression; use Hosterra\FluxBuilder\Expression\Map as MapExpression;
use Arendsen\FluxQueryBuilder\Expression\Selection as SelectionExpression; use Hosterra\FluxBuilder\Expression\Selection as SelectionExpression;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class MapFunctionTest extends TestCase final class MapFunctionTest extends TestCase {
{ public function testSimpleMap() {
public function testSimpleMap() $expression = new Map( 'r with name: r.user' );
{
$expression = new Map('r with name: r.user');
$query = '|> map(fn: (r) => ({ r with name: r.user })) '; $query = '|> map(fn: (r) => ({ r with name: r.user })) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testWithMapObject() public function testWithMapObject() {
{ $expression = new Map( MapExpression::with( 'name', 'r.user' ) );
$expression = new Map(MapExpression::with('name', 'r.user'));
$query = '|> map(fn: (r) => ({ r with name: r.user })) '; $query = '|> map(fn: (r) => ({ r with name: r.user })) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testRecordMapObject() public function testRecordMapObject() {
{ $expression = new Map( MapExpression::columns( [
$expression = new Map(MapExpression::columns([ 'time' => 'r._time',
'time' => 'r._time', 'source' => 'r.tag',
'source' => 'r.tag', 'alert' => SelectionExpression::if( 'r._value > 10' )->then( true )->else( false )->__toString(),
'alert' => SelectionExpression::if('r._value > 10')->then(true)->else(false)->__toString(), 'test' => SelectionExpression::if( 'r._value > 10' )->then( 'yes' )->else( 'no' )->__toString()
'test' => SelectionExpression::if('r._value > 10')->then('yes')->else('no')->__toString() ] )->__toString() );
])->__toString());
$query = '|> map(fn: (r) => ({ {time: r._time, source: r.tag, ' . $query = '|> map(fn: (r) => ({ {time: r._time, source: r.tag, ' .
'alert: if r._value > 10 then true else false, ' . 'alert: if r._value > 10 then true else false, ' .
'test: if r._value > 10 then "yes" else "no"} })) '; 'test: if r._value > 10 then "yes" else "no"} })) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Max; use Hosterra\FluxBuilder\Functions\Max;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class MaxFunctionTest extends TestCase final class MaxFunctionTest extends TestCase {
{ public function testSimpleMax() {
public function testSimpleMax() $expression = new Max();
{
$expression = new Max();
$query = '|> max() '; $query = '|> max() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testMaxWithColumn() public function testMaxWithColumn() {
{ $expression = new Max( 'something' );
$expression = new Max('something');
$query = '|> max(column: "something") '; $query = '|> max(column: "something") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Mean; use Hosterra\FluxBuilder\Functions\Mean;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class MeanFunctionTest extends TestCase final class MeanFunctionTest extends TestCase {
{ public function testSimpleMean() {
public function testSimpleMean() $expression = new Mean();
{
$expression = new Mean();
$query = '|> mean() '; $query = '|> mean() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testWithParameterColumn() public function testWithParameterColumn() {
{ $expression = new Mean( 'test' );
$expression = new Mean('test');
$query = '|> mean(column: "test") '; $query = '|> mean(column: "test") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Min; use Hosterra\FluxBuilder\Functions\Min;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class MinFunctionTest extends TestCase final class MinFunctionTest extends TestCase {
{ public function testSimpleMin() {
public function testSimpleMin() $expression = new Min();
{
$expression = new Min();
$query = '|> min() '; $query = '|> min() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testMinWithColumn() public function testMinWithColumn() {
{ $expression = new Min( 'something' );
$expression = new Min('something');
$query = '|> min(column: "something") '; $query = '|> min(column: "something") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,47 +1,43 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use DateTime; use DateTime;
use Arendsen\FluxQueryBuilder\Functions\Range; use Hosterra\FluxBuilder\Functions\Range;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class RangeFunctionTest extends TestCase final class RangeFunctionTest extends TestCase {
{ public function testOnlyStartOption() {
public function testOnlyStartOption() $expression = new Range(
{ new DateTime( '2022-08-12 18:00:00' )
$expression = new Range( );
new DateTime('2022-08-12 18:00:00')
);
$query = '|> range(start: time(v: 2022-08-12T18:00:00Z)) '; $query = '|> range(start: time(v: 2022-08-12T18:00:00Z)) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testWithStopOption() public function testWithStopOption() {
{ $expression = new Range(
$expression = new Range( new DateTime( '2022-08-12 18:00:00' ),
new DateTime('2022-08-12 18:00:00'), new DateTime( '2022-08-12 20:00:00' )
new DateTime('2022-08-12 20:00:00') );
);
$query = '|> range(start: time(v: 2022-08-12T18:00:00Z), stop: time(v: 2022-08-12T20:00:00Z)) '; $query = '|> range(start: time(v: 2022-08-12T18:00:00Z), stop: time(v: 2022-08-12T20:00:00Z)) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testRangeInBetween() public function testRangeInBetween() {
{ $expression = new Range(
$expression = new Range( new DateTime( '2022-08-12 17:31:00' ),
new DateTime('2022-08-12 17:31:00'), new DateTime( '2022-08-12 18:31:00' )
new DateTime('2022-08-12 18:31:00') );
);
$expected = '|> range(start: time(v: 2022-08-12T17:31:00Z), stop: time(v: 2022-08-12T18:31:00Z)) '; $expected = '|> range(start: time(v: 2022-08-12T17:31:00Z), stop: time(v: 2022-08-12T18:31:00Z)) ';
$this->assertEquals($expected, $expression->__toString()); $this->assertEquals( $expected, $expression->__toString() );
} }
} }

View file

@ -1,21 +1,19 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Reduce; use Hosterra\FluxBuilder\Functions\Reduce;
use Arendsen\FluxQueryBuilder\Type\MathType; use Hosterra\FluxBuilder\Type\MathType;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class ReduceFunctionTest extends TestCase final class ReduceFunctionTest extends TestCase {
{ public function testSimpleReduce() {
public function testSimpleReduce() $expression = new Reduce( [ 'sum' => new MathType( 'r._value + accumulator.sum' ) ], [ 'sum' => 0 ] );
{
$expression = new Reduce(['sum' => new MathType('r._value + accumulator.sum')], ['sum' => 0]);
$query = '|> reduce(fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0}) '; $query = '|> reduce(fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0}) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Sort; use Hosterra\FluxBuilder\Functions\Sort;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class SortFunctionTest extends TestCase final class SortFunctionTest extends TestCase {
{ public function testSimpleSort() {
public function testSimpleSort() $expression = new Sort( [ 'foo', 'bar' ], true );
{
$expression = new Sort(['foo', 'bar'], true);
$query = '|> sort(columns: ["foo", "bar"], desc: true) '; $query = '|> sort(columns: ["foo", "bar"], desc: true) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Sum; use Hosterra\FluxBuilder\Functions\Sum;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class SumFunctionTest extends TestCase final class SumFunctionTest extends TestCase {
{ public function testSimpleSum() {
public function testSimpleSum() $expression = new Sum( '_value' );
{
$expression = new Sum('_value');
$query = '|> sum(column: "_value") '; $query = '|> sum(column: "_value") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,29 +1,26 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Unique; use Hosterra\FluxBuilder\Functions\Unique;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class UniqueunctionTest extends TestCase final class UniqueunctionTest extends TestCase {
{ public function testSimpleUnique() {
public function testSimpleUnique() $expression = new Unique();
{
$expression = new Unique();
$query = '|> unique() '; $query = '|> unique() ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testUniqueWithColumn() public function testUniqueWithColumn() {
{ $expression = new Unique( 'something' );
$expression = new Unique('something');
$query = '|> unique(column: "something") '; $query = '|> unique(column: "something") ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,38 +1,35 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Functions; namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Window; use Hosterra\FluxBuilder\Functions\Window;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class WindowFunctionTest extends TestCase final class WindowFunctionTest extends TestCase {
{ public function testSimpleWindow() {
public function testSimpleWindow() $expression = new Window( '20s' );
{
$expression = new Window('20s');
$query = '|> window(every: 20s) '; $query = '|> window(every: 20s) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
public function testAllParameters() public function testAllParameters() {
{ $expression = new Window( '20s', [
$expression = new Window('20s', [ 'period' => 'every',
'period' => 'every', 'offset' => '0s',
'offset' => '0s', 'location' => 'location',
'location' => 'location', 'timeColumn' => '_time',
'timeColumn' => '_time', 'startColumn' => '_start',
'startColumn' => '_start', 'stopColumn' => '_stop',
'stopColumn' => '_stop', 'createEmpty' => true
'createEmpty' => true ] );
]);
$query = '|> window(every: 20s, period: every, offset: 0s, location: "location", ' . $query = '|> window(every: 20s, period: every, offset: 0s, location: "location", ' .
'timeColumn: "_time", startColumn: "_start", stopColumn: "_stop", createEmpty: true) '; 'timeColumn: "_time", startColumn: "_start", stopColumn: "_stop", createEmpty: true) ';
$this->assertEquals($query, $expression->__toString()); $this->assertEquals( $query, $expression->__toString() );
} }
} }

View file

@ -1,256 +1,253 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests; namespace Tests;
use Closure; use Closure;
use DateTime; use DateTime;
use Exception; use Exception;
use Arendsen\FluxQueryBuilder\Expression\KeyFilter; use Hosterra\FluxBuilder\Expression\KeyFilter;
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Arendsen\FluxQueryBuilder\QueryBuilder; use Hosterra\FluxBuilder\QueryBuilder;
use Arendsen\FluxQueryBuilder\Type\MathType; use Hosterra\FluxBuilder\Type\MathType;
final class QueryBuilderTest extends TestCase final class QueryBuilderTest extends TestCase {
{ /**
/** * @dataProvider newTestsProvider
* @dataProvider newTestsProvider */
*/ public function testBasicQuery( string $methodName, array $params = [], string $expected = '' ) {
public function testBasicQuery(string $methodName, array $params = [], string $expected = '') $queryBuilder = new QueryBuilder();
{ $queryBuilder->fromBucket( 'test_bucket' )
$queryBuilder = new QueryBuilder(); ->addRangeStart( new DateTime( '2022-08-12 17:31:00' ) )
$queryBuilder->fromBucket('test_bucket') ->fromMeasurement( 'test_measurement' );
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
->fromMeasurement('test_measurement');
call_user_func_array([$queryBuilder, $methodName], $params); call_user_func_array( [ $queryBuilder, $methodName ], $params );
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' . $expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
'|> filter(fn: (r) => r._measurement == "test_measurement") ' . $expected; '|> filter(fn: (r) => r._measurement == "test_measurement") ' . $expected;
$this->assertEquals($expectedQuery, $queryBuilder->build()); $this->assertEquals( $expectedQuery, $queryBuilder->build() );
} }
public function newTestsProvider(): array public function newTestsProvider(): array {
{ return [
return [ 'addAggregateWindow' => [
'addAggregateWindow' => [ 'addAggregateWindow',
'addAggregateWindow', [ '20s', 'mean', [ 'timeDst' => '_time' ] ],
['20s', 'mean', ['timeDst' => '_time']], '|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") '
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") ' ],
], 'addBottom' => [
'addBottom' => [ 'addBottom',
'addBottom', [ 2, [ '_value' ] ],
[2, ['_value']], '|> bottom(n: 2, columns: ["_value"]) '
'|> bottom(n: 2, columns: ["_value"]) ' ],
], 'addCount' => [
'addCount' => [ 'addCount',
'addCount', [ '_value' ],
['_value'], '|> count(column: "_value") '
'|> count(column: "_value") ' ],
], 'addDuplicate' => [
'addDuplicate' => [ 'addDuplicate',
'addDuplicate', [ 'old_name', 'new_name' ],
['old_name', 'new_name'], '|> duplicate(column: "old_name", as: "new_name") '
'|> duplicate(column: "old_name", as: "new_name") ' ],
], 'addFilter' => [
'addFilter' => [ 'addFilter',
'addFilter', [ KeyValue::setGreaterOrEqualTo( 'count', 2 )->andGreaterOrEqualTo( 'count2', 3 ) ],
[KeyValue::setGreaterOrEqualTo('count', 2)->andGreaterOrEqualTo('count2', 3)], '|> filter(fn: (r) => r.count >= 2 and r.count2 >= 3) '
'|> filter(fn: (r) => r.count >= 2 and r.count2 >= 3) ' ],
], 'addKeyFilter' => [
'addKeyFilter' => [ 'addKeyFilter',
'addKeyFilter', [ KeyFilter::setEqualTo( 'user', 'username' ) ],
[KeyFilter::setEqualTo('user', 'username')], '|> filter(fn: (r) => r.user == "username") '
'|> filter(fn: (r) => r.user == "username") ' ],
], 'addFieldFilter' => [
'addFieldFilter' => [ 'addFieldFilter',
'addFieldFilter', [ [ 'email', 'username' ] ],
[['email', 'username']], '|> filter(fn: (r) => r._field == "email" or r._field == "username") '
'|> filter(fn: (r) => r._field == "email" or r._field == "username") ' ],
], 'addFirst' => [
'addFirst' => [ 'addFirst',
'addFirst', [ 'something' ],
['something'], '|> first(column: "something") '
'|> first(column: "something") ' ],
], 'addGroup' => [
'addGroup' => [ 'addGroup',
'addGroup', [ [ '_field', 'ip' ] ],
[['_field', 'ip']], '|> group(columns: ["_field", "ip"], mode: "by") '
'|> group(columns: ["_field", "ip"], mode: "by") ' ],
], 'addLast' => [
'addLast' => [ 'addLast',
'addLast', [ 'something' ],
['something'], '|> last(column: "something") '
'|> last(column: "something") ' ],
], 'addLimit' => [
'addLimit' => [ 'addLimit',
'addLimit', [ 20, 40 ],
[20, 40], '|> limit(n: 20, offset: 40) '
'|> limit(n: 20, offset: 40) ' ],
], 'addMap' => [
'addMap' => [ 'addMap',
'addMap', [ 'r with name: r.user' ],
['r with name: r.user'], '|> map(fn: (r) => ({ r with name: r.user })) '
'|> map(fn: (r) => ({ r with name: r.user })) ' ],
], 'addMax' => [
'addMax' => [ 'addMax',
'addMax', [],
[], '|> max() '
'|> max() ' ],
], 'addMean' => [
'addMean' => [ 'addMean',
'addMean', [ 'something' ],
['something'], '|> mean(column: "something") '
'|> mean(column: "something") ' ],
], 'addMin' => [
'addMin' => [ 'addMin',
'addMin', [ 'something' ],
['something'], '|> min(column: "something") '
'|> min(column: "something") ' ],
], 'addRawFunction' => [
'addRawFunction' => [ 'addRawFunction',
'addRawFunction', [ '|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time")' ],
['|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time")'], '|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") '
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") ' ],
], 'addReduce' => [
'addReduce' => [ 'addReduce',
'addReduce', [ [ 'count' => new MathType( 'accumulator.count + 1' ) ], [ 'count' => 0 ] ],
[['count' => new MathType('accumulator.count + 1')], ['count' => 0]], '|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) '
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' ],
], 'addSort' => [
'addSort' => [ 'addSort',
'addSort', [ [ 'column1', 'column2' ], true ],
[['column1', 'column2'], true], '|> sort(columns: ["column1", "column2"], desc: true) '
'|> sort(columns: ["column1", "column2"], desc: true) ' ],
], 'addSum' => [
'addSum' => [ 'addSum',
'addSum', [ 'something' ],
['something'], '|> sum(column: "something") '
'|> sum(column: "something") ' ],
], 'addUnique' => [
'addUnique' => [ 'addUnique',
'addUnique', [ 'something' ],
['something'], '|> unique(column: "something") '
'|> unique(column: "something") ' ],
], 'addUnwindow' => [
'addUnwindow' => [ 'addUnwindow',
'addUnwindow', [],
[], '|> window(every: inf) '
'|> window(every: inf) ' ],
], 'addWindow' => [
'addWindow' => [ 'addWindow',
'addWindow', [ '20s', [ 'timeColumn' => '_time' ] ],
['20s', ['timeColumn' => '_time']], '|> window(every: 20s, timeColumn: "_time") '
'|> window(every: 20s, timeColumn: "_time") ' ],
], ];
]; }
}
/** /**
* @dataProvider throwsExceptionWithoutRequiredDataProvider * @dataProvider throwsExceptionWithoutRequiredDataProvider
*/ */
public function testThrowsExceptionWithoutRequiredData($from, $measurement, $range) public function testThrowsExceptionWithoutRequiredData( $from, $measurement, $range ) {
{ $this->expectException( Exception::class );
$this->expectException(Exception::class);
$queryBuilder = new QueryBuilder(); $queryBuilder = new QueryBuilder();
if ($from) { if ( $from ) {
$queryBuilder->from($from); $queryBuilder->from( $from );
} }
if ($range) { if ( $range ) {
$queryBuilder->addRangeStart($range['start']); $queryBuilder->addRangeStart( $range['start'] );
} }
if ($measurement) { if ( $measurement ) {
$queryBuilder->fromMeasurement($measurement); $queryBuilder->fromMeasurement( $measurement );
} }
$queryBuilder->build(); $queryBuilder->build();
} }
public function throwsExceptionWithoutRequiredDataProvider(): array public function throwsExceptionWithoutRequiredDataProvider(): array {
{ return [
return [ 'without from data' => [
'without from data' => [ null,
null, 'test_measurement', ['start' => new DateTime('2022-08-12 20:05:00')], 'test_measurement',
], [ 'start' => new DateTime( '2022-08-12 20:05:00' ) ],
'without measurement data' => [ ],
['from' => 'test_bucket'], null, ['start' => new DateTime('2022-08-12 20:05:00')], 'without measurement data' => [
], [ 'from' => 'test_bucket' ],
'without range data' => [ null,
['from' => 'test_bucket'], 'test_measurement', null, [ 'start' => new DateTime( '2022-08-12 20:05:00' ) ],
], ],
]; 'without range data' => [
} [ 'from' => 'test_bucket' ],
'test_measurement',
null,
],
];
}
public function testThrowsExceptionWhenIncorrectOrder() public function testThrowsExceptionWhenIncorrectOrder() {
{ $this->expectException( Exception::class );
$this->expectException(Exception::class);
$queryBuilder = new QueryBuilder(); $queryBuilder = new QueryBuilder();
$queryBuilder->from(['bucket' => 'test_bucket']) $queryBuilder->from( [ 'bucket' => 'test_bucket' ] )
->fromMeasurement('test_measurement') ->fromMeasurement( 'test_measurement' )
->addRangeStart(new DateTime('2022-08-12 20:05:00')); ->addRangeStart( new DateTime( '2022-08-12 20:05:00' ) );
$queryBuilder->build(); $queryBuilder->build();
} }
public function testQueryWithWindow() public function testQueryWithWindow() {
{ $queryBuilder = new QueryBuilder();
$queryBuilder = new QueryBuilder(); $queryBuilder->fromBucket( 'test_bucket' )
$queryBuilder->fromBucket('test_bucket') ->addRangeStart( new DateTime( '2022-08-12 17:31:00' ) )
->addRangeStart(new DateTime('2022-08-12 17:31:00')) ->addReduce( [ 'count' => new MathType( 'accumulator.count + 1' ) ], [ 'count' => 0 ] )
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0]) ->addWindow( '20s' )
->addWindow('20s') ->addMean()
->addMean() ->addDuplicate( 'tag', 'tag_dup' )
->addDuplicate('tag', 'tag_dup') ->fromMeasurement( 'test_measurement' )
->fromMeasurement('test_measurement') ->addFilter( KeyValue::setGreaterOrEqualTo( 'count', 2 )->andGreaterOrEqualTo( 'count2', 3 ) )
->addFilter(KeyValue::setGreaterOrEqualTo('count', 2)->andGreaterOrEqualTo('count2', 3)) ->addKeyFilter( KeyFilter::setGreaterOrEqualTo( 'count', 1 )->andGreaterOrEqualTo( 'count2', 2 ) )
->addKeyFilter(KeyFilter::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2)) ->addUnWindow();
->addUnWindow();
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' . $expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' . '|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
'|> window(every: 20s) |> mean() |> duplicate(column: "tag", as: "tag_dup") ' . '|> window(every: 20s) |> mean() |> duplicate(column: "tag", as: "tag_dup") ' .
'|> filter(fn: (r) => r._measurement == "test_measurement") ' . '|> filter(fn: (r) => r._measurement == "test_measurement") ' .
'|> filter(fn: (r) => r.count >= 2 and r.count2 >= 3) ' . '|> filter(fn: (r) => r.count >= 2 and r.count2 >= 3) ' .
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> window(every: inf) '; '|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> window(every: inf) ';
$this->assertEquals($expectedQuery, $queryBuilder->build()); $this->assertEquals( $expectedQuery, $queryBuilder->build() );
} }
public function testCorrectInstancesAreCreated() public function testCorrectInstancesAreCreated() {
{ $queryBuilder = new QueryBuilder();
$queryBuilder = new QueryBuilder(); $queryBuilder->fromBucket( 'test_bucket' )
$queryBuilder->fromBucket('test_bucket') ->addRangeStart( new DateTime( '2022-08-12 17:31:00' ) );
->addRangeStart(new DateTime('2022-08-12 17:31:00'));
$instances = [ $instances = [
\Arendsen\FluxQueryBuilder\Functions\From::class, \Hosterra\FluxBuilder\Functions\From::class,
\Arendsen\FluxQueryBuilder\Functions\Range::class, \Hosterra\FluxBuilder\Functions\Range::class,
\Arendsen\FluxQueryBuilder\Functions\Measurement::class, \Hosterra\FluxBuilder\Functions\Measurement::class,
]; ];
$requiredFluxQueryParts = $this->getClassProperty($queryBuilder, 'requiredFluxQueryParts'); $requiredFluxQueryParts = $this->getClassProperty( $queryBuilder, 'requiredFluxQueryParts' );
foreach ($requiredFluxQueryParts as $keyPart => $part) { foreach ( $requiredFluxQueryParts as $keyPart => $part ) {
$this->assertEquals($instances[$keyPart], $part); $this->assertEquals( $instances[ $keyPart ], $part );
} }
} }
private function getClassProperty($object, string $property): array private function getClassProperty( $object, string $property ): array {
{ $propertyReader = function & ( $object, $property ) {
$propertyReader = function & ($object, $property) { $value = &Closure::bind( function & () use ( $property ) {
$value = & Closure::bind(function & () use ($property) { return $this->$property;
return $this->$property; }, $object, $object )->__invoke();
}, $object, $object)->__invoke();
return $value; return $value;
}; };
return $propertyReader($object, $property); return $propertyReader( $object, $property );
} }
} }

View file

@ -1,97 +1,94 @@
<?php <?php
declare(strict_types=1); declare( strict_types=1 );
namespace Tests\Type; namespace Tests\Type;
use DateTime; use DateTime;
use Arendsen\FluxQueryBuilder\Type; use Hosterra\FluxBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\CustomType; use Hosterra\FluxBuilder\Type\CustomType;
use Arendsen\FluxQueryBuilder\Type\DurationType; use Hosterra\FluxBuilder\Type\DurationType;
use Arendsen\FluxQueryBuilder\Type\FnType; use Hosterra\FluxBuilder\Type\FnType;
use Arendsen\FluxQueryBuilder\Type\MathType; use Hosterra\FluxBuilder\Type\MathType;
use Arendsen\FluxQueryBuilder\Type\RecordType; use Hosterra\FluxBuilder\Type\RecordType;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class FactoryTypeTest extends TestCase final class FactoryTypeTest extends TestCase {
{ /**
/** * @dataProvider getFluxStringProvider
* @dataProvider getFluxStringProvider */
*/ public function testGetFluxString( $value, $expected ) {
public function testGetFluxString($value, $expected) $type = new Type( $value );
{
$type = new Type($value);
$this->assertEquals($expected, $type->__toString()); $this->assertEquals( $expected, $type->__toString() );
} }
public function getFluxStringProvider() public function getFluxStringProvider() {
{ return [
return [ 'DateTime' => [
'DateTime' => [ new DateTime( '2022-08-15 23:09:00' ),
new DateTime('2022-08-15 23:09:00'), 'time(v: 2022-08-15T23:09:00Z)',
'time(v: 2022-08-15T23:09:00Z)', ],
], 'String' => [
'String' => [ 'value',
'value', '"value"',
'"value"', ],
], 'Integer' => [
'Integer' => [ 12345,
12345, '12345'
'12345' ],
], 'Boolean True' => [
'Boolean True' => [ true,
true, 'true'
'true' ],
], 'Boolean False' => [
'Boolean False' => [ false,
false, 'false'
'false' ],
], 'Array' => [
'Array' => [ [ 'hello', 'world' ],
['hello', 'world'], '"hello", "world"'
'"hello", "world"' ],
], 'Dictionary' => [
'Dictionary' => [ [ 'hello' => 'world', 'foo' => 'bar' ],
['hello' => 'world', 'foo' => 'bar'], 'hello: "world", foo: "bar"'
'hello: "world", foo: "bar"' ],
], 'Array Multidimensional' => [
'Array Multidimensional' => [ [ 'hello' => [ 'test', 'foo' ] ],
['hello' => ['test', 'foo']], 'hello: ["test", "foo"]'
'hello: ["test", "foo"]' ],
], 'Array Multidimensional 2' => [
'Array Multidimensional 2' => [ [ 'hello' => [ 'test' => 'bar', 'foo' => 'hi' ] ],
['hello' => ['test' => 'bar', 'foo' => 'hi']], 'hello: [test: "bar", foo: "hi"]'
'hello: [test: "bar", foo: "hi"]' ],
], 'RecordType' => [
'RecordType' => [ new RecordType( [ 'foo' => 'bar', 'nested' => [ 'hello' => 'world' ] ] ),
new RecordType(['foo' => 'bar', 'nested' => ['hello' => 'world']]), '{foo: "bar", nested: {hello: "world"}}'
'{foo: "bar", nested: {hello: "world"}}' ],
], 'DurationType' => [
'DurationType' => [ new DurationType( '5h' ),
new DurationType('5h'), '5h',
'5h', ],
], 'MathType' => [
'MathType' => [ new MathType( 'r.count + 1' ),
new MathType('r.count + 1'), 'r.count + 1'
'r.count + 1' ],
], 'CustomType' => [
'CustomType' => [ new CustomType( 'this can be anything' ),
new CustomType('this can be anything'), 'this can be anything'
'this can be anything' ],
], 'FnType with only content' => [
'FnType with only content' => [ FnType::params( [] )->withBlock( 'r with _value: r._value * r._value' ),
FnType::params([])->withBlock('r with _value: r._value * r._value'), '() => { r with _value: r._value * r._value }'
'() => { r with _value: r._value * r._value }' ],
], 'FnType with params and a body' => [
'FnType with params and a body' => [ FnType::params( [ 'r' ] )->withBody( 'r with _value: r._value * r._value' ),
FnType::params(['r'])->withBody('r with _value: r._value * r._value'), '(r) => r with _value: r._value * r._value'
'(r) => r with _value: r._value * r._value' ],
], 'FnType with params and a block' => [
'FnType with params and a block' => [ FnType::params( [ 'r', 'a' ] )->withBlock( 'return r with _value: r._value * r._value' ),
FnType::params(['r', 'a'])->withBlock('return r with _value: r._value * r._value'), '(r, a) => { return r with _value: r._value * r._value }'
'(r, a) => { return r with _value: r._value * r._value }' ],
], ];
]; }
}
} }