Skip to content

Commit fe55989

Browse files
authored
feature KnpLabs#1031 adding code_with_match (KnpLabs#1024) (QuentinRa)
This PR was squashed before being merged into the 3.4.x-dev branch. Discussion ---------- Hi! As described in KnpLabs#1024, I added a function `code_with_match`. Commits ------- 32acac4 adding code_with_match (KnpLabs#1024) 179532a adding a new line c3be951 changes requested f89811e removing the whole phpdoc comment
1 parent de92500 commit fe55989

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

doc/search.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ $repos = $client->api('search')->repositories('github language:php');
1313
Returns a list of repositories found by such criteria.
1414

1515
### Search code
16-
16+
1717
```php
1818
$files = $client->api('search')->code('@todo language:php');
1919
```
2020

2121
Returns a list of files found by such criteria (containing "@todo" and language==php).
2222

23+
```php
24+
$files = $client->api('search')->codeWithMatch('@todo language:php');
25+
```
26+
27+
Same as code, with additional data to highlight the matching fragments (see [Text match metadata](https://docs.github.com/en/rest/reference/search#text-match-metadata)).
28+
2329
### Search issues
2430

2531
```php

lib/Github/Api/Search.php

+15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ public function code($q, $sort = 'updated', $order = 'desc')
6161
return $this->get('/search/code', ['q' => $q, 'sort' => $sort, 'order' => $order]);
6262
}
6363

64+
/**
65+
* Search code by filter (q), but will return additional data to highlight
66+
* the matched results.
67+
*
68+
* @link https://docs.github.com/en/rest/reference/search#text-match-metadata
69+
*
70+
* @return array list of code found
71+
*/
72+
public function codeWithMatch(string $q, string $sort = 'updated', string $order = 'desc'): array
73+
{
74+
$this->acceptHeaderValue = 'application/vnd.github.v3.text-match+json';
75+
76+
return $this->code($q, $sort, $order);
77+
}
78+
6479
/**
6580
* Search users by filter (q).
6681
*

test/Github/Tests/Api/SearchTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,49 @@ public function shouldSearchCodeRegardingSortAndOrder()
133133
);
134134
}
135135

136+
/**
137+
* @test
138+
*/
139+
public function shouldSearchCodeWithMatchByQuery()
140+
{
141+
$expectedArray = [['total_count' => '0']];
142+
143+
$api = $this->getApiMock();
144+
145+
$api->expects($this->once())
146+
->method('get')
147+
->with(
148+
'/search/code',
149+
['q' => 'query text', 'sort' => 'updated', 'order' => 'desc']
150+
)
151+
->will($this->returnValue($expectedArray));
152+
153+
$this->assertEquals($expectedArray, $api->codeWithMatch('query text'));
154+
}
155+
156+
/**
157+
* @test
158+
*/
159+
public function shouldSearchCodeWithMatchRegardingSortAndOrder()
160+
{
161+
$expectedArray = [['total_count' => '0']];
162+
163+
$api = $this->getApiMock();
164+
165+
$api->expects($this->once())
166+
->method('get')
167+
->with(
168+
'/search/code',
169+
['q' => 'query text', 'sort' => 'created', 'order' => 'asc']
170+
)
171+
->will($this->returnValue($expectedArray));
172+
173+
$this->assertEquals(
174+
$expectedArray,
175+
$api->codeWithMatch('query text', 'created', 'asc')
176+
);
177+
}
178+
136179
/**
137180
* @test
138181
*/

0 commit comments

Comments
 (0)