Completed
Push — work-fleets ( bd15ac...5c3a01 )
by SuperNova.WS
06:16
created

EntityContainer::clearProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
3
use \Common\GlobalContainer;
4
5
/**
6
 * Class EntityContainer
7
 *
8
 * Introduces linked models and export/import operations
9
 *
10
 * Importer is a callable like
11
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
12
 *
13
 * Exporter is a callable like
14
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
15
 *
16
 * @property int|float $dbId Entity DB ID
17
 */
18
class EntityContainer extends ContainerAccessors implements IEntityContainer {
19
  protected static $exceptionClass = 'EntityException';
20
21
  /**
22
   * Property list and description
23
   *
24
   * propertyName => array(
25
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
26
   * )
27
   *
28
   * @var array[] $properties
29
   */
30
  protected $properties = array();
31
32
33
//  /**
34
//   * @var  \Common\GlobalContainer $gc
35
//   */
36
//  protected $gc;
37
38
39
  /**
40
   * BuddyContainer constructor.
41
   *
42
   */
43
  public function __construct() {
44
    // TODO - remove. No dependenceon container - we should extract all needed info here
45
//    $this->gc = $gc;
46
//    $this->model = new static::$modelClass($gc);
47
//    static::$dbStatic = $gc->db;
48
//    static::$rowOperator = $gc->dbRowOperator;
49
  }
50
51
  public function setProperties($properties) {
52
    $this->properties = $properties;
53
  }
54
55
  /**
56
   * @param array  $row
57
   * @param string $processor
58
   */
59
  protected function processRow(&$row, $processor) {
60
    foreach ($this->properties as $propertyName => $propertyData) {
61
      $fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : '';
62
      if (
63
        !empty($this->accessors[$propertyName][$processor])
64
        &&
65
        is_callable($this->accessors[$propertyName][$processor])
66
      ) {
67
        call_user_func_array($this->accessors[$propertyName][$processor], array($this, &$row, $propertyName, $fieldName));
68
      } elseif ($fieldName) {
69
        if($processor == P_CONTAINER_IMPORT) {
70
          $this->$propertyName = $row[$fieldName];
71
        } else {
72
          $row[$fieldName] = $this->$propertyName;
73
        }
74
      }
75
      // Otherwise it's internal field - filled and used internally
76
    }
77
78
  }
79
80
  public function importRow($row) {
81
    $this->clearProperties();
82
83
    if (empty($row)) {
84
      return true;
85
    }
86
87
    $this->processRow($row, P_CONTAINER_IMPORT);
88
89
    return true;
90
  }
91
92
  /**
93
   * @return array
94
   */
95
  public function exportRow() {
96
    $row = array();
97
    $this->processRow($row, P_CONTAINER_EXPORT);
98
99
    return $row;
100
  }
101
102
  public function isEmpty() {
103
    // TODO - empty container - only properties
104
    return empty($this->dbId);
105
  }
106
107
  public function isNew() {
108
    return empty($this->dbId);
109
  }
110
111
  public function clearProperties() {
112
    foreach ($this->properties as $propertyName => $propertyData) {
113
      unset($this->$propertyName);
114
    }
115
  }
116
117
}
118