Completed
Push — master ( 96d385...d5a6c2 )
by Vitaly
04:18
created

Field::byIDs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 4
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
    /** @var string Additional field value type */
14
    public $type;
15
16
    /** @var string Additional field name */
17
    public $Name;
18
19
    /** @var string Default field value */
20
    public $Value;
21
22
    /** @var bool Flag is localized */
23
    public $local;
24
25
    /**
26
     * Get current entity instances collection by their identifiers.
27
     * Method can accept different query executors.
28
     *
29
     * @param QueryInterface $query Database query
30
     * @param string|array $fieldIDs Field identifier or their colleciton
31
     * @param self[]|array|null $return Variable where request result would be returned
32
     * @param string $executor Method name for query execution
33
     * @return bool|self[] True if material entities has been found and $return is passed
34
     *                      or self[] if only two parameters is passed.
35
     */
36 View Code Duplication
    public static function byIDs(QueryInterface $query, $fieldIDs, &$return = array(), $executor = 'exec')
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...
37
    {
38
        $return = $query->entity(get_called_class())
39
            ->where('FieldID', $fieldIDs)
0 ignored issues
show
Bug introduced by
It seems like $fieldIDs defined by parameter $fieldIDs on line 36 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...
40
            ->where('Active', 1)
41
            ->orderBy('priority')
42
            ->$executor();
43
44
        // If only one argument is passed - return null, otherwise bool
45
        return func_num_args() > 2 ? sizeof($return) : $return;
46
    }
47
48
    /**
49
     * Get current entity identifiers collection by navigation identifier.
50
     *
51
     * @param QueryInterface $query Database query
52
     * @param string $navigationID Navigation identifier
53
     * @param array $return Variable where request result would be returned
54
     * @param array $materialIDs Collection of material identifiers for filtering query
55
     * @return bool|array True if field entities has been found and $return is passed
56
     *                      or collection of identifiers if only two parameters is passed.
57
     */
58 View Code Duplication
    public static function idsByNavigationID(
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...
59
        QueryInterface $query,
60
        $navigationID,
61
        &$return = array(),
62
        $materialIDs = null
63
    ) {
64
        // Prepare query
65
        $query->entity(CMS::FIELD_NAVIGATION_RELATION_ENTITY)
66
            ->where('StructureID', $navigationID)
67
            ->where('Active', 1);
68
69
        // Add material identifier filter if passed
70
        if (isset($materialIDs)) {
71
            $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...
72
        }
73
74
        // Perform database query and get only material identifiers collection
75
        $return = $query->fields('FieldID');
76
77
        // If only one argument is passed - return null, otherwise bool
78
        return func_num_args() > 2 ? sizeof($return) : $return;
79
    }
80
81
    /**
82
     * Get current entity instances collection by navigation identifier.
83
     *
84
     * @param QueryInterface $query Database query
85
     * @param string $navigationID Navigation identifier
86
     * @param self[]|array|null $return Variable where request result would be returned
87
     * @return bool|self[] True if field entities has been found and $return is passed
88
     *                      or self[] if only two parameters is passed.
89
     */
90 View Code Duplication
    public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array())
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...
91
    {
92
        /** @var array $fieldIDs Collection of entity identifiers filtered by additional field */
93
        $fieldIDs = null;
94
        if (static::idsByNavigationID($query, $navigationID, $fieldIDs)) {
95
            static::byIDs($query, $fieldIDs, $return);
96
        }
97
98
        // If only one argument is passed - return null, otherwise bool
99
        return func_num_args() > 2 ? sizeof($return) : $return;
100
    }
101
102
    /**
103
     * Find additional field database record by Name.
104
     * This is generic method that should be used in nested classes to find its
105
     * records by some its primary key value.
106
     *
107
     * @param QueryInterface $query Query object instance
108
     * @param string $name Additional field name
109
     * @param self $return Variable to return found database record
110
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
111
     */
112
    public static function byName(QueryInterface $query, $name, self & $return = null)
113
    {
114
        // Get field record by name column
115
        $return = static::oneByColumn($query, 'Name', $name);
116
117
        // If only one argument is passed - return null, otherwise bool
118
        return func_num_args() > 1 ? $return == null : $return;
119
    }
120
121
    /**
122
     * Find additional field database record by Name or ID.
123
     * This is generic method that should be used in nested classes to find its
124
     * records by some its primary key value.
125
     *
126
     * @param QueryInterface $query Query object instance
127
     * @param string $nameOrID Additional field name or identifier
128
     * @param self $return Variable to return found database record
129
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
130
     */
131
    public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null)
132
    {
133
        // Create id or URL condition
134
        $idOrUrl = new Condition('OR');
135
        $idOrUrl->add('FieldID', $nameOrID)->add('Name', $nameOrID);
136
137
        // Perform query
138
        $return = $query->entity(get_called_class())->whereCondition($idOrUrl)->first();
139
140
        // If only one argument is passed - return null, otherwise bool
141
        return func_num_args() > 1 ? $return == null : $return;
142
    }
143
144
    /**
145
     * If this field has defined key=>value set.
146
     *
147
     * @return array|mixed Grouped collection of field key => value possible values or value for key passed.
148
     */
149
    public function options($key = null)
150
    {
151
        $types = array();
152
        // Convert possible field values to array
153
        foreach (explode(',', $this->Value) as $typeValue) {
154
            // Split view and value
155
            $typeValue = explode(':', $typeValue);
156
157
            // Store to key => value collection
158
            $types[$typeValue[0]] = $typeValue[1];
159
        }
160
161
        return isset($key) ? $types[$key] : $types;
162
    }
163
164
    /** @return string Get additional field value field name depending on its type */
165
    public function valueFieldName()
166
    {
167
        switch ($this->type) {
168
            case 7:
169
                return 'numeric_value';
170
            case 6:
171
                return 'key_value';
172
            default:
173
                return 'Value';
174
        }
175
    }
176
}
177