CustomColumnType::getTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * COPS (Calibre OPDS PHP Server) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Sébastien Lucas <[email protected]>
7
 */
8
9
/**
10
 * A single calibre custom column
11
 */
12
abstract class CustomColumnType
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    const ALL_CUSTOMS_ID       = "cops:custom";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal cops:custom does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
15
16
    const CUSTOM_TYPE_TEXT      = "text";        // type 1 + 2
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal text does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
17
    const CUSTOM_TYPE_COMMENT   = "comments";    // type 3
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal comments does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
18
    const CUSTOM_TYPE_SERIES    = "series";      // type 4
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal series does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
19
    const CUSTOM_TYPE_ENUM      = "enumeration"; // type 5
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal enumeration does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
20
    const CUSTOM_TYPE_DATE      = "datetime";    // type 6
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal datetime does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
21
    const CUSTOM_TYPE_FLOAT     = "float";       // type 7
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal float does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
22
    const CUSTOM_TYPE_INT       = "int";         // type 8
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal int does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
23
    const CUSTOM_TYPE_RATING    = "rating";      // type 9
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal rating does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
24
    const CUSTOM_TYPE_BOOL      = "bool";        // type 10
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal bool does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
25
    const CUSTOM_TYPE_COMPOSITE = "composite";   // type 11 + 12
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal composite does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
26
27
    /** @var array[integer]CustomColumnType  */
28
    private static $customColumnCacheID = array();
29
30
    /** @var array[string]CustomColumnType  */
31
    private static $customColumnCacheLookup = array();
32
33
    /** @var integer the id of this column */
34
    public $customId;
35
    /** @var string name/title of this column */
36
    public $columnTitle;
37
    /** @var string the datatype of this column (one of the CUSTOM_TYPE_* constant values) */
38
    public $datatype;
39
    /** @var null|Entry[] */
40
    private $customValues = NULL;
41
42 14
    protected function __construct($pcustomId, $pdatatype)
43
    {
44 14
        $this->columnTitle = self::getTitleByCustomID($pcustomId);
45 14
        $this->customId = $pcustomId;
46 14
        $this->datatype = $pdatatype;
47 14
        $this->customValues = NULL;
48 14
    }
49
50
    /**
51
     * The URI to show all book swith a specific value in this column
52
     *
53
     * @param string|integer $id the id of the value to show
54
     * @return string
55
     */
56 13
    public function getUri($id)
57
    {
58 13
        return "?page=" . Base::PAGE_CUSTOM_DETAIL . "&custom={$this->customId}&id={$id}";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ?page= does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
59
    }
60
61
    /**
62
     * The URI to show all the values of this column
63
     *
64
     * @return string
65
     */
66 25
    public function getUriAllCustoms()
67
    {
68 25
        return "?page=" . Base::PAGE_ALL_CUSTOMS . "&custom={$this->customId}";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ?page= does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
69
    }
70
71
    /**
72
     * The EntryID to show all book swith a specific value in this column
73
     *
74
     * @param string|integer $id the id of the value to show
75
     * @return string
76
     */
77 17
    public function getEntryId($id)
78
    {
79 17
        return self::ALL_CUSTOMS_ID . ":" . $this->customId . ":" . $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
80
    }
81
82
    /**
83
     * The EntryID to show all the values of this column
84
     *
85
     * @return string
86
     */
87 37
    public function getAllCustomsId()
88
    {
89 37
        return self::ALL_CUSTOMS_ID . ":" . $this->customId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
90
    }
91
92
    /**
93
     * The title of this column
94
     *
95
     * @return string
96
     */
97 37
    public function getTitle()
98
    {
99 37
        return $this->columnTitle;
100
    }
101
102
    /**
103
     * The description of this column as it is definied in the database
104
     *
105
     * @return string|null
106
     */
107 20
    public function getDatabaseDescription()
108
    {
109 20
        $result = Base::getDb()->prepare('SELECT display FROM custom_columns WHERE id = ?');
110 20
        $result->execute(array($this->customId));
111 20
        if ($post = $result->fetchObject()) {
112 20
            $json = json_decode($post->display);
113 20
            return (isset($json->description) && !empty($json->description)) ? $json->description : NULL;
114
        }
115
        return NULL;
116
    }
117
118
    /**
119
     * Get the Entry for this column
120
     * This is used in the initializeContent method to display e.g. the index page
121
     *
122
     * @return Entry
123
     */
124 14
    public function getCount()
125
    {
126 14
        $ptitle = $this->getTitle();
127 14
        $pid = $this->getAllCustomsId();
128 14
        $pcontent = $this->getDescription();
129 14
        $pcontentType = $this->datatype;
130 14
        $plinkArray = array(new LinkNavigation($this->getUriAllCustoms()));
131 14
        $pclass = "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
132 14
        $pcount = $this->getDistinctValueCount();
133
134 14
        return new Entry($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass, $pcount);
135
    }
136
137
    /**
138
     * Get the amount of distinct values for this column
139
     *
140
     * @return int
141
     */
142 16
    protected function getDistinctValueCount()
143
    {
144 16
        return count($this->getAllCustomValues());
145
    }
146
147
    /**
148
     * Encode a value of this column ready to be displayed in an HTML document
149
     *
150
     * @param integer|string $value
151
     * @return string
152
     */
153 7
    public function encodeHTMLValue($value)
154
    {
155 7
        return htmlspecialchars($value);
156
    }
157
158
    /**
159
     * Get the datatype of a CustomColumn by its customID
160
     *
161
     * @param integer $customId
162
     * @return string|null
163
     */
164 17 View Code Duplication
    private static function getDatatypeByCustomID($customId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166 17
        $result = Base::getDb()->prepare('SELECT datatype FROM custom_columns WHERE id = ?');
167 17
        $result->execute(array($customId));
168 17
        if ($post = $result->fetchObject()) {
169 16
            return $post->datatype;
170
        }
171 1
        return NULL;
172
    }
173
174
    /**
175
     * Create a CustomColumnType by CustomID
176
     *
177
     * @param integer $customId the id of the custom column
178
     * @return CustomColumnType|null
179
     * @throws Exception If the $customId is not found or the datatype is unknown
180
     */
181 42
    public static function createByCustomID($customId)
182
    {
183
        // Reuse already created CustomColumns for performance
184 42
        if (array_key_exists($customId, self::$customColumnCacheID))
185 42
            return self::$customColumnCacheID[$customId];
186
187 17
        $datatype = self::getDatatypeByCustomID($customId);
188
189
        switch ($datatype) {
190 17
            case self::CUSTOM_TYPE_TEXT:
191 5
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeText($customId);
192 12
            case self::CUSTOM_TYPE_SERIES:
193 2
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeSeries($customId);
194 10
            case self::CUSTOM_TYPE_ENUM:
195 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeEnumeration($customId);
196 9
            case self::CUSTOM_TYPE_COMMENT:
197 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeComment($customId);
198 8
            case self::CUSTOM_TYPE_DATE:
199 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeDate($customId);
200 7
            case self::CUSTOM_TYPE_FLOAT:
201 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeFloat($customId);
202 6
            case self::CUSTOM_TYPE_INT:
203 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeInteger($customId);
204 5
            case self::CUSTOM_TYPE_RATING:
205 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeRating($customId);
206 4
            case self::CUSTOM_TYPE_BOOL:
207 1
                return self::$customColumnCacheID[$customId] = new CustomColumnTypeBool($customId);
208 3
            case self::CUSTOM_TYPE_COMPOSITE:
209 2
                return NULL; //TODO Currently not supported
210 1
            default:
211 1
                throw new Exception("Unkown column type: " . $datatype);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unkown column type: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
212 1
        }
213
    }
214
215
    /**
216
     * Create a CustomColumnType by its lookup name
217
     *
218
     * @param string $lookup the lookup-name of the custom column
219
     * @return CustomColumnType|null
220
     */
221 33
    public static function createByLookup($lookup)
222
    {
223
        // Reuse already created CustomColumns for performance
224 33
        if (array_key_exists($lookup, self::$customColumnCacheLookup))
225 33
            return self::$customColumnCacheLookup[$lookup];
226
227 17
        $result = Base::getDb()->prepare('SELECT id FROM custom_columns WHERE label = ?');
228 17
        $result->execute(array($lookup));
229 17
        if ($post = $result->fetchObject()) {
230 16
            return self::$customColumnCacheLookup[$lookup] = self::createByCustomID($post->id);
231
        }
232 1
        return self::$customColumnCacheLookup[$lookup] = NULL;
233
    }
234
235
    /**
236
     * Return an entry array for all possible (in the DB used) values of this column
237
     * These are the values used in the getUriAllCustoms() page
238
     *
239
     * @return Entry[]
240
     */
241 36
    public function getAllCustomValues()
242
    {
243
        // lazy loading
244 36
        if ($this->customValues == NULL)
245 36
            $this->customValues = $this->getAllCustomValuesFromDatabase();
246
247 36
        return $this->customValues;
248
    }
249
250
    /**
251
     * Get the title of a CustomColumn by its customID
252
     *
253
     * @param integer $customId
254
     * @return string
255
     */
256 14 View Code Duplication
    protected static function getTitleByCustomID($customId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
    {
258 14
        $result = Base::getDb()->prepare('SELECT name FROM custom_columns WHERE id = ?');
259 14
        $result->execute(array($customId));
260 14
        if ($post = $result->fetchObject()) {
261 14
            return $post->name;
262
        }
263
        return "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
264
    }
265
266
    /**
267
     * Get the query to find all books with a specific value of this column
268
     * the returning array has two values:
269
     *  - first the query (string)
270
     *  - second an array of all PreparedStatement parameters
271
     *
272
     * @param string|integer $id the id of the searched value
273
     * @return array
274
     */
275
    abstract public function getQuery($id);
276
277
    /**
278
     * Get a CustomColumn for a specified (by ID) value
279
     *
280
     * @param string|integer $id the id of the searched value
281
     * @return CustomColumn
282
     */
283
    abstract public function getCustom($id);
284
285
    /**
286
     * Return an entry array for all possible (in the DB used) values of this column by querying the database
287
     *
288
     * @return Entry[]
289
     */
290
    abstract protected function getAllCustomValuesFromDatabase();
291
292
    /**
293
     * The description used in the index page
294
     *
295
     * @return string
296
     */
297
    abstract public function getDescription();
298
299
    /**
300
     * Find the value of this column for a specific book
301
     *
302
     * @param Book $book
303
     * @return CustomColumn
304
     */
305
    public abstract function getCustomByBook($book);
306
307
    /**
308
     * Is this column searchable by value
309
     * only searchable columns can be displayed on the index page
310
     *
311
     * @return bool
312
     */
313
    public abstract function isSearchable();
314
}
315