Completed
Push — master ( 32d600...fb4d81 )
by Vitaly
05:25
created

Field::options()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 3
eloc 6
nc 4
nop 1
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
    /**
23
     * Find additional field database record by Name.
24
     * This is generic method that should be used in nested classes to find its
25
     * records by some its primary key value.
26
     *
27
     * @param QueryInterface $query Query object instance
28
     * @param string $name Additional field name
29
     * @param self $return Variable to return found database record
30
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
31
     */
32
    public static function byName(QueryInterface $query, $name, self & $return = null)
33
    {
34
        // Get field record by name column
35
        $return = static::oneByColumn($query, 'Name', $name);
36
37
        // If only one argument is passed - return null, otherwise bool
38
        return func_num_args() > 1 ? $return == null : $return;
39
    }
40
41
    /**
42
     * Find additional field database record by Name or ID.
43
     * This is generic method that should be used in nested classes to find its
44
     * records by some its primary key value.
45
     *
46
     * @param QueryInterface $query Query object instance
47
     * @param string $nameOrID Additional field name or identifier
48
     * @param self $return Variable to return found database record
49
     * @return bool|null|self  Field instance or null if 3rd parameter not passed
50
     */
51 View Code Duplication
    public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null)
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...
52
    {
53
        // Create id or URL condition
54
        $idOrUrl = new Condition('OR');
55
        $idOrUrl->add('FieldID', $nameOrID)->add('Name', $nameOrID);
56
57
        // Perform query
58
        $return = $query->entity(get_called_class())->where($idOrUrl)->first();
0 ignored issues
show
Documentation introduced by
$idOrUrl is of type object<samsonframework\orm\Condition>, but the function expects a string.

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...
59
60
        // If only one argument is passed - return null, otherwise bool
61
        return func_num_args() > 1 ? $return == null : $return;
62
    }
63
64
    /**
65
     * If this field has defined key=>value set.
66
     *
67
     * @return array|mixed Grouped collection of field key => value possible values or value for key passed.
68
     */
69
    public function options($key = null)
70
    {
71
        $types = array();
72
        // Convert possible field values to array
73
        foreach (explode(',', $this->Value) as $typeValue) {
74
            // Split view and value
75
            $typeValue = explode(':', $typeValue);
76
77
            // Store to key => value collection
78
            $types[$typeValue[0]] = $typeValue[1];
79
        }
80
81
        return isset($key) ? $types[$key] : $types;
82
    }
83
84
    /** @return string Get additional field value field name depending on its type */
85
    public function valueFieldName()
86
    {
87
        switch ($this->type) {
88
            case 7:
89
                return 'numeric_value';
90
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
91
            case 6:
92
                return 'key_value';
93
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
94
            default:
95
                return 'Value';
96
        }
97
    }
98
}
99