Skip to content

Commit

Permalink
added execpt method
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Jun 11, 2018
1 parent 9c824a2 commit f108d51
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 9 deletions.
196 changes: 196 additions & 0 deletions examples/data1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"data": [
{
"id": 42844285,
"number": "1",
"issued_at": "2018-01-15",
"created_at": "2018-01-15T15:29:22-05:00",
"due_at": "2018-01-31",
"tax_rate": 0,
"secondary_tax_rate": 0,
"updated_at": "2018-01-15T15:34:38-05:00",
"subject": null,
"purchase_order": null,
"type": "MatterBill",
"memo": null,
"start_at": "2018-01-15",
"end_at": "2018-01-15",
"balance": 0,
"config": {
"text": {
"client_address_custom": "45 Hotot Street"
},
"show": {
"client_address_id": 80750795
},
"css": []
},
"state": "paid",
"kind": "revenue_kind",
"total": 405,
"paid": 405,
"paid_at": "2018-01-15",
"pending": 0,
"due": 0,
"can_update": false,
"credits_issued": 0,
"client_addresses": [
{
"id": 80750795
}
],
"shared": false,
"sub_total": 405,
"tax_sum": 0,
"secondary_tax_sum": 0,
"discount": {
"rate": 0,
"type": "percentage",
"note": null,
"early_payment_rate": 0,
"early_payment_period": 0
},
"interest": {
"rate": 0,
"type": 0,
"period": 0,
"balance": 0,
"total": 0
},
"original_bill": null,
"balances": [
{
"id": 1055396233,
"amount": 0,
"type": "Matter",
"interest_amount": 0,
"due": 0
}
],
"matter_totals": [
{
"amount": 405
}
],
"currency": {
"id": 1,
"code": "USD",
"sign": "$"
},
"user": {
"id": 345101099,
"initials": "JT",
"first_name": "James",
"last_name": "Turner",
"name": "James Turner",
"phone_number": "",
"rate": 225,
"subscription_type": "Attorney"
},
"client": {
"id": 950888541
},
"matters": [
{
"id": 1055396233
}
]
},
{
"id": 42844287,
"number": "2",
"issued_at": "2018-01-15",
"created_at": "2018-01-15T15:29:22-05:00",
"due_at": "2018-01-31",
"tax_rate": 0,
"secondary_tax_rate": 0,
"updated_at": "2018-03-30T15:20:57-04:00",
"subject": null,
"purchase_order": null,
"type": "MatterBill",
"memo": null,
"start_at": "2018-01-15",
"end_at": "2018-01-15",
"balance": 740,
"config": {
"text": {
"client_address_custom": "566 Olympic Way"
},
"show": {
"client_address_id": 80750975
},
"css": []
},
"state": "awaiting_payment",
"kind": "revenue_kind",
"total": 1190,
"paid": 450,
"paid_at": "2018-03-30",
"pending": 0,
"due": 740,
"can_update": false,
"credits_issued": 0,
"client_addresses": [
{
"id": 80750975
}
],
"shared": false,
"sub_total": 1190,
"tax_sum": 0,
"secondary_tax_sum": 0,
"discount": {
"rate": 0,
"type": "percentage",
"note": null,
"early_payment_rate": 0,
"early_payment_period": 0
},
"interest": {
"rate": 0,
"type": 0,
"period": 0,
"balance": 0,
"total": 0
},
"original_bill": null,
"balances": [
{
"id": 1055396221,
"amount": 740,
"type": "Matter",
"interest_amount": 0,
"due": 740
}
],
"matter_totals": [
{
"amount": 1190
}
],
"currency": {
"id": 1,
"code": "USD",
"sign": "$"
},
"user": {
"id": 345101099,
"initials": "JT",
"first_name": "James",
"last_name": "Turner",
"name": "James Turner",
"phone_number": "",
"rate": 225,
"subscription_type": "Attorney"
},
"client": {
"id": 950888733
},
"matters": [
{
"id": 1055396221
}
]
}
]
}
16 changes: 10 additions & 6 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
// return is_integer($payable);
//});
//
$jq = new Jsonq('data.json');

$result = $jq->from('products')
->select('name', 'id')
->where('cat', '=', 2)
->sortBy('user_id', 'asc')
->get();
$jq = new Jsonq('data1.json');

$result = $jq->from('data')
->pipe(function($j) {
return $j->transform(function($val) {
$val['user_id'] = $val['user']['id'];
return $val;
});
})->get();

dump($result);
33 changes: 31 additions & 2 deletions src/JsonQueriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ trait JsonQueriable
*/
protected $_select = [];

/**
* contains column names for except
* @var array
*/
protected $_except = [];

/**
* Stores base contents.
*
Expand Down Expand Up @@ -192,6 +198,11 @@ public function isJson($value, $isReturnMap = false)
}


public function takeColumn($array)
{
return $this->selectColumn($this->exceptColumn($array));
}

/**
* selecting specific column
*
Expand All @@ -209,6 +220,24 @@ protected function selectColumn($array)
return array_intersect_key($array, array_flip((array) $keys));
}

/**
* selecting specific column
*
* @param $array
* @return array
*/
protected function exceptColumn($array)
{
$keys = $this->_except;

if (count($keys) == 0) {
return $array;
}

return array_diff_key($array, array_flip((array) $keys));
}


/**
* Prepare data for result
*
Expand All @@ -221,11 +250,11 @@ protected function prepareResult($data, $isObject)
$output = [];
if ($this->isMultiArray($data)) {
foreach ($data as $key => $val) {
$val = $this->selectColumn($val);
$val = $this->takeColumn($val);
$output[$key] = $isObject ? (object) $val : $val;
}
} else {
$output = json_decode(json_encode($this->selectColumn($data)), !$isObject);
$output = json_decode(json_encode($this->takeColumn($data)), !$isObject);
}

return $output;
Expand Down
18 changes: 17 additions & 1 deletion src/Jsonq.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ public function select()
return $this;
}

/**
* select desired column for except
*
* @param ... scalar
* @return $this
*/
public function except()
{
$args = func_get_args();
if (count($args) > 0 ){
$this->_except = $args;
}

return $this;
}

/**
* getting prepared data
*
Expand All @@ -114,7 +130,7 @@ public function get($object = true)
}

if (!$this->isMultiArray($this->_map)) {
return (object) $this->selectColumn($this->_map);
return (object) $this->takeColumn($this->_map);
}

return $this->prepareResult($this->_map, $object);
Expand Down

0 comments on commit f108d51

Please sign in to comment.