Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractJsonRecordCollectionFactory
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 factory
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 getDefaultRecordCollectionClass
n/a
0 / 0
n/a
0 / 0
0
 getDocumentListFromResponse
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3/**
4 * Simple abstract factory for record collection.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010-2024.
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 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org
29 */
30
31namespace VuFindSearch\Response;
32
33use VuFindSearch\Exception\InvalidArgumentException;
34
35use function call_user_func;
36use function gettype;
37use function is_array;
38use function is_callable;
39
40/**
41 * Simple factory for record collection.
42 *
43 * @category VuFind
44 * @package  Search
45 * @author   David Maus <maus@hab.de>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org
48 */
49abstract class AbstractJsonRecordCollectionFactory 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 (null for default)
69     * @param string   $collectionClass Class of collection
70     *
71     * @return void
72     */
73    public function __construct($recordFactory = null, $collectionClass = null)
74    {
75        // Set default record factory if none provided:
76        if (null === $recordFactory) {
77            $recordFactory = function ($i) {
78                return new JsonRecord($i);
79            };
80        } elseif (!is_callable($recordFactory)) {
81            throw new InvalidArgumentException('Record factory must be callable.');
82        }
83        $this->recordFactory = $recordFactory;
84        $this->collectionClass = $collectionClass ?? $this->getDefaultRecordCollectionClass();
85    }
86
87    /**
88     * Return record collection.
89     *
90     * @param array $response Backend response
91     *
92     * @return RecordCollection
93     */
94    public function factory($response)
95    {
96        if (!is_array($response)) {
97            throw new InvalidArgumentException(
98                sprintf(
99                    'Unexpected type of value: Expected array, got %s',
100                    gettype($response)
101                )
102            );
103        }
104        $collection = new $this->collectionClass($response);
105        foreach ($this->getDocumentListFromResponse($response) as $doc) {
106            $collection->add(call_user_func($this->recordFactory, $doc), false);
107        }
108        return $collection;
109    }
110
111    /**
112     * Get the class name of the record collection to use by default.
113     *
114     * @return string
115     */
116    abstract protected function getDefaultRecordCollectionClass(): string;
117
118    /**
119     * Given a backend response, return an array of documents.
120     *
121     * @param array $response Backend response
122     *
123     * @return array
124     */
125    abstract protected function getDocumentListFromResponse($response): array;
126}