4 Commits

Author SHA1 Message Date
65c224c636 Merge branch 'develop' into master 2021-08-01 20:51:44 -04:00
2bf938a9b7 FIX: array_walk returns bool and tertiary operator 2021-08-01 20:50:42 -04:00
6cd26a88ea Merge branch 'develop' into master 2021-08-01 20:34:04 -04:00
293b5af3ff FIX: self -> static 2021-08-01 20:31:49 -04:00
3 changed files with 20 additions and 20 deletions

View File

@ -124,36 +124,33 @@ abstract class Model extends BaseModel implements ModelInterface {
}
protected static function parseInput($input): array {
return array_intersect_key((array) $input, array_combine(self::$fields, self::$fields));
return array_intersect_key((array) $input, array_combine(static::$fields, static::$fields));
}
public static function add(ModelFactory $factory, $input): bool {
$data = self::parseInput($input);
public static function add(ModelFactory $factory, $input): ?ModelInterface {
$data = static::parseInput($input);
$class = get_called_class();
if (method_exists($class, 'find')) {
$obj = self::find($factory, $input);
$obj = static::find($factory, $input);
} else {
$where = $data;
$where = array_values(array_walk($where, function(&$item, $key) {
array_walk($where, function(&$item, $key) {
$item = [$key, $item];
}));
});
$where = array_values($where);
$obj = $factory->find($class)->where($where)->one();
}
if ($obj === false) {
if ($obj === null) {
$obj = $factory->create($class, $data);
return $obj->save();
}
return false;
return $obj;
}
public function edit($input): bool {
$data = self::parseInput($input);
foreach (self::$fields as $field) {
$data = static::parseInput($input);
foreach (static::$fields as $field) {
if ($this->{$field} != $data[$field]) {
$this->{$field} = $data[$field];
}
}
if ($this->isDirty()) {
return $this->save();
}
return false;
return $this->save();
}
}

View File

@ -12,6 +12,6 @@ interface Model {
public function toArray(): array;
public static function add(ModelFactory $factory, $input): bool;
public static function add(ModelFactory $factory, $input): ?Model;
public function edit($input): bool;
}

View File

@ -81,7 +81,7 @@ class Model {
'to' => $j['to'] ?? $j[2],
'sym' => $j['sym'] ?? ($j[3] ?? '='),
'alias' => $j['alias'] ?? '',
'type' => strtolower($j['type']) ?? '',
'type' => strtolower($j['type'] ?? ''),
'params' => $j['params'] ?? ''
];
$this->joins []= $join;
@ -98,7 +98,7 @@ class Model {
'column' => $c['column'] ?? $c[0],
'value' => $c['value'] ?? $c[1],
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
'type' => strtolower($c['type']) ?? ''
'type' => strtolower($c['type'] ?? '')
];
$this->conditions []= $cond;
}
@ -363,10 +363,13 @@ class Model {
return $results;
}
public function array(): ?array {
$results = $this->build()->findArray();
if (!$results) {
$results = $this->many();
if (!$results or $results === null) {
return null;
}
array_walk($results, function(&$item) {
$item = $item->toArray();
});
return $results;
}