Skip to content

Commit ac86daf

Browse files
committed
init commit
0 parents  commit ac86daf

File tree

11 files changed

+549
-0
lines changed

11 files changed

+549
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
coverage.html

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tester = vendor/bin/tester
2+
tests_dir = tests/
3+
coverage_name = $(tests_dir)coverage.html
4+
php_ini = $(tests_dir)php-unix.ini
5+
php_bin = php
6+
7+
.PHONY: test coverage clean
8+
test:
9+
@$(tester) -p $(php_bin) -c $(php_ini) $(tests_dir)
10+
11+
coverage:
12+
@$(tester) -p $(php_bin) -c $(php_ini) --coverage $(coverage_name) --coverage-src src/ $(tests_dir)
13+
14+
clean:
15+
@rm -f $(coverage_name)

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "czproject/arrays",
3+
"type": "library",
4+
"description": "Array tools library.",
5+
"license": "BSD-3-Clause",
6+
"authors": [
7+
{
8+
"name": "Jan Pecha",
9+
"homepage": "https://www.janpecha.cz/"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4.0"
14+
},
15+
"autoload": {
16+
"classmap": ["src/"]
17+
},
18+
"require-dev": {
19+
"nette/tester": "^1.7"
20+
}
21+
}

license.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
New BSD License
2+
---------------
3+
4+
Copyright © 2016 Jan Pecha (https://www.janpecha.cz/) All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright notice, this
11+
list of conditions and the following disclaimer in the documentation and/or
12+
other materials provided with the distribution.
13+
* Neither the name of “CzProject“ nor the names of its contributors may be used to
14+
endorse or promote products derived from this software without specific prior
15+
written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

readme.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
# CzProject\Arrays
3+
4+
Array tools library.
5+
6+
## Usage
7+
8+
9+
``` php
10+
<?php
11+
12+
use CzProject\Arrays;
13+
14+
```
15+
16+
### `flatten()`
17+
18+
``` php
19+
<?php
20+
21+
$data = Arrays::flatten(array(
22+
'value 1',
23+
'values' => array(
24+
'value 2-1',
25+
'value 2-2',
26+
'value 2-3',
27+
),
28+
'value 3',
29+
));
30+
31+
/* Returns:
32+
[
33+
'value 1',
34+
'value 2-1',
35+
'value 2-2',
36+
'value 2-3',
37+
'value 3',
38+
]
39+
*/
40+
```
41+
42+
43+
### `fetchPairs()`
44+
45+
``` php
46+
<?php
47+
48+
$rows = array(
49+
array(
50+
'id' => 1,
51+
'name' => 'Row #1',
52+
),
53+
54+
array(
55+
'id' => 2,
56+
'name' => 'Row #2',
57+
),
58+
59+
array(
60+
'id' => 3,
61+
'name' => 'Row #3',
62+
),
63+
);
64+
65+
$data = Arrays::fetchPairs($rows, 'id', 'name');
66+
67+
/* Returns:
68+
[
69+
1 => 'Row #1',
70+
2 => 'Row #2',
71+
3 => 'Row #3',
72+
]
73+
*/
74+
```
75+
76+
77+
### `merge()`
78+
79+
``` php
80+
<?php
81+
82+
$defaultConfig = array(
83+
'parameters' => array(
84+
'database' => array(
85+
'host' => 'localhost',
86+
'database' => 'lorem_ipsum',
87+
'driver' => 'mysql',
88+
),
89+
),
90+
91+
'messages' => array(
92+
'success' => 'Success!',
93+
'error' => 'Error!',
94+
),
95+
);
96+
97+
$config = array(
98+
'parameters' => array(
99+
'database' => array(
100+
'user' => 'user123',
101+
'password' => 'password123',
102+
),
103+
),
104+
105+
'messages' => array(
106+
'error' => 'Fatal Error!',
107+
),
108+
);
109+
110+
$data = Arrays::merge($config, $defaultConfig);
111+
112+
/* Returns:
113+
[
114+
parameters => [
115+
database => [
116+
host => 'localhost',
117+
database => 'lorem_ipsum',
118+
driver => 'mysql',
119+
user => 'user123',
120+
password => 'password123',
121+
]
122+
],
123+
124+
messages => [
125+
success => 'Success!',
126+
error => 'Fatal Error!',
127+
]
128+
]
129+
*/
130+
```
131+
132+
133+
Installation
134+
------------
135+
136+
[Download a latest package](https://github.com/czproject/arrays/releases) or use [Composer](http://getcomposer.org/):
137+
138+
```
139+
composer require [--dev] czproject/arrays
140+
```
141+
142+
`CzProject\Arrays` requires PHP 5.4.0 or later.
143+
144+
145+
------------------------------
146+
147+
License: [New BSD License](license.md)
148+
<br>Author: Jan Pecha, https://www.janpecha.cz/

src/Arrays.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace CzProject;
4+
5+
6+
class Arrays
7+
{
8+
/**
9+
* @param array|\Traversable
10+
* @return array
11+
*/
12+
public static function flatten($arr)
13+
{
14+
$res = array();
15+
16+
static::recursiveWalk($arr, function ($val, $key) use (&$res) {
17+
if (is_scalar($val)) {
18+
$res[] = $val;
19+
}
20+
});
21+
22+
return $res;
23+
}
24+
25+
26+
/**
27+
* @param array|\Traversable
28+
* @return void
29+
*/
30+
public static function recursiveWalk($arr, $callback)
31+
{
32+
foreach ($arr as $key => $value) {
33+
if (is_array($value) || $value instanceof \Traversable) {
34+
static::recursiveWalk($value, $callback);
35+
36+
} else {
37+
call_user_func_array($callback, array($value, $key));
38+
// $callback($value, $key);
39+
}
40+
}
41+
}
42+
43+
44+
/**
45+
* @param array|object[]
46+
* @param string|callback
47+
* @param string|callback
48+
* @return array
49+
*/
50+
public static function fetchPairs($data, $key, $value)
51+
{
52+
$list = array();
53+
54+
foreach ($data as $row) {
55+
$itemKey = NULL;
56+
$itemLabel = NULL;
57+
58+
if (is_callable($key)) {
59+
$itemKey = call_user_func_array($key, array($row));
60+
61+
} else {
62+
$itemKey = is_array($row) ? $row[$key] : $row->{$key};
63+
}
64+
65+
if (is_callable($value)) {
66+
$itemLabel = call_user_func_array($value, array($row));
67+
68+
} else {
69+
$itemLabel = is_array($row) ? $row[$value] : $row->{$value};
70+
}
71+
72+
$list[$itemKey] = $itemLabel;
73+
}
74+
75+
return $list;
76+
}
77+
78+
79+
/**
80+
* Merges arrays. Left has higher priority than right one.
81+
* @param array|NULL
82+
* @param array|NULL
83+
* @return array|string
84+
* @see https://github.com/nette/di/blob/master/src/DI/Config/Helpers.php
85+
*/
86+
public static function merge($left, $right)
87+
{
88+
if (is_array($left) && is_array($right)) {
89+
foreach ($left as $key => $val) {
90+
if (is_int($key)) {
91+
$right[] = $val;
92+
93+
} else {
94+
if (isset($right[$key])) {
95+
$val = static::merge($val, $right[$key]);
96+
}
97+
$right[$key] = $val;
98+
}
99+
}
100+
return $right;
101+
102+
} elseif ($left === NULL && is_array($right)) {
103+
return $right;
104+
105+
} else {
106+
return $left;
107+
}
108+
}
109+
}

tests/Arrays/fetchPairs.phpt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
use CzProject\Arrays;
4+
use Tester\Assert;
5+
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
9+
test(function () {
10+
11+
$rows = array(
12+
array(
13+
'id' => 1,
14+
'name' => 'Row #1',
15+
),
16+
17+
array(
18+
'id' => 2,
19+
'name' => 'Row #2',
20+
),
21+
22+
array(
23+
'id' => 3,
24+
'name' => 'Row #3',
25+
),
26+
);
27+
28+
$data = Arrays::fetchPairs($rows, 'id', 'name');
29+
30+
Assert::same(array(
31+
1 => 'Row #1',
32+
2 => 'Row #2',
33+
3 => 'Row #3',
34+
), $data);
35+
36+
});
37+
38+
39+
test(function () {
40+
41+
$rows = array(
42+
array(
43+
'id' => 1,
44+
'name' => 'Row #1',
45+
),
46+
47+
array(
48+
'id' => 2,
49+
'name' => 'Row #2',
50+
),
51+
52+
array(
53+
'id' => 3,
54+
'name' => 'Row #3',
55+
),
56+
);
57+
58+
$data = Arrays::fetchPairs($rows, function ($row) {
59+
return $row['id'] + 10;
60+
61+
}, function ($row) {
62+
return strtoupper($row['name']);
63+
});
64+
65+
Assert::same(array(
66+
11 => 'ROW #1',
67+
12 => 'ROW #2',
68+
13 => 'ROW #3',
69+
), $data);
70+
71+
});

0 commit comments

Comments
 (0)