Skip to content

Commit

Permalink
Merge pull request #22 from OPUS4/OPUSVIER-3884
Browse files Browse the repository at this point in the history
OPUSVIER-3884 Convenience functions for enrichments.
  • Loading branch information
j3nsch authored Apr 11, 2018
2 parents c13bbae + 2f09996 commit 1e0d41e
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 14 deletions.
61 changes: 61 additions & 0 deletions library/Opus/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,4 +1298,65 @@ public function isOpenAccess()
}
}

/**
* @param null $key Index or key name
* @return mixed|null
* @throws Opus_Model_Exception
* @throws Opus_Security_Exception
*/
public function getEnrichment($key = null)
{
if (is_null($key) || is_numeric($key)) {
return $this->__call('getEnrichment', [$key]);
} else {
$enrichments = $this->__call('getEnrichment', []);

$matches = array_filter($enrichments, function ($enrichment) use ($key) {
return $enrichment->getKeyName() == $key;
});

switch (count($matches)) {
case 0:
return null;

case 1:
return $matches[0];

default:
return $matches;
}
}
}

/**
* Returns the value of an enrichment key
*
* @param $key Name of enrichment
* @return mixed
* @throws Opus_Model_Exception If the enrichment key does not exist
* @throws Opus_Security_Exception
*/
public function getEnrichmentValue($key)
{
$enrichment = $this->getEnrichment($key);

if (!is_null($enrichment)) {
if (is_array($enrichment)) {
return array_map(function($value) {
return $value->getValue();
}, $enrichment);
} else {
return $enrichment->getValue();
}
}
else {
$enrichmentKey = Opus_EnrichmentKey::fetchByName($key);

if (is_null($enrichmentKey)) {
throw new Opus_Model_Exception('unknown enrichment key');
} else {
return null;
}
}
}
}
17 changes: 12 additions & 5 deletions library/Opus/Enrichment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @author Pascal-Nicolas Becker <[email protected]>
* @author Gunar Maiwald <[email protected]>
* @author Jens Schwidder <[email protected]>
* @copyright Copyright (c) 2008-2017, OPUS 4 development team
* @copyright Copyright (c) 2008-2018, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

Expand All @@ -40,8 +40,15 @@
* @category Framework
* @package Opus
* @uses Opus_Model_Abstract
*
* @method setKeyName(string $name)
* @method string getKeyName()
* @method setValue(string $value)
* @method string getValue()
*/
class Opus_Enrichment extends Opus_Model_Dependent_Abstract {
class Opus_Enrichment extends Opus_Model_Dependent_Abstract
{

/**
* Primary key of the parent model.
*
Expand All @@ -63,16 +70,16 @@ class Opus_Enrichment extends Opus_Model_Dependent_Abstract {
*
* @return void
*/
protected function _init() {
protected function _init()
{
$key = new Opus_Model_Field('KeyName');
$key->setMandatory(true)->setValidator(new Zend_Validate_NotEmpty());

$value = new Opus_Model_Field('Value');
$value->setMandatory(true)
->setValidator(new Zend_Validate_NotEmpty());
->setValidator(new Zend_Validate_NotEmpty());

$this->addField($key);
$this->addField($value);
}

}
27 changes: 18 additions & 9 deletions library/Opus/EnrichmentKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* @category Framework
* @package Opus
* @author Gunar Maiwald <[email protected]>
* @copyright Copyright (c) 2011, OPUS 4 development team
* @author Jens Schwidder <[email protected]>
* @copyright Copyright (c) 2011-2018, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
* @version $Id$
*/

/**
Expand All @@ -38,8 +38,13 @@
* @category Framework
* @package Opus
* @uses Opus_Model_Abstract
*
* @method setName(string $string)
* @method string getName()
*/
class Opus_EnrichmentKey extends Opus_Model_AbstractDb {
class Opus_EnrichmentKey extends Opus_Model_AbstractDb
{

/**
* Specify the table gateway.
*
Expand All @@ -52,7 +57,8 @@ class Opus_EnrichmentKey extends Opus_Model_AbstractDb {
*
* @return array Array of Opus_EnrichmentKeys objects.
*/
public static function getAll() {
public static function getAll()
{
return self::getAllFrom('Opus_EnrichmentKey', 'Opus_Db_EnrichmentKeys');
}

Expand All @@ -62,7 +68,8 @@ public static function getAll() {
*
* @return void
*/
protected function _init() {
protected function _init()
{
$name = new Opus_Model_Field('Name');
$name->setMandatory(true)
->setValidator(new Zend_Validate_NotEmpty());
Expand All @@ -77,7 +84,8 @@ protected function _init() {
* @param string $name
* @return Opus_EnrichmentKey
*/
public static function fetchByName($name = null) {
public static function fetchByName($name = null)
{
if (false === isset($name)) {
return;
}
Expand All @@ -98,7 +106,8 @@ public static function fetchByName($name = null) {
*
* @see library/Opus/Model/Opus_Model_Abstract#getDisplayName()
*/
public function getDisplayName() {
public function getDisplayName()
{
return $this->getName();
}

Expand All @@ -107,13 +116,13 @@ public function getDisplayName() {
*
* @return array Array of Opus_EnrichmentKeys objects.
*/
public static function getAllReferenced() {
public static function getAllReferenced()
{
$table = Opus_Db_TableGateway::getInstance('Opus_Db_DocumentEnrichments');
$db = $table->getAdapter();
$select = $db->select()->from(array('document_enrichments'));
$select->reset('columns');
$select->columns("key_name")->distinct(true);
return $db->fetchCol($select);
}

}
186 changes: 186 additions & 0 deletions tests/Opus/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2597,4 +2597,190 @@ public function testGetIdentifierDoiProducesDifferentResultThanGetIdentifier()
$this->assertEquals($test1, $test2);
}

public function testGetEnrichment()
{
$keyName= 'test.key1';

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName($keyName);
$enrichmentKey->store();

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value');

$doc = new Opus_Document();
$doc->setLanguage('deu');
$doc->addEnrichment($enrichment);

$docId = $doc->store();

$doc = new Opus_Document($docId);

$enrichment = $doc->getEnrichment();

$this->assertInternalType('array', $enrichment);
$this->assertCount(1, $enrichment);
$this->assertEquals($keyName, $enrichment[0]->getKeyName());
$this->assertEquals('test-value', $enrichment[0]->getValue());

$enrichment = $doc->getEnrichment(0);

$this->assertInstanceOf('Opus_Enrichment', $enrichment);
$this->assertEquals($keyName, $enrichment->getKeyName());
$this->assertEquals('test-value', $enrichment->getValue());

$enrichment = $doc->getEnrichment($keyName);

$this->assertInstanceOf('Opus_Enrichment', $enrichment);
$this->assertEquals($keyName, $enrichment->getKeyName());
$this->assertEquals('test-value', $enrichment->getValue());
}

public function testGetEnrichmentBadKey()
{
$keyName= 'test.key1';

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName($keyName);
$enrichmentKey->store();

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value');

$doc = new Opus_Document();
$doc->setLanguage('deu');
$doc->addEnrichment($enrichment);

$docId = $doc->store();

$doc = new Opus_Document($docId);

$enrichment = $doc->getEnrichment('unknownkey');

$this->assertNull($enrichment);
}

public function testGetEnrichmentValue()
{
$keyName= 'test.key1';

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName($keyName);
$enrichmentKey->store();

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value');

$doc = new Opus_Document();
$doc->setLanguage('deu');
$doc->addEnrichment($enrichment);

$docId = $doc->store();

$doc = new Opus_Document($docId);

$value = $doc->getEnrichmentValue($keyName);

$this->assertEquals('test-value', $value);
}

/**
* @expectedException Opus_Model_Exception
* @expectedExceptionMessage unknown enrichment key
*/
public function testGetEnrichmentValueBadKey()
{
$keyName= 'test.key1';

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName($keyName);
$enrichmentKey->store();

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value');

$doc = new Opus_Document();
$doc->setLanguage('deu');
$doc->addEnrichment($enrichment);

$docId = $doc->store();

$doc = new Opus_Document($docId);

$doc->getEnrichmentValue('unknownkey');
}

public function testGetEnrichmentMultiValue()
{
$keyName= 'test.key1';

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName($keyName);
$enrichmentKey->store();

$enrichmentKey = new Opus_EnrichmentKey();
$enrichmentKey->setName('otherkey');
$enrichmentKey->store();

$doc = new Opus_Document();
$doc->setLanguage('deu');

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value');

$doc->addEnrichment($enrichment);

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName($keyName);
$enrichment->setValue('test-value-2');

$doc->addEnrichment($enrichment);

$enrichment = new Opus_Enrichment();
$enrichment->setKeyName('otherkey');
$enrichment->setValue('test-value-other');

$doc->addEnrichment($enrichment);

$docId = $doc->store();

$doc = new Opus_Document($docId);

$enrichments = $doc->getEnrichment();

$this->assertInternalType('array', $enrichments);
$this->assertCount(3, $enrichments);

$this->assertEquals($keyName, $enrichments[0]->getKeyName());
$this->assertEquals('test-value', $enrichments[0]->getValue());

$this->assertEquals($keyName, $enrichments[1]->getKeyName());
$this->assertEquals('test-value-2', $enrichments[1]->getValue());

$this->assertEquals('otherkey', $enrichments[2]->getKeyName());
$this->assertEquals('test-value-other', $enrichments[2]->getValue());

$enrichments = $doc->getEnrichment($keyName);

$this->assertInternalType('array', $enrichments);
$this->assertCount(2, $enrichments);

$this->assertEquals($keyName, $enrichments[0]->getKeyName());
$this->assertEquals('test-value', $enrichments[0]->getValue());

$this->assertEquals($keyName, $enrichments[1]->getKeyName());
$this->assertEquals('test-value-2', $enrichments[1]->getValue());

$values = $doc->getEnrichmentValue($keyName);

$this->assertInternalType('array', $values);
$this->assertCount(2, $values);
$this->assertContains('test-value', $values);
$this->assertContains('test-value-2', $values);
}
}

0 comments on commit 1e0d41e

Please sign in to comment.