Table::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 09.12.15
6
 * Time: 09:57
7
 */
8
namespace samsoncms\api\field;
9
10
use samsoncms\api\CMS;
11
use samsoncms\api\Field;
12
use samsoncms\api\Material;
13
use samsoncms\api\MaterialField;
14
use samsoncms\api\query\FieldNavigation;
15
use samsoncms\api\query\MaterialNavigation;
16
use samsonframework\orm\Condition;
17
use samsonframework\orm\ConditionInterface;
18
use samsonframework\orm\QueryInterface;
19
20
/**
21
 * Material additional fields table.
22
 * @package samsoncms\api
23
 * @deprecated Use \samsoncms\api\query\EntityTable
24
 */
25
class Table
26
{
27
    /** @var array Collection of real row field names  */
28
    protected static $fieldsRealNames = array();
29
30
    /** @var integer Navigation identifier for table structure */
31
    protected $navigationID;
32
33
    /** @var integer Table parent material identifier */
34
    protected $materialID;
35
36
    /** @var Field[] Collection field instances that correspond table columns */
37
    protected $fields;
38
39
    /** @var QueryInterface Database query interface */
40
    protected $query;
41
42
    /** @var string Locale identifier */
43
    protected $locale;
44
45
    /** @var Row[] Fields table rows collection */
46
    protected $collection = array();
47
48
    /** @var string Row class name */
49
    protected $rowInstance = '\samsoncms\api\field\Row';
50
51
    /**
52
     * FieldsTable constructor.
53
     *
54
     * @param QueryInterface $query        Database query interface
55
     * @param int[]          $navigationID Navigation identifier for table structure
56
     * @param integer        $materialID   Table parent material identifier
57
     * @param string|null    $locale       Locale identifier
58
     */
59
    public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = null)
60
    {
61
        $this->query = $query;
62
        $this->navigationID = $navigationID;
0 ignored issues
show
Documentation Bug introduced by
It seems like $navigationID of type array<integer,integer> is incompatible with the declared type integer of property $navigationID.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        $this->materialID = $materialID;
64
        $this->locale = $locale;
65
66
        $this->find();
67
    }
68
69
    /**
70
     * Fill table with data from database.
71
     */
72
    protected function find()
73
    {
74
        // Get table Fields instances
75
        $this->fields = (new FieldNavigation())->byRelationID($this->navigationID);
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \samsoncms\api\quer...ID($this->navigationID) of type array<integer,*> is incompatible with the declared type array<integer,object<samsoncms\api\Field>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
77
        $collection = array();
78
        if (count($rowIDs = $this->rowIDs())) {
79
            /** @var MaterialField $fieldValue Get additional field value instances */
80
            foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY)
0 ignored issues
show
Bug introduced by
The expression $this->query->entity(\sa...$this->fields))->exec() of type boolean|array<integer,ob...k\orm\RecordInterface>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
81
                         // Get only needed rows(materials)
82
                         ->where(Material::F_PRIMARY, $rowIDs)
83
                         ->where(Material::F_DELETION, 1)
84
                         // Get correct localizes field condition for columns
85
                         ->whereCondition($this->fieldsCondition($this->fields))
86
                         ->exec() as $fieldValue) {
87
                /** @var Field $field Try to find Field instance by identifier */
88
                $field = &$this->fields[$fieldValue[Field::F_PRIMARY]];
89
                if (null !== $field) {
90
                    /**
91
                     * As we generate camelCase names for fields we need to store
92
                     * original names to get their values and correctly set row
93
                     * fields.
94
                     */
95
                    $fieldName = null !== static::$fieldsRealNames[$field->Name]
0 ignored issues
show
Bug introduced by
The property Name does not seem to exist in samsoncms\api\Field.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
96
                        ? static::$fieldsRealNames[$field->Name] : $field->Name;
97
                    /**
98
                     * Store table row(material) as it primary, store columns(Fields)
99
                     * by field primary. Use correct column for value.
100
                     */
101
                    $collection[$fieldValue[Material::F_PRIMARY]][$fieldName]
102
                        = $fieldValue[$field->valueFieldName()];
103
                }
104
            }
105
106
            /** @var Material[] $materials */
107
            $materials = $this->query->entity(Material::class)->where(Material::F_PRIMARY, array_keys($collection))->exec();
108
109
110
            // Go through collection again and created specific rows
111
            foreach ($collection as $materialID => $fields) {
112
                $this->collection[$materialID] = new $this->rowInstance($materialID, array_merge($fields, array('created' => $materials[$materialID]->Created, 'modified' => $materials[$materialID]->Modyfied)));
113
            }
114
        }
115
116
        return $this->collection;
117
    }
118
119
    /** @return array Collection of table rows(materials) identifiers */
120
    protected function rowIDs()
121
    {
122
        // Get collection of nested materials
123
        return $this->query
124
            ->entity(Material::class)
125
            ->where(Material::F_DELETION, 1)
126
            ->where(Material::F_PRIMARY, (new MaterialNavigation())->idsByRelationID($this->navigationID))
127
            ->where(Material::F_PARENT, $this->materialID)
128
            ->orderBy(Material::F_PRIORITY)
129
            ->fields(Material::F_PRIMARY);
130
    }
131
132
    /**
133
     * Build correct localized field request for retrieving additional fields records.
134
     *
135
     * @param Field[] $fields Collection of additional fields
136
     *
137
     * @return Condition Built condition for query
138
     */
139
    protected function fieldsCondition($fields)
140
    {
141
        // Group fields by localization
142
        $localizedColumns = array();
143
        $notLocalizedColumns = array();
144
        /** @var Field $field Iterate table columns(fields) */
145
        foreach ($fields as $field) {
146
            if ($field->localized()) {
147
                $localizedColumns[] = $field->id;
148
            } else {
149
                $notLocalizedColumns[] = $field->id;
150
            }
151
        }
152
153
        // Create field condition
154
        $fieldsCondition = new Condition(ConditionInterface::DISJUNCTION);
155
        // Create localized condition
156
        if (count($localizedColumns)) {
157
            $localizedCondition = new Condition(ConditionInterface::CONJUNCTION);
158
            $localizedCondition->add(Field::F_PRIMARY, $localizedColumns)
159
                ->add(MaterialField::F_LOCALE, $this->locale);
160
161
            // Add this condition to condition group
162
            $fieldsCondition->addCondition($localizedCondition);
163
        }
164
165
        // Create not localized condition
166
        if (count($notLocalizedColumns)) {
167
            $fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns);
168
        }
169
170
        return $fieldsCondition;
171
    }
172
173
    /** @return array Get field table column names collection */
174
    public function columns()
175
    {
176
        return array_column($this->fields, Field::F_IDENTIFIER);
177
    }
178
179
    /**
180
     * Get collection of table column values as array.
181
     *
182
     * @param string $fieldID Additional field identifier
183
     *
184
     * @return array Collection of table column values as array
185
     */
186
    public function values($fieldID)
187
    {
188
        return (null !== $this->fields[$fieldID]) ? array_column($this->collection, $fieldID) : array();
189
    }
190
191
    /**
192
     * Get field table as multidimensional array.
193
     *
194
     * @return Row[] Field table represented as array
195
     */
196
    public function toArray()
197
    {
198
        return $this->collection;
199
    }
200
}
201