Completed
Push — work-fleets ( d28adc...7496c9 )
by SuperNova.WS
06:01
created

Entity::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class Entity
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class Entity implements \Common\IMagicAccess, \Common\IEntity {
9
  const ENTITY_DB_ID_INCLUDE = true;
10
  const ENTITY_DB_ID_EXCLUDE = true;
11
12
  /**
13
   * Link to DB which used by this Entity
14
   *
15
   * @var db_mysql $dbStatic
16
   * deprecated - replace with container ID like 'db' or 'dbAuth'
17
   */
18
  protected static $dbStatic;
19
  /**
20
   * Name of table for this entity
21
   *
22
   * @var string $tableName
23
   */
24
  protected static $tableName = '_table';
25
  /**
26
   * Name of key field field in this table
27
   *
28
   * @var string $idField
29
   */
30
  protected static $idField = 'id';
31
32
  /**
33
   * Name of exception class that would be thrown
34
   *
35
   * Uses for calling when you don't know which exact exception should be called
36
   * On Entity's children should be used exception class name
37
   *
38
   * @var string $exceptionClass
39
   */
40
  protected static $exceptionClass = 'EntityException';
41
42
  /**
43
   * Container for property values
44
   *
45
   * @var \Common\IPropertyContainer $_container
46
   */
47
  protected $_container;
48
  protected static $_containerName = 'V2PropertyContainer';
49
50
  /**
51
   * Property list and description
52
   *
53
   * propertyName => array(
54
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
55
   * )
56
   *
57
   * @var array[]
58
   */
59
  protected static $_properties = array();
60
61
  /**
62
   * Service to work with rows
63
   *
64
   * @var \DbRowDirectOperator $rowOperator
65
   */
66
  protected static $rowOperator;
67
68
  /**
69
   * Entity constructor.
70
   *
71
   * @param \Common\GlobalContainer $gc
72
   */
73
  public function __construct($gc) {
74
    empty(static::$dbStatic) && !empty($gc->db) ? static::$dbStatic = $gc->db : false;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->db can also be of type object<Closure>. However, the property $dbStatic is declared as type object<db_mysql>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
    static::$rowOperator = $gc->dbRowOperator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->dbRowOperator can also be of type object<Closure>. However, the property $rowOperator is declared as type object<DbRowDirectOperator>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
77
    $this->_container = new static::$_containerName();
78
    $this->_container->setProperties(static::$_properties);
79
  }
80
81
  /**
82
   * @return array[]
83
   */
84
  public function getProperties() {
85
    return static::$_properties;
86
  }
87
88
  /**
89
   * Import DB row state into object properties
90
   *
91
   * @param array $row
92
   */
93
  public function importRow($row) {
94
    $this->_container->importRow($row);
95
  }
96
97
  /**
98
   * Export data from object properties into DB row for further use
99
   *
100
   * @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements
101
   *
102
   * @return array
103
   */
104
  protected function exportDbRow($withDbId = self::ENTITY_DB_ID_INCLUDE) {
105
    $row = $this->_container->exportRow();
106
107
    if ($withDbId == self::ENTITY_DB_ID_EXCLUDE) {
108
      unset($row[$this->getIdFieldName()]);
109
    }
110
111
    return $row;
112
  }
113
114
  /**
115
   * @return array
116
   */
117
  public function exportRowWithoutId() {
118
    return $this->exportDbRow(self::ENTITY_DB_ID_EXCLUDE);
119
  }
120
121
  /**
122
   * @return array
123
   */
124
  public function exportRowWithId() {
125
    return $this->exportDbRow(self::ENTITY_DB_ID_INCLUDE);
126
  }
127
128
  /**
129
   * Trying to load object info by buddy ID - if it is supplied
130
   *
131
   * @param int|float|string $dbId
132
   *
133
   * @return bool
134
   */
135
  protected function loadTry($dbId) {
136
    $this->dbId = $dbId;
137
    $row = static::$rowOperator->getById($this);
138
139
    if (empty($row)) {
140
      $this->dbId = 0;
141
142
      return false;
143
    } else {
144
      $this->importRow($row);
145
    }
146
147
    return true;
148
  }
149
150
  /**
151
   * Gets entity's table name
152
   *
153
   * @return string
154
   */
155
  public function getTableName() {
156
    return static::$tableName;
157
  }
158
159
  /**
160
   * @return string
161
   */
162
  public function getIdFieldName() {
163
    return static::$idField;
164
  }
165
166
  /**
167
   * @return bool
168
   */
169
  public function isContainerEmpty() {
170
    return $this->_container->isEmpty();
171
  }
172
173
  /**
174
   * @return bool
175
   */
176
  public function isNew() {
177
    return empty($this->dbId);
178
  }
179
180
  /**
181
   * @return db_mysql
182
   */
183
  public function getDbStatic() {
184
    return static::$dbStatic;
185
  }
186
187
  public function __get($name) {
188
    return $this->_container->$name;
189
  }
190
191
  public function __set($name, $value) {
192
    $this->_container->$name = $value;
193
  }
194
195
  public function __isset($name) {
196
    return isset($this->_container->$name);
197
  }
198
199
  public function __unset($name) {
200
    unset($this->_container->$name);
201
  }
202
203
}
204