Completed
Push — work-fleets ( 6cc592...094cef )
by SuperNova.WS
06:07
created

EntityContainer::importRow()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 4
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
3
/**
4
 * Class EntityContainer
5
 *
6
 * Introduces linked models and export/import operations
7
 *
8
 * Importer is a callable like
9
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
10
 *
11
 * Exporter is a callable like
12
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
13
 */
14
class EntityContainer extends ContainerAccessors {
15
  /**
16
   * Property list and description
17
   *
18
   * propertyName => array(
19
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
20
   * )
21
   *
22
   * @var array[] $properties
23
   */
24
  protected $properties = array();
25
26
27
  /**
28
   * Set properties data from external source
29
   *
30
   * @param array $properties
31
   */
32 1
  public function setProperties($properties) {
33 1
    $this->properties = $properties;
34 1
  }
35
36
  /**
37
   * @param array  $row
38
   * @param string $processor
39
   */
40 1
  protected function processRow(&$row, $processor) {
41
//var_dump($row);
42 1
    foreach ($this->properties as $propertyName => $propertyData) {
43 1
      $fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : '';
44
      if (
45 1
        !empty($this->accessors[$propertyName][$processor])
46 1
        &&
47 1
        is_callable($this->accessors[$propertyName][$processor])
48 1
      ) {
49 1
        call_user_func_array($this->accessors[$propertyName][$processor], array($this, &$row, $propertyName, $fieldName));
50 1
      } elseif ($fieldName) {
51 1
        if ($processor == P_CONTAINER_IMPORT) {
52 1
          $this->$propertyName = isset($row[$fieldName]) ? $row[$fieldName] : null;
53 1
        } else {
54 1
          $row[$fieldName] = $this->$propertyName;
55
        }
56 1
      }
57
      // Otherwise it's internal field - filled and used internally
58 1
    }
59
60 1
  }
61
62
  /**
63
   * Import DB row state into object properties
64
   *
65
   * @param array $row
66
   */
67 1
  public function importRow($row) {
68 1
    $this->clearProperties();
69
70 1
    if (is_array($row) && !empty($row)) {
71 1
      $this->processRow($row, P_CONTAINER_IMPORT);
72 1
    }
73
74 1
    return true;
75
  }
76
77
  /**
78
   * Exports object properties to DB row state WITHOUT ID
79
   *
80
   * Useful for INSERT operations
81
   *
82
   * @return array
83
   */
84
  public function exportRow() {
85
    $row = array();
86
    $this->processRow($row, P_CONTAINER_EXPORT);
87
88
    return $row;
89
  }
90
91
  /**
92
   * Clears only properties which declared in $properties array
93
   */
94 1
  public function clearProperties() {
95 1
    foreach ($this->properties as $propertyName => $propertyData) {
96 1
      unset($this->$propertyName);
97 1
    }
98 1
  }
99
100
}
101