Skip to content
Snippets Groups Projects
Commit 5a6c060d authored by Ere Maijala's avatar Ere Maijala Committed by Robert Lange
Browse files

Add simple record class for use in lightweight responses.

parent 1f5efe6e
Branches
Tags
No related merge requests found
<?php
/**
* A minimal record class for wrapping an array of fields
*
* PHP version 7
*
* Copyright (C) The National Library of Finland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Sitemap
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
namespace VuFindSearch\Response;
/**
* A minimal record class for wrapping an array of fields
*
* @category VuFind
* @package Sitemap
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
class SimpleRecord implements RecordInterface
{
/**
* Source backend identifier
*/
protected $sourceId = DEFAULT_SEARCH_BACKEND;
/**
* Field data
*
* @var array
*/
protected $fields = [];
/**
* Constructor
*
* @param array $fields Raw data
*/
public function __construct($fields)
{
$this->fields = $fields;
}
/**
* Get field contents.
*
* @param string $field Field to get
*
* @return mixed
*/
public function get($field)
{
return $this->fields[$field] ?? null;
}
/**
* Set the source backend identifier.
*
* @param string $identifier Backend identifier
*
* @return void
*/
public function setSourceIdentifier($identifier)
{
$this->sourceId = $identifier;
}
/**
* Return the source backend identifier.
*
* @return string
*/
public function getSourceIdentifier()
{
return $this->sourceId;
}
}
<?php
/**
* Unit tests for SimpleRecord class.
*
* PHP version 7
*
* Copyright (C) Villanova University 2020.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Search
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org
*/
namespace VuFindTest\Response;
use PHPUnit\Framework\TestCase;
use VuFindSearch\Response\SimpleRecord;
/**
* Unit tests for SimpleRecord class.
*
* @category VuFind
* @package Search
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org
*/
class SimpleRecordTest extends TestCase
{
/**
* Test for expected default source ID.
*
* @return void
*/
public function testDefaultSourceId()
{
$record = new SimpleRecord([]);
$this->assertEquals(DEFAULT_SEARCH_BACKEND, $record->getSourceIdentifier());
}
/**
* Test that we can change default source ID.
*
* @return void
*/
public function testSetSourceId()
{
$record = new SimpleRecord([]);
$record->setSourceIdentifier('foo');
$this->assertEquals('foo', $record->getSourceIdentifier());
}
/**
* Test retrieving data fields.
*
* @return void
*/
public function testGetFields()
{
$record = new SimpleRecord(['foo' => 'bar']);
$this->assertEquals('bar', $record->get('foo'));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment