Completed
Push — master ( 2497fd...24ae5a )
by Stefano
02:23
created

Model::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Model class
5
 *
6
 * Base class for an ORM.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
abstract class Model implements JsonSerializable {
14
    use Module, Persistence, Events, Relation;
15
16
    public static function where($where_sql = false, $params = [], $flush = false){
0 ignored issues
show
Unused Code introduced by
The parameter $flush is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
      $key   = static::persistenceOptions('key');
18
19
      return SQL::reduce("SELECT {$key} FROM " . static::persistenceOptions('table') . ($where_sql ? " where {$where_sql}" : ''), $params, function($results, $row) use ($key) {
20
           $results[] = static::load($row->{$key});
21
           return $results;
22
      }, []);
23
    }
24
25
    public static function count($where_sql = false, $params = []) {
26
      return (int) SQL::value('SELECT COUNT(1) FROM ' . static::persistenceOptions('table') . ($where_sql ? " where {$where_sql}" : ''), $params);
27
    }
28
29
    public static function all($page=1, $limit=-1){
30
      return static::where($limit < 1 ? "" : "1 limit {$limit} offset " . (max(1,$page)-1)*$limit);
0 ignored issues
show
Documentation introduced by
$limit < 1 ? '' : "1 lim...1, $page) - 1) * $limit is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
    }
32
33
    public function primaryKey(){
34
      $key  = static::persistenceOptions('key');
35
      return $this->{$key};
36
    }
37
38
    public static function create($data){
39
      $tmp = new static;
40
      $data = (object)$data;
41
      foreach (array_keys(get_object_vars($tmp)) as $key) {
42
         if (isset($data->{$key})) $tmp->{$key} = $data->{$key};
43
      }
44
      $tmp->save();
45
      static::trigger('create',$tmp,$data);
46
      return $tmp;
47
    }
48
49
    public function export($transformer=null, $disabled_relations=[]){
50
      $data = [];
51
      if (!is_callable($transformer)) $transformer = function($k,$v){ return [$k=>$v]; };
52
      foreach (get_object_vars($this) as $key => $value) {
53 View Code Duplication
        if ($res = $transformer($key, $value)){
54
          $data[key($res)] = current($res);
55
        }
56
      }
57
58
      foreach (static::relationOptions()->links as $hash => $link) {
59
        $relation = $link->method;
60
        // Expand relations but protect from self-references loop
61
        if (isset($disabled_relations[$hash])) continue;
62
        $disabled_relations[$hash] = true;
63
        $value = $this->$relation;
64
        if ($value && is_array($value))
65
          foreach ($value as &$val) $val = $val->export(null,$disabled_relations);
66
        else
67
          $value = $value ? $value->export(null,$disabled_relations) : false;
68
        unset($disabled_relations[$hash]);
69
70 View Code Duplication
        if ($res = $transformer($relation, $value)){
71
          $data[key($res)] = current($res);
72
        }
73
74
      }
75
      return $data;
76
    }
77
78
    public function jsonSerialize() {
79
      return $this->export();
80
    }
81
82
}
83