Skip to content

Commit f7a3a5f

Browse files
committed
introducing OcUtils utility class,
fixes #33
1 parent 214d5c7 commit f7a3a5f

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@
5353

5454
# 1.9.0
5555
- Allow passing additonal options to Guzzle #30
56+
- Added `OcUtils` class, a utility class providing additional functionality to simplify the integration and consumption of this library.
57+
- Initially includes the `findValueByKey` function, which is meant to retrieve a specific value from the response body. [#33]

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,26 @@ $baseResponse = $ocBaseApi->setRequestConnectionTimeout(10)->get();
301301

302302
- `/sysinfo/bundles/version`: (v1.1.1) only bundle version endpoint is available. [Sysinfo Endpoint definitions WiKi](https://github.com/elan-ev/opencast-php-library/wiki/OcSysinfo)
303303

304+
# Utility Class
305+
Starting from version **v1.9.0**, a new utility class has been introduced to provide additional functionality, making it easier to integrate and consume this library's output within applications.
306+
## How to use
307+
To use the utility class, initialize it directly via its namespace and start calling its functions:
308+
```php
309+
use OpencastApi\Opencast;
310+
use OpencastApi\Util\OcUtils;
311+
...
312+
$config = [...];
313+
$opencastApi = new Opencast($config);
314+
315+
$params = [...];
316+
$response = $opencastApi->ocSearch->getEpisodes($params);
317+
318+
// Use `findValueByKey` from the Utility class to extract the mediapackage from the response,
319+
// regardless of the response's structure.
320+
$mediapackage = OcUtils::findValueByKey($response['body'], 'mediapackage');
321+
...
322+
```
323+
304324
# Mocking Responses
305325
In order to conduct proper testing, a mocking mechanism is provided.
306326
## How to use

src/OpencastApi/Util/OcUtils.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace OpencastApi\Util;
4+
5+
/**
6+
* Opencast API Utility class.
7+
*
8+
* This class provides additional functionality to simplify the integration and consumption of this library's output within applications.
9+
*/
10+
class OcUtils
11+
{
12+
/**
13+
* This function searches for a specific key in a given object or array, including nested structures.
14+
*
15+
* @param object|array $object The object or array to search in.
16+
* @param string $targetKey The key to search for.
17+
*
18+
* @return mixed|null The value of the found key, or null if the key is not found.
19+
*/
20+
public static function findValueByKey(object|array $object, string $targetKey) {
21+
if (is_object($object)) {
22+
// Perform first-level type casting,
23+
// to preserve the data types of child elements.
24+
$object = (array) $object;
25+
}
26+
27+
foreach ($object as $key => $value) {
28+
if ($key === $targetKey) {
29+
return $value;
30+
} elseif (is_array($value) || is_object($value)) {
31+
// Recursively search in nested structures.
32+
$found = self::findValueByKey($value, $targetKey);
33+
if ($found !== null) {
34+
return $found;
35+
}
36+
}
37+
}
38+
39+
return null;
40+
}
41+
}

tests/DataProvider/SearchDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static function getEpisodeQueryCases(): array
77
{
88
return [
99
[[], 'json'],
10-
[['id' => 'fe0b45b0-7ed5-4944-8b0a-a0a283331791'], ''],
10+
[['id' => 'ID-spring'], ''],
1111
[['sid' => '8010876e-1dce-4d38-ab8d-24b956e3d8b7'], ''],
1212
[['sname' => 'HUB_LOCAL_TEST'], ''],
1313
[['sort' => 'modified asc'], ''],
@@ -47,4 +47,4 @@ public static function getSeriesQueryCases(): array
4747
];
4848
}
4949
}
50-
?>
50+
?>

tests/Unit/OcSearchTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PHPUnit\Framework\TestCase;
77
use OpencastApi\Opencast;
8+
use OpencastApi\Util\OcUtils;
89

910
class OcSearchTest extends TestCase
1011
{
@@ -26,6 +27,17 @@ public function get_eposides($params, $format): void
2627
$this->assertSame(200, $response['code'], 'Failure to search episode');
2728
}
2829

30+
/**
31+
* @test
32+
*/
33+
public function find_mediapackage_using_ocutils() {
34+
$params = ['id' => 'ID-spring'];
35+
$response = $this->ocSearch->getEpisodes($params);
36+
$this->assertSame(200, $response['code'], 'Failure to search episode for OcUtils');
37+
$mediapackage = OcUtils::findValueByKey($response['body'], 'mediapackage');
38+
$this->assertNotEmpty($mediapackage, 'Cannot extract mediapackage from response using "OcUtils::findValueByKey"');
39+
}
40+
2941
/**
3042
* @test
3143
* @dataProvider \Tests\DataProvider\SearchDataProvider::getSeriesQueryCases()

0 commit comments

Comments
 (0)