Merge branch 'develop' into master
This commit is contained in:
@ -124,13 +124,13 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static function parseInput($input): array {
|
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 {
|
public static function add(ModelFactory $factory, $input): ?ModelInterface {
|
||||||
$data = self::parseInput($input);
|
$data = static::parseInput($input);
|
||||||
$class = get_called_class();
|
$class = get_called_class();
|
||||||
if (method_exists($class, 'find')) {
|
if (method_exists($class, 'find')) {
|
||||||
$obj = self::find($factory, $input);
|
$obj = static::find($factory, $input);
|
||||||
} else {
|
} else {
|
||||||
$where = $data;
|
$where = $data;
|
||||||
$where = array_values(array_walk($where, function(&$item, $key) {
|
$where = array_values(array_walk($where, function(&$item, $key) {
|
||||||
@ -138,22 +138,18 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
}));
|
}));
|
||||||
$obj = $factory->find($class)->where($where)->one();
|
$obj = $factory->find($class)->where($where)->one();
|
||||||
}
|
}
|
||||||
if ($obj === false) {
|
if ($obj === null) {
|
||||||
$obj = $factory->create($class, $data);
|
$obj = $factory->create($class, $data);
|
||||||
return $obj->save();
|
|
||||||
}
|
}
|
||||||
return false;
|
return $obj;
|
||||||
}
|
}
|
||||||
public function edit($input): bool {
|
public function edit($input): bool {
|
||||||
$data = self::parseInput($input);
|
$data = static::parseInput($input);
|
||||||
foreach (self::$fields as $field) {
|
foreach (static::$fields as $field) {
|
||||||
if ($this->{$field} != $data[$field]) {
|
if ($this->{$field} != $data[$field]) {
|
||||||
$this->{$field} = $data[$field];
|
$this->{$field} = $data[$field];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($this->isDirty()) {
|
return $this->save();
|
||||||
return $this->save();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,6 @@ interface Model {
|
|||||||
|
|
||||||
public function toArray(): array;
|
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;
|
public function edit($input): bool;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user