Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecordCollectionFactory
85.71% covered (warning)
85.71%
12 / 14
50.00% covered (danger)
50.00%
1 / 2
5.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
 factory
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Simple factory for record collection.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   David Maus <maus@hab.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend\Pazpar2\Response;
31
32use VuFindSearch\Exception\InvalidArgumentException;
33use VuFindSearch\Response\RecordCollectionFactoryInterface;
34
35use function call_user_func;
36use function is_callable;
37
38/**
39 * Simple factory for record collection.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   David Maus <maus@hab.de>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org
46 */
47class RecordCollectionFactory implements RecordCollectionFactoryInterface
48{
49    /**
50     * Factory to turn data into a record object.
51     *
52     * @var callable
53     */
54    protected $recordFactory;
55
56    /**
57     * Class of collection.
58     *
59     * @var string
60     */
61    protected $collectionClass;
62
63    /**
64     * Constructor.
65     *
66     * @param callable $recordFactory   Record factory callback
67     * @param string   $collectionClass Class of collection
68     *
69     * @return void
70     */
71    public function __construct($recordFactory = null, $collectionClass = null)
72    {
73        // Set default record factory if none provided:
74        if (null === $recordFactory) {
75            $recordFactory = function ($i) {
76                return new Record($i);
77            };
78        } elseif (!is_callable($recordFactory)) {
79            throw new InvalidArgumentException('Record factory must be callable.');
80        }
81        $this->recordFactory = $recordFactory;
82        $this->collectionClass = $collectionClass ?? RecordCollection::class;
83    }
84
85    /**
86     * Return record collection.
87     *
88     * @param array $response Array with 'records', 'total' and 'offset' keys
89     *
90     * @return RecordCollection
91     */
92    public function factory($response)
93    {
94        $collection = new $this->collectionClass(
95            $response['total'],
96            $response['offset']
97        );
98        foreach ($response['records'] as $doc) {
99            $collection->add(call_user_func($this->recordFactory, $doc), false);
100        }
101        return $collection;
102    }
103}