Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RecordCollectionFactory
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 factory
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Factory for record collection.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) EBSCO Industries 2013
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Search
25 * @author   Michelle Milton <mmilton@epnet.com>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend\EDS\Response;
31
32use VuFindSearch\Exception\InvalidArgumentException;
33use VuFindSearch\Response\RecordCollectionFactoryInterface;
34
35use function call_user_func;
36use function gettype;
37use function is_array;
38use function is_callable;
39
40/**
41 * Factory for record collection.
42 *
43 * @category VuFind
44 * @package  Search
45 * @author   Michelle Milton <mmilton@epnet.com>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org
48 */
49class RecordCollectionFactory implements RecordCollectionFactoryInterface
50{
51    /**
52     * Factory to turn data into a record object.
53     *
54     * @var callable
55     */
56    protected $recordFactory;
57
58    /**
59     * Class of collection.
60     *
61     * @var string
62     */
63    protected $collectionClass;
64
65    /**
66     * Constructor.
67     *
68     * @param callable $recordFactory   Record factory callback
69     * @param string   $collectionClass Class of collection
70     *
71     * @return void
72     */
73    public function __construct($recordFactory = null, $collectionClass = null)
74    {
75        if (!is_callable($recordFactory)) {
76            throw new InvalidArgumentException('Record factory must be callable.');
77        }
78        $this->recordFactory = $recordFactory;
79        $this->collectionClass = $collectionClass ?? RecordCollection::class;
80    }
81
82    /**
83     * Return record collection.
84     *
85     * @param array $response EdsApi search response
86     *
87     * @return RecordCollection
88     */
89    public function factory($response)
90    {
91        if (!is_array($response)) {
92            throw new InvalidArgumentException(
93                sprintf(
94                    'Unexpected type of value: Expected array, got %s',
95                    gettype($response)
96                )
97            );
98        }
99        $collection = new $this->collectionClass($response);
100        // Obtain path to records -- first try format of the search response,
101        // then try format of the retrieve response, then give up with empty array.
102        $records = $response['SearchResult']['Data']['Records']
103            ?? $response['Records'] ?? [];
104
105        foreach ($records as $record) {
106            $collection->add(call_user_func($this->recordFactory, $record), false);
107        }
108        return $collection;
109    }
110}