Passed
Push — main ( 207c72...2c3242 )
by Sammy
01:26
created

TableToModel::to_table_row()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace HexMakina\TightORM;
4
5
use \HexMakina\Crudites\Crudites;
6
use \HexMakina\Crudites\Interfaces\TableManipulationInterface;
7
8
abstract class TableToModel extends Crudites
9
{
10
    //check all primary keys are set (TODO that doesn't work unles AIPK.. nice try)
11
    public function is_new() : bool
12
    {
13
        $match = static::table()->primary_keys_match(get_object_vars($this));
14
        return empty($match);
15
    }
16
17
    public function get_id($mode = null)
18
    {
19
        $primary_key = static::table()->auto_incremented_primary_key();
20
        if (is_null($primary_key) && count($pks = static::table()->primary_keys())==1) {
21
            $primary_key = current($pks);
22
        }
23
24
        return $mode === 'name' ? $primary_key->name() : $this->get($primary_key->name());
25
    }
26
27
    public function get($prop_name)
28
    {
29
        if (property_exists($this, $prop_name) === true) {
30
            return $this->$prop_name;
31
        }
32
33
        return null;
34
    }
35
36
    public function set($prop_name, $value)
37
    {
38
        $this->$prop_name = $value;
39
    }
40
41
    public function import($assoc_data)
42
    {
43
        if (!is_array($assoc_data)) {
44
            throw new \Exception(__FUNCTION__.'(assoc_data) parm is not an array');
45
        }
46
47
        // shove it all up in model, god will sort them out
48
        foreach ($assoc_data as $field => $value) {
49
            $this->set($field, $value);
50
        }
51
52
        return $this;
53
    }
54
55
    public static function table() : TableManipulationInterface
56
    {
57
        $table = static::table_name();
58
        $table = self::inspect($table);
59
60
        return $table;
61
    }
62
63
    public static function table_name() : string
64
    {
65
        $reflect = new \ReflectionClass(get_called_class());
66
67
        $table_name = $reflect->getConstant('TABLE_NAME');
68
69
        if ($table_name === false) {
70
            $calling_class = $reflect->getShortName();
71
            if (defined($const_name = 'TABLE_'.strtoupper($calling_class))) {
72
                $table_name = constant($const_name);
73
            } else {
74
                $table_name = strtolower($calling_class);
75
            }
76
        }
77
78
        return $table_name;
79
    }
80
81
82
    public function to_table_row($operator_id = null)
83
    {
84
        if (!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) {
85
            $this->set('created_by', $operator_id);
86
        }
87
88
        $model_data = get_object_vars($this);
89
90
        // 1. Produce OR restore a row
91
        if ($this->is_new()) {
92
            $table_row = static::table()->produce($model_data);
93
        } else {
94
            $table_row = static::table()->restore($model_data);
95
        }
96
97
        // 2. Apply alterations from form_model data
98
        $table_row->alter($model_data);
99
100
        return $table_row;
101
    }
102
}
103