Completed
Push — master ( 2738e7...2ba442 )
by Vitaly
04:01
created

Field::localized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace samsoncms\api;
3
4
use samsonframework\orm\Condition;
5
use samsonframework\orm\QueryInterface;
6
7
/**
8
 * SamsonCMS additional field table entity class
9
 * @package samson\cms
10
 */
11
class Field extends \samson\activerecord\field
12
{
13
    /** Store entity name */
14
    const ENTITY = __CLASS__;
15
16
    /** Entity field names constants for using in code */
17
    const F_PRIMARY = 'FieldID';
18
    const F_IDENTIFIER = 'Name';
19
    const F_DELETION = 'Active';
20
    const F_DEFAULT = 'Value';
21
    const F_LOCALIZED = 'local';
22
23
    /** @var string Additional field value type */
24
    public $type;
25
26
    /** @var string Additional field name */
27
    public $Name;
28
29
    /** @var string Default field value */
30
    public $Value;
31
32
    /** @var bool Flag is localized */
33
    public $local;
34
35
    /** @var bool Internal existence flag */
36
    public $Active;
37
38
    /**
39
     * Get current entity instances collection by their identifiers.
40
     * Method can accept different query executors.
41
     *
42
     * @param QueryInterface $query Database query
43
     * @param string|array $fieldIDs Field identifier or their colleciton
44
     * @param self[]|array|null $return Variable where request result would be returned
45
     * @param string $executor Method name for query execution
46
     * @return bool|self[] True if material entities has been found and $return is passed
47
     *                      or self[] if only two parameters is passed.
48
     */
49
    public static function byIDs(QueryInterface $query, $fieldIDs, &$return = array(), $executor = 'exec')
50
    {
51
        $return = $query->entity(get_called_class())
52
            ->where('FieldID', $fieldIDs)
0 ignored issues
show
Bug introduced by
It seems like $fieldIDs defined by parameter $fieldIDs on line 49 can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
            ->where('Active', 1)
54
            ->orderBy('priority')
55
            ->$executor();
56
57
        // If only one argument is passed - return null, otherwise bool
58
        return func_num_args() > 2 ? sizeof($return) : $return;
59
    }
60
61
    /**
62
     * Get current entity identifiers collection by navigation identifier.
63
     *
64
     * @param QueryInterface $query Database query
65
     * @param string $navigationID Navigation identifier
66
     * @param array $return Variable where request result would be returned
67
     * @param array $materialIDs Collection of material identifiers for filtering query
68
     * @return bool|array True if field entities has been found and $return is passed
69
     *                      or collection of identifiers if only two parameters is passed.
70
     */
71
    public static function idsByNavigationID(
72
        QueryInterface $query,
73
        $navigationID,
74
        &$return = array(),
75
        $materialIDs = null
76
    ) {
77
        // Prepare query
78
        $query->entity(CMS::FIELD_NAVIGATION_RELATION_ENTITY)
79
            ->where('StructureID', $navigationID)
80
            ->where('Active', 1);
81
82
        // Add material identifier filter if passed
83
        if (isset($materialIDs)) {
84
            $query->where('MaterialID', $materialIDs);
0 ignored issues
show
Documentation introduced by
$materialIDs is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
        }
86
87
        // Perform database query and get only material identifiers collection
88
        $return = $query->fields('FieldID');
89
90
        // If only one argument is passed - return null, otherwise bool
91
        return func_num_args() > 2 ? sizeof($return) : $return;
92
    }
93
94
    /**
95
     * Get current entity instances collection by navigation identifier.
96
     *
97
     * @param QueryInterface $query Database query
98
     * @param string $navigationID Navigation identifier
99
     * @param self[]|array|null $return Variable where request result would be returned
100
     * @return bool|self[] True if field entities has been found and $return is passed
101
     *                      or self[] if only two parameters is passed.
102
     */
103
    public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array())
104
    {
105
        /** @var array $fieldIDs Collection of entity identifiers filtered by additional field */
106
        $fieldIDs = null;
107
        if (static::idsByNavigationID($query, $navigationID, $fieldIDs)) {
108
            static::byIDs($query, $fieldIDs, $return);
109
        }
110
111
        // If only one argument is passed - return null, otherwise bool
112
        return func_num_args() > 2 ? sizeof($return) : $return;
113
    }
114
115
    /**
116
     * Find additional field database record by Name.
117
     * This is generic method that should be used in nested classes to find its
118
     * records by some its primary key value.
119
     *
120
     * @param QueryInterface $query Query object instance
121
     * @param string $name Additional field name
122
     * @param self $return Variable to return found database record
123
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
124
     */
125
    public static function byName(QueryInterface $query, $name, self & $return = null)
126
    {
127
        // Get field record by name column
128
        $return = static::oneByColumn($query, 'Name', $name);
129
130
        // If only one argument is passed - return null, otherwise bool
131
        return func_num_args() > 1 ? $return == null : $return;
132
    }
133
134
    /**
135
     * Find additional field database record by Name or ID.
136
     * This is generic method that should be used in nested classes to find its
137
     * records by some its primary key value.
138
     *
139
     * @param QueryInterface $query Query object instance
140
     * @param string $nameOrID Additional field name or identifier
141
     * @param self $return Variable to return found database record
142
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
143
     */
144
    public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null)
145
    {
146
        // Create id or URL condition
147
        $idOrUrl = new Condition('OR');
148
        $idOrUrl->add('FieldID', $nameOrID)->add('Name', $nameOrID);
149
150
        // Perform query
151
        $return = $query->entity(get_called_class())->whereCondition($idOrUrl)->first();
152
153
        // If only one argument is passed - return null, otherwise bool
154
        return func_num_args() > 1 ? $return == null : $return;
155
    }
156
157
    /**
158
     * If this field has defined key=>value set.
159
     *
160
     * @return array|mixed Grouped collection of field key => value possible values or value for key passed.
161
     */
162
    public function options($key = null)
163
    {
164
        $types = array();
165
        // Convert possible field values to array
166
        foreach (explode(',', $this->Value) as $typeValue) {
167
            // Split view and value
168
            $typeValue = explode(':', $typeValue);
169
170
            // Store to key => value collection
171
            $types[$typeValue[0]] = $typeValue[1];
172
        }
173
174
        return isset($key) ? $types[$key] : $types;
175
    }
176
177
    /** @return string Get additional field value field name depending on its type */
178
    public function valueFieldName()
179
    {
180
        switch ($this->type) {
181
            case 7:
182
                return 'numeric_value';
183
            case 6:
184
                return 'key_value';
185
            default:
186
                return 'Value';
187
        }
188
    }
189
190
    /** @return bool True if field is localized */
191
    public function localized()
192
    {
193
        return ((int)$this->local) === 1;
194
    }
195
}
196