diff --git a/examples/data1.json b/examples/data1.json new file mode 100644 index 0000000..2b16a56 --- /dev/null +++ b/examples/data1.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/examples/index.php b/examples/index.php index 191ba57..f3a73f4 100755 --- a/examples/index.php +++ b/examples/index.php @@ -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); \ No newline at end of file diff --git a/src/JsonQueriable.php b/src/JsonQueriable.php index cb808db..eb2e642 100644 --- a/src/JsonQueriable.php +++ b/src/JsonQueriable.php @@ -26,6 +26,12 @@ trait JsonQueriable */ protected $_select = []; + /** + * contains column names for except + * @var array + */ + protected $_except = []; + /** * Stores base contents. * @@ -192,6 +198,11 @@ public function isJson($value, $isReturnMap = false) } + public function takeColumn($array) + { + return $this->selectColumn($this->exceptColumn($array)); + } + /** * selecting specific column * @@ -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 * @@ -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; diff --git a/src/Jsonq.php b/src/Jsonq.php index 063ec99..ca3615b 100644 --- a/src/Jsonq.php +++ b/src/Jsonq.php @@ -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 * @@ -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);