Skip to content

Commit

Permalink
Merge pull request #1 from cloudingcity/refactor/change-collection-to…
Browse files Browse the repository at this point in the history
…-array

Refactor/change collection to array
  • Loading branch information
cloudingcity authored Apr 5, 2019
2 parents a1a7775 + 9d77a48 commit ae79376
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 240 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ Get a default connection and send query
$posts = $presto->connection()->query('select * from posts')->get();
```

If set manager as global, just query directly and get data with [collection](https://github.com/tightenco/collect)
If set manager as global, just query directly and get data
```php
$posts = Presto::query('SELECT * FROM posts')->get();
$posts->toArray();

/*
[
[1, 'Good pracetice'],
Expand All @@ -57,8 +55,6 @@ $posts->toArray();
*/

$posts = Presto::query('SELECT * FROM posts')->getAssoc();
$posts->toArray();

/*
[
['id' => 1, 'title' => 'Good pracetice'],
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
"require": {
"php": ">=7.2",
"ext-json": "*",
"guzzlehttp/guzzle": "~6.3",
"tightenco/collect": "^5.7"
"guzzlehttp/guzzle": "~6.3"
},
"require-dev": {
"blastcloud/guzzler": "^1.0",
"mockery/mockery": "^1.2",
"phpstan/phpstan": "^0.11.2",
"phpstan/phpstan-mockery": "^0.11.0",
"phpunit/phpunit": "^8.0",
"squizlabs/php_codesniffer": "^3.4"
"squizlabs/php_codesniffer": "^3.4",
"symfony/var-dumper": "^4.2"
},
"autoload": {
"psr-4": {
Expand Down
45 changes: 22 additions & 23 deletions src/Collectors/AssocCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,50 @@

namespace Clouding\Presto\Collectors;

use Clouding\Presto\Contracts\Collectorable;
use Tightenco\Collect\Support\Collection;

class AssocCollector implements Collectorable
{
/**
* The collection of collect data.
* The array of collect data.
*
* @var \Tightenco\Collect\Support\Collection
* @var array
*/
protected $collection;
protected $collection = [];

/**
* Create a new collector instance.
* Collect data from presto response.
*
* @param object $response
*/
public function __construct()
public function collect(object $response)
{
$this->collection = new Collection();
if (!isset($response->data, $response->columns)) {
return;
}

$this->collection = array_merge($this->collection, $this->getAssocData($response));
}

/**
* Collect needs data from object
* Get associated data.
*
* @param object $object
* @param object $response
* @return array
*/
public function collect(object $object)
protected function getAssocData(object $response): array
{
if (!isset($object->data, $object->columns)) {
return;
}

$columns = (new Collection($object->columns))->pluck('name');
$data = (new Collection($object->data))->map(function (array $row) use ($columns) {
return $columns->combine($row)->toArray();
});
$columns = array_column($response->columns, 'name');

$this->collection = $this->collection->merge($data);
return array_map(function (array $data) use ($columns) {
return array_combine($columns, $data);
}, $response->data);
}

/**
* Get collect data.
*
* @return \Tightenco\Collect\Support\Collection
* @return array
*/
public function get(): Collection
public function get(): array
{
return $this->collection;
}
Expand Down
39 changes: 39 additions & 0 deletions src/Collectors/BasicCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Clouding\Presto\Collectors;

class BasicCollector implements Collectorable
{
/**
* The array of collect data.
*
* @var array
*/
protected $collection = [];

/**
* Collect data from presto response.
*
* @param object $response
*/
public function collect(object $response)
{
if (!isset($response->data)) {
return;
}

$this->collection = array_merge($this->collection, $response->data);
}

/**
* Get collect data.
*
* @return array
*/
public function get(): array
{
return $this->collection;
}
}
50 changes: 0 additions & 50 deletions src/Collectors/Collector.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Collectors/Collectorable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Clouding\Presto\Collectors;

interface Collectorable
{
/**
* Collect needs data from presto response.
*
* @param object $response
*/
public function collect(object $response);

/**
* Get collect data.
*
* @return array
*/
public function get(): array;
}
2 changes: 1 addition & 1 deletion src/Connection/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function connection(string $name): Connection
/**
* Make a connection.
*
* @param string $name
* @param string $name
* @return \Clouding\Presto\Connection\Connection
*/
protected function makeConnection(string $name): Connection
Expand Down
4 changes: 2 additions & 2 deletions src/Connection/QueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait QueryTrait
/**
* Begin a fluent query against a raw query.
*
* @param string $query
* @param string $query
* @return \Clouding\Presto\QueryBuilder
*/
public function query(string $query): QueryBuilder
Expand All @@ -23,7 +23,7 @@ public function query(string $query): QueryBuilder
/**
* Get query builder with processor.
*
* @param \Clouding\Presto\Processor|null $processor
* @param \Clouding\Presto\Processor|null $processor
* @return \Clouding\Presto\QueryBuilder
*/
protected function getBuilder(Processor $processor = null): QueryBuilder
Expand Down
16 changes: 8 additions & 8 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function toArray()
/**
* Determine if a given offset exists.
*
* @param string $key
* @param string $key
* @return bool
*/
public function offsetExists($key): bool
Expand All @@ -49,7 +49,7 @@ public function offsetExists($key): bool
/**
* Get the value at a given offset.
*
* @param string $key
* @param string $key
* @return mixed
*/
public function offsetGet($key)
Expand All @@ -60,8 +60,8 @@ public function offsetGet($key)
/**
* Set the value at a given offset.
*
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
*/
public function offsetSet($key, $value)
{
Expand All @@ -71,7 +71,7 @@ public function offsetSet($key, $value)
/**
* Unset the value at a given offset.
*
* @param string $key
* @param string $key
* @return void
*/
public function offsetUnset($key)
Expand All @@ -82,7 +82,7 @@ public function offsetUnset($key)
/**
* Dynamically access container items.
*
* @param string $key
* @param string $key
* @return mixed
*/
public function __get($key)
Expand All @@ -93,8 +93,8 @@ public function __get($key)
/**
* Dynamically set container items.
*
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
Expand Down
22 changes: 0 additions & 22 deletions src/Contracts/Collectorable.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Presto.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function __construct(Container $container = null)
/**
* Register a connection.
*
* @param array $config
* @param string $name
* @param array $config
* @param string $name
* @return void
*/
public function addConnection(array $config, $name = 'default')
Expand All @@ -64,8 +64,8 @@ public function setAsGlobal()
/**
* Get a fluent query builder instance.
*
* @param string $query
* @param string $connection
* @param string $query
* @param string $connection
* @return \Clouding\Presto\QueryBuilder
*/
public static function query($query, $connection = null): QueryBuilder
Expand All @@ -76,7 +76,7 @@ public static function query($query, $connection = null): QueryBuilder
/**
* Get connection instance from manager.
*
* @param string|null $connection
* @param string|null $connection
* @return \Clouding\Presto\Connection\Connection
*/
public function connection(string $connection = null): Connection
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/PrestoState.php → src/PrestoState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Clouding\Presto\Contracts;
namespace Clouding\Presto;

interface PrestoState
class PrestoState
{
/**
* Query has been accepted and is awaiting execution.
Expand Down
Loading

0 comments on commit ae79376

Please sign in to comment.