Skip to content

Commit 35e14d5

Browse files
authored
Implement apull on Objects and Arrays (#8)
* add gitignore * add apull to Objects * add apull to Arrays * update docblocks * update variable names * add circle config * add bcmath
1 parent 1e43072 commit 35e14d5

File tree

6 files changed

+191
-11
lines changed

6 files changed

+191
-11
lines changed

.circleci/config.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defaults: &defaults
2+
steps:
3+
# common php steps
4+
- run: echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
5+
- run: if [ -n "$ADD_PACKAGES" ]; then apk -U add $ADD_PACKAGES; fi;
6+
- run: if [ -n "$ADD_MODULES" ]; then docker-php-ext-install $ADD_MODULES; fi;
7+
- run: echo "date.timezone = UTC" >> $(php --ini |grep Scan |awk '{print $NF}')/timezone.ini
8+
- run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
9+
10+
# pre-checkout steps
11+
12+
# checkout
13+
- checkout
14+
15+
# post-checkout steps
16+
17+
# run tests
18+
- run: composer install -n --prefer-dist
19+
- run: php vendor/phpunit/phpunit/phpunit -c phpunit.xml --log-junit /tmp/test-results/phpunit/junit.xml
20+
- store_test_results:
21+
path: /tmp/test-results
22+
23+
version: 2
24+
jobs:
25+
build-php56:
26+
<<: *defaults
27+
docker:
28+
- image: php:5.6-alpine
29+
environment:
30+
ADD_MODULES: bcmath
31+
build-php70:
32+
<<: *defaults
33+
docker:
34+
- image: php:7.0-alpine
35+
environment:
36+
ADD_MODULES: bcmath
37+
build-php71:
38+
<<: *defaults
39+
docker:
40+
- image: php:7.1-alpine
41+
environment:
42+
ADD_MODULES: bcmath
43+
44+
workflows:
45+
version: 2
46+
build:
47+
jobs:
48+
- build-php56
49+
- build-php70
50+
- build-php71

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

src/Arrays.php

+36
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ public static function ipull(array $list, $index, $keyIndex = null)
305305
return $result;
306306
}
307307

308+
/**
309+
* Short for 'array pull'. Extracts specified items from a list of arrays
310+
* and returns them in an array keyed by the original key, or alternatively the
311+
* value of another item on the array.
312+
*
313+
* @param array[] $list A list of arrays.
314+
* @param string[] $keys Array of keys to extract.
315+
* @param string|null $keyIndex Determines how **keys** will be
316+
* assigned in the result array. Use a string like
317+
* 'id' to use the specified index as each item's
318+
* key, or ##null## to preserve the original keys.
319+
*
320+
* @return array An array keyed by $keyProperty populated by the
321+
* properties specified in $properties.
322+
*/
323+
public static function apull(array $list, array $keys, $keyIndex = null)
324+
{
325+
$result = [];
326+
foreach($list as $key => $data)
327+
{
328+
if($keyIndex !== null)
329+
{
330+
$key = $data[$keyIndex];
331+
}
332+
333+
$value = [];
334+
foreach($keys as $index)
335+
{
336+
$value[$index] = Arrays::value($data, $index);
337+
}
338+
339+
$result[$key] = $value;
340+
}
341+
return $result;
342+
}
343+
308344
/**
309345
* Group a list of arrays by the value of some index. This function is the
310346
* same as @{function:mgroup}, except it operates on the values of array

src/Objects.php

+37
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,43 @@ public static function ppull(array $list, $property, $keyProperty = null)
366366
return $result;
367367
}
368368

369+
/**
370+
* Short for 'array pull'. Extracts specified properties from a list of objects
371+
* and returns them in an array keyed by the original key, or alternatively the
372+
* value of another property on the object.
373+
*
374+
* @param array $list Some list of objects.
375+
* @param string[] $properties Array of properties to extract.
376+
* @param string|null $keyProperty Determines how **keys** will be
377+
* assigned in the result array. Use a string like
378+
* 'id' to use the result of accessing the named
379+
* property as each object's key, or
380+
* ##null## to preserve the original keys.
381+
*
382+
* @return array An array keyed by $keyProperty populated by the
383+
* properties specified in $properties.
384+
*/
385+
public static function apull(array $list, array $properties, $keyProperty = null)
386+
{
387+
$result = [];
388+
foreach($list as $key => $object)
389+
{
390+
if($keyProperty !== null && is_object($object))
391+
{
392+
$key = $object->$keyProperty;
393+
}
394+
395+
$value = [];
396+
foreach($properties as $property)
397+
{
398+
$value[$property] = Objects::property($object, $property);
399+
}
400+
401+
$result[$key] = $value;
402+
}
403+
return $result;
404+
}
405+
369406
/**
370407
* Group a list of objects by the result of some method, similar to how
371408
* GROUP BY works in an SQL query. This function simplifies grouping objects

tests/ArraysTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,35 @@ public function testIpull()
352352
$this->assertEquals($expected, Arrays::ipull($list, null, 'name'));
353353
}
354354

355+
public function testApull()
356+
{
357+
$a = [
358+
'name' => "a",
359+
'value1' => 1,
360+
'value2' => 2,
361+
];
362+
$b = [
363+
'name' => "b",
364+
'value1' => 2,
365+
'value2' => 3,
366+
];
367+
$c = [
368+
'name' => "c",
369+
'value1' => 3,
370+
'value2' => 4,
371+
];
372+
$list = [$a, $b, $c];
373+
374+
$this->assertEquals(
375+
[
376+
'a' => ['value1' => 1, 'value2' => 2],
377+
'b' => ['value1' => 2, 'value2' => 3],
378+
'c' => ['value1' => 3, 'value2' => 4],
379+
],
380+
Arrays::apull($list, ['value1', 'value2'], 'name')
381+
);
382+
}
383+
355384
public function testIsort()
356385
{
357386
$list = [

tests/ObjectsTest.php

+37-11
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testClassShortName()
137137
$expectations = [
138138
['Strings', "Strings"],
139139
['\Packaged\Helpers\Strings', "Strings"],
140-
[new \Packaged\Helpers\Strings, "Strings"],
140+
[new \Packaged\Helpers\Strings(), "Strings"],
141141
];
142142
foreach($expectations as $expect)
143143
{
@@ -174,7 +174,7 @@ public function testGetNamespace()
174174
['', ''],
175175
['Strings', ''],
176176
['\Packaged\Helpers\Strings', '\Packaged\Helpers'],
177-
[new \Packaged\Helpers\Strings, '\Packaged\Helpers'],
177+
[new \Packaged\Helpers\Strings(), '\Packaged\Helpers'],
178178
];
179179
foreach($expectations as $expect)
180180
{
@@ -222,6 +222,32 @@ public function testPpull()
222222
$this->assertEquals($expected, Objects::ppull($list, null, 'name'));
223223
}
224224

225+
public function testApull()
226+
{
227+
$a = new stdClass();
228+
$a->name = "a";
229+
$a->value1 = 1;
230+
$a->value2 = 2;
231+
$b = new stdClass();
232+
$b->name = "b";
233+
$b->value1 = 2;
234+
$b->value2 = 3;
235+
$c = new stdClass();
236+
$c->name = "c";
237+
$c->value1 = 3;
238+
$c->value2 = 4;
239+
$list = [$a, $b, $c];
240+
241+
$this->assertEquals(
242+
[
243+
'a' => ['value1' => 1, 'value2' => 2],
244+
'b' => ['value1' => 2, 'value2' => 3],
245+
'c' => ['value1' => 3, 'value2' => 4],
246+
],
247+
Objects::apull($list, ['value1', 'value2'], 'name')
248+
);
249+
}
250+
225251
public function testMsort()
226252
{
227253
$a = new MFilterTestHelper('1', 'a', 'q');
@@ -251,21 +277,21 @@ public function testMGroup()
251277
$expect = [
252278
'food' => [
253279
'fruit' => ['a' => $apple],
254-
'vegetable' => ['c' => $carrot]
280+
'vegetable' => ['c' => $carrot],
255281
],
256282
'creature' => [
257-
'animal' => ['b' => $bear]
283+
'animal' => ['b' => $bear],
258284
],
259285
];
260286
$this->assertEquals($expect, Objects::mgroup($list, 'group', 'type'));
261287

262288
$expect = [
263289
'food' => [
264290
'a' => $apple,
265-
'c' => $carrot
291+
'c' => $carrot,
266292
],
267293
'creature' => [
268-
'b' => $bear
294+
'b' => $bear,
269295
],
270296
];
271297
$this->assertEquals($expect, Objects::mgroup($list, 'group'));
@@ -289,10 +315,10 @@ public function testPGroup()
289315
$expect = [
290316
'food' => [
291317
'fruit' => ['a' => $apple],
292-
'vegetable' => ['c' => $carrot]
318+
'vegetable' => ['c' => $carrot],
293319
],
294320
'creature' => [
295-
'animal' => ['b' => $bear]
321+
'animal' => ['b' => $bear],
296322
],
297323
];
298324
$this->assertEquals(
@@ -303,10 +329,10 @@ public function testPGroup()
303329
$expect = [
304330
'food' => [
305331
'a' => $apple,
306-
'c' => $carrot
332+
'c' => $carrot,
307333
],
308334
'creature' => [
309-
'b' => $bear
335+
'b' => $bear,
310336
],
311337
];
312338
$this->assertEquals($expect, Objects::pgroup($list, 'groupProperty'));
@@ -326,7 +352,7 @@ public function testPsort()
326352
["apple" => $apple, "pear" => $pear, "grape" => $grape],
327353
"name",
328354
["apple" => $apple, "grape" => $grape, "pear" => $pear],
329-
]
355+
],
330356
];
331357
foreach($expectations as $expect)
332358
{

0 commit comments

Comments
 (0)