Skip to content

Commit f108d51

Browse files
committed
added execpt method
1 parent 9c824a2 commit f108d51

File tree

4 files changed

+254
-9
lines changed

4 files changed

+254
-9
lines changed

examples/data1.json

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
{
2+
"data": [
3+
{
4+
"id": 42844285,
5+
"number": "1",
6+
"issued_at": "2018-01-15",
7+
"created_at": "2018-01-15T15:29:22-05:00",
8+
"due_at": "2018-01-31",
9+
"tax_rate": 0,
10+
"secondary_tax_rate": 0,
11+
"updated_at": "2018-01-15T15:34:38-05:00",
12+
"subject": null,
13+
"purchase_order": null,
14+
"type": "MatterBill",
15+
"memo": null,
16+
"start_at": "2018-01-15",
17+
"end_at": "2018-01-15",
18+
"balance": 0,
19+
"config": {
20+
"text": {
21+
"client_address_custom": "45 Hotot Street"
22+
},
23+
"show": {
24+
"client_address_id": 80750795
25+
},
26+
"css": []
27+
},
28+
"state": "paid",
29+
"kind": "revenue_kind",
30+
"total": 405,
31+
"paid": 405,
32+
"paid_at": "2018-01-15",
33+
"pending": 0,
34+
"due": 0,
35+
"can_update": false,
36+
"credits_issued": 0,
37+
"client_addresses": [
38+
{
39+
"id": 80750795
40+
}
41+
],
42+
"shared": false,
43+
"sub_total": 405,
44+
"tax_sum": 0,
45+
"secondary_tax_sum": 0,
46+
"discount": {
47+
"rate": 0,
48+
"type": "percentage",
49+
"note": null,
50+
"early_payment_rate": 0,
51+
"early_payment_period": 0
52+
},
53+
"interest": {
54+
"rate": 0,
55+
"type": 0,
56+
"period": 0,
57+
"balance": 0,
58+
"total": 0
59+
},
60+
"original_bill": null,
61+
"balances": [
62+
{
63+
"id": 1055396233,
64+
"amount": 0,
65+
"type": "Matter",
66+
"interest_amount": 0,
67+
"due": 0
68+
}
69+
],
70+
"matter_totals": [
71+
{
72+
"amount": 405
73+
}
74+
],
75+
"currency": {
76+
"id": 1,
77+
"code": "USD",
78+
"sign": "$"
79+
},
80+
"user": {
81+
"id": 345101099,
82+
"initials": "JT",
83+
"first_name": "James",
84+
"last_name": "Turner",
85+
"name": "James Turner",
86+
"phone_number": "",
87+
"rate": 225,
88+
"subscription_type": "Attorney"
89+
},
90+
"client": {
91+
"id": 950888541
92+
},
93+
"matters": [
94+
{
95+
"id": 1055396233
96+
}
97+
]
98+
},
99+
{
100+
"id": 42844287,
101+
"number": "2",
102+
"issued_at": "2018-01-15",
103+
"created_at": "2018-01-15T15:29:22-05:00",
104+
"due_at": "2018-01-31",
105+
"tax_rate": 0,
106+
"secondary_tax_rate": 0,
107+
"updated_at": "2018-03-30T15:20:57-04:00",
108+
"subject": null,
109+
"purchase_order": null,
110+
"type": "MatterBill",
111+
"memo": null,
112+
"start_at": "2018-01-15",
113+
"end_at": "2018-01-15",
114+
"balance": 740,
115+
"config": {
116+
"text": {
117+
"client_address_custom": "566 Olympic Way"
118+
},
119+
"show": {
120+
"client_address_id": 80750975
121+
},
122+
"css": []
123+
},
124+
"state": "awaiting_payment",
125+
"kind": "revenue_kind",
126+
"total": 1190,
127+
"paid": 450,
128+
"paid_at": "2018-03-30",
129+
"pending": 0,
130+
"due": 740,
131+
"can_update": false,
132+
"credits_issued": 0,
133+
"client_addresses": [
134+
{
135+
"id": 80750975
136+
}
137+
],
138+
"shared": false,
139+
"sub_total": 1190,
140+
"tax_sum": 0,
141+
"secondary_tax_sum": 0,
142+
"discount": {
143+
"rate": 0,
144+
"type": "percentage",
145+
"note": null,
146+
"early_payment_rate": 0,
147+
"early_payment_period": 0
148+
},
149+
"interest": {
150+
"rate": 0,
151+
"type": 0,
152+
"period": 0,
153+
"balance": 0,
154+
"total": 0
155+
},
156+
"original_bill": null,
157+
"balances": [
158+
{
159+
"id": 1055396221,
160+
"amount": 740,
161+
"type": "Matter",
162+
"interest_amount": 0,
163+
"due": 740
164+
}
165+
],
166+
"matter_totals": [
167+
{
168+
"amount": 1190
169+
}
170+
],
171+
"currency": {
172+
"id": 1,
173+
"code": "USD",
174+
"sign": "$"
175+
},
176+
"user": {
177+
"id": 345101099,
178+
"initials": "JT",
179+
"first_name": "James",
180+
"last_name": "Turner",
181+
"name": "James Turner",
182+
"phone_number": "",
183+
"rate": 225,
184+
"subscription_type": "Attorney"
185+
},
186+
"client": {
187+
"id": 950888733
188+
},
189+
"matters": [
190+
{
191+
"id": 1055396221
192+
}
193+
]
194+
}
195+
]
196+
}

examples/index.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
// return is_integer($payable);
1515
//});
1616
//
17-
$jq = new Jsonq('data.json');
1817

19-
$result = $jq->from('products')
20-
->select('name', 'id')
21-
->where('cat', '=', 2)
22-
->sortBy('user_id', 'asc')
23-
->get();
18+
$jq = new Jsonq('data1.json');
19+
20+
$result = $jq->from('data')
21+
->pipe(function($j) {
22+
return $j->transform(function($val) {
23+
$val['user_id'] = $val['user']['id'];
24+
return $val;
25+
});
26+
})->get();
27+
2428
dump($result);

src/JsonQueriable.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ trait JsonQueriable
2626
*/
2727
protected $_select = [];
2828

29+
/**
30+
* contains column names for except
31+
* @var array
32+
*/
33+
protected $_except = [];
34+
2935
/**
3036
* Stores base contents.
3137
*
@@ -192,6 +198,11 @@ public function isJson($value, $isReturnMap = false)
192198
}
193199

194200

201+
public function takeColumn($array)
202+
{
203+
return $this->selectColumn($this->exceptColumn($array));
204+
}
205+
195206
/**
196207
* selecting specific column
197208
*
@@ -209,6 +220,24 @@ protected function selectColumn($array)
209220
return array_intersect_key($array, array_flip((array) $keys));
210221
}
211222

223+
/**
224+
* selecting specific column
225+
*
226+
* @param $array
227+
* @return array
228+
*/
229+
protected function exceptColumn($array)
230+
{
231+
$keys = $this->_except;
232+
233+
if (count($keys) == 0) {
234+
return $array;
235+
}
236+
237+
return array_diff_key($array, array_flip((array) $keys));
238+
}
239+
240+
212241
/**
213242
* Prepare data for result
214243
*
@@ -221,11 +250,11 @@ protected function prepareResult($data, $isObject)
221250
$output = [];
222251
if ($this->isMultiArray($data)) {
223252
foreach ($data as $key => $val) {
224-
$val = $this->selectColumn($val);
253+
$val = $this->takeColumn($val);
225254
$output[$key] = $isObject ? (object) $val : $val;
226255
}
227256
} else {
228-
$output = json_decode(json_encode($this->selectColumn($data)), !$isObject);
257+
$output = json_decode(json_encode($this->takeColumn($data)), !$isObject);
229258
}
230259

231260
return $output;

src/Jsonq.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ public function select()
9898
return $this;
9999
}
100100

101+
/**
102+
* select desired column for except
103+
*
104+
* @param ... scalar
105+
* @return $this
106+
*/
107+
public function except()
108+
{
109+
$args = func_get_args();
110+
if (count($args) > 0 ){
111+
$this->_except = $args;
112+
}
113+
114+
return $this;
115+
}
116+
101117
/**
102118
* getting prepared data
103119
*
@@ -114,7 +130,7 @@ public function get($object = true)
114130
}
115131

116132
if (!$this->isMultiArray($this->_map)) {
117-
return (object) $this->selectColumn($this->_map);
133+
return (object) $this->takeColumn($this->_map);
118134
}
119135

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

0 commit comments

Comments
 (0)