chobie\Jira\Api::findVersionByName PHP Method

findVersionByName() public method

Helper method to find a specific version based on the name of the version.
Since: 2.0.0
public findVersionByName ( string $project_key, string $name ) : array | null
$project_key string Project Key.
$name string The version name to match on.
return array | null Version data on match or null when there is no match.
    public function findVersionByName($project_key, $name)
    {
        // Fetch all versions of this project.
        $versions = $this->getVersions($project_key);
        // Filter results on the name.
        $matching_versions = array_filter($versions, function (array $version) use($name) {
            return $version['name'] == $name;
        });
        // Early out for no results.
        if (empty($matching_versions)) {
            return null;
        }
        // Multiple results should not happen since name is unique.
        return reset($matching_versions);
    }

Usage Example

Example #1
0
 public function testFindVersionByName()
 {
     $project_key = 'POR';
     $version_id = '14206';
     $version_name = '3.36.0';
     $versions = array(array('id' => '14205', 'name' => '3.62.0'), array('id' => $version_id, 'name' => $version_name), array('id' => '14207', 'name' => '3.66.0'));
     $this->expectClientCall(Api::REQUEST_GET, '/rest/api/2/project/' . $project_key . '/versions', array(), json_encode($versions));
     $this->assertEquals(array('id' => $version_id, 'name' => $version_name), $this->api->findVersionByName($project_key, $version_name), 'Version found');
     $this->assertNull($this->api->findVersionByName($project_key, 'i_do_not_exist'));
 }