Skip to content

Commit 1ffdc92

Browse files
authored
Merge pull request #189 from Automattic/add-options-get-starting-with
Adding function vipgoci_options_get_starting_with()
2 parents 40d47b2 + a8988c4 commit 1ffdc92

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

options.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,3 +1454,32 @@ function vipgoci_option_phpcs_runtime_set(
14541454
}
14551455
}
14561456

1457+
/*
1458+
* Return options as key-value pairs that start
1459+
* with $start_with, but skip any options in
1460+
* $options_skip. Will sort the results according
1461+
* to key.
1462+
*/
1463+
function vipgoci_options_get_starting_with(
1464+
array $options,
1465+
string $starts_with,
1466+
array $options_skip
1467+
) :array {
1468+
$ret_vals = array();
1469+
1470+
foreach( array_keys( $options ) as $option_name ) {
1471+
if ( 0 !== strpos( $option_name, $starts_with ) ) {
1472+
continue;
1473+
}
1474+
1475+
if ( in_array( $option_name, $options_skip ) ) {
1476+
continue;
1477+
}
1478+
1479+
$ret_vals[ $option_name ] = $options[ $option_name ];
1480+
}
1481+
1482+
ksort( $ret_vals );
1483+
1484+
return $ret_vals;
1485+
}

tests/GitHubPrsImplicatedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testGitHubPrsImplicatedIncludeDraftPrs() {
8888
);
8989

9090
$this->assertSame(
91-
'80ebd6d65db88e87665b6ff1aa045f68d17ddeb7',
91+
'0d205d30dbd0917f53d00171a602806d964cd915',
9292
$prs_implicated[9]->merge_commit_sha
9393
);
9494

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Vipgoci\tests;
4+
5+
require_once( __DIR__ . '/IncludesForTests.php' );
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
// phpcs:disable PSR1.Files.SideEffects
10+
11+
final class OptionsGetStartingWithTest extends TestCase {
12+
/**
13+
* @covers ::vipgoci_options_get_starting_with
14+
*/
15+
public function testOptionsStartingWith() {
16+
$expected = array(
17+
'test0' => 't0',
18+
'test2' => 't9',
19+
);
20+
21+
$result = vipgoci_options_get_starting_with(
22+
array(
23+
'test1' => 't1',
24+
'test0' => 't0',
25+
'test2' => 't9',
26+
'atest3' => '999',
27+
'atest4' => '888',
28+
'atest0' => '777'
29+
),
30+
'test',
31+
array(
32+
'test1',
33+
)
34+
);
35+
36+
$this->assertsame(
37+
$expected,
38+
$result
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)