Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.00% covered (danger)
10.00%
1 / 10
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tags
10.00% covered (danger)
10.00%
1 / 10
20.00% covered (danger)
20.00%
1 / 5
68.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResources
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTag
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Row Definition for tags
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  Db_Row
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Db\Row;
31
32use Laminas\Db\Sql\Expression;
33use Laminas\Db\Sql\Select;
34use VuFind\Db\Entity\TagsEntityInterface;
35use VuFind\Db\Table\Resource as ResourceTable;
36
37/**
38 * Row Definition for tags
39 *
40 * @category VuFind
41 * @package  Db_Row
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 *
46 * @property int    $id
47 * @property string $tag
48 */
49class Tags extends RowGateway implements \VuFind\Db\Table\DbTableAwareInterface, TagsEntityInterface
50{
51    use \VuFind\Db\Table\DbTableAwareTrait;
52
53    /**
54     * Constructor
55     *
56     * @param \Laminas\Db\Adapter\Adapter $adapter Database adapter
57     */
58    public function __construct($adapter)
59    {
60        parent::__construct('id', 'tags', $adapter);
61    }
62
63    /**
64     * Get all resources associated with the current tag.
65     *
66     * @param string $source Record source (optional limiter)
67     * @param string $sort   Resource field to sort on (optional)
68     * @param int    $offset Offset for results
69     * @param int    $limit  Limit for results (null for none)
70     *
71     * @return array
72     */
73    public function getResources(
74        $source = null,
75        $sort = null,
76        $offset = 0,
77        $limit = null
78    ) {
79        // Set up base query:
80        $tag = $this;
81        $callback = function ($select) use ($tag, $source, $sort, $offset, $limit) {
82            $columns = [
83                new Expression(
84                    'DISTINCT(?)',
85                    ['resource.id'],
86                    [Expression::TYPE_IDENTIFIER]
87                ), Select::SQL_STAR,
88            ];
89            $select->columns($columns);
90            $select->join(
91                ['rt' => 'resource_tags'],
92                'resource.id = rt.resource_id',
93                []
94            );
95            $select->where->equalTo('rt.tag_id', $tag->id);
96
97            if (!empty($source)) {
98                $select->where->equalTo('source', $source);
99            }
100
101            if (!empty($sort)) {
102                ResourceTable::applySort($select, $sort, 'resource', $columns);
103            }
104
105            if ($offset > 0) {
106                $select->offset($offset);
107            }
108            if (null !== $limit) {
109                $select->limit($limit);
110            }
111        };
112
113        $table = $this->getDbTable('Resource');
114        return $table->select($callback);
115    }
116
117    /**
118     * Id getter
119     *
120     * @return int
121     */
122    public function getId(): int
123    {
124        return $this->id;
125    }
126
127    /**
128     * Tag setter
129     *
130     * @param string $tag Tag
131     *
132     * @return TagsEntityInterface
133     */
134    public function setTag(string $tag): TagsEntityInterface
135    {
136        $this->tag = $tag;
137        return $this;
138    }
139
140    /**
141     * Tag getter
142     *
143     * @return string
144     */
145    public function getTag(): string
146    {
147        return $this->tag;
148    }
149}