6 Commits

3 changed files with 25 additions and 25 deletions

View File

@ -124,36 +124,33 @@ 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) { array_walk($where, function(&$item, $key) {
$item = [$key, $item]; $item = [$key, $item];
})); });
$where = array_values($where);
$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;
} }
} }

View File

@ -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;
} }

View File

@ -79,9 +79,9 @@ class Model {
'table' => $j['table'] ?? $j[0], 'table' => $j['table'] ?? $j[0],
'from' => $j['from'] ?? $j[1], 'from' => $j['from'] ?? $j[1],
'to' => $j['to'] ?? $j[2], 'to' => $j['to'] ?? $j[2],
'sym' => $j['sym'] ?? ($j[3] ?? '='), 'symb' => $j['symb'] ?? ($j[3] ?? '='),
'alias' => $j['alias'] ?? '', 'alias' => $j['alias'] ?? '',
'type' => strtolower($j['type']) ?? '', 'type' => strtolower($j['type'] ?? ''),
'params' => $j['params'] ?? '' 'params' => $j['params'] ?? ''
]; ];
$this->joins []= $join; $this->joins []= $join;
@ -97,8 +97,8 @@ class Model {
$cond = (object) [ $cond = (object) [
'column' => $c['column'] ?? $c[0], 'column' => $c['column'] ?? $c[0],
'value' => $c['value'] ?? $c[1], 'value' => $c['value'] ?? $c[1],
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')), 'symb' => strtolower($c['symb'] ?? ($c[2] ?? '=')),
'type' => strtolower($c['type']) ?? '' 'type' => strtolower($c['type'] ?? '')
]; ];
$this->conditions []= $cond; $this->conditions []= $cond;
} }
@ -154,7 +154,7 @@ class Model {
return $orm; return $orm;
} }
foreach ($this->columns as $col) { foreach ($this->columns as $col) {
$orm = $orm->select(trim(implode('.', $col), '.')); $orm = $orm->select(trim(implode('.', (array) $col), '.'));
} }
return $orm; return $orm;
} }
@ -192,7 +192,7 @@ class Model {
} }
if ($join->type == 'raw') { if ($join->type == 'raw') {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params); $orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
} elseif ($join->alias === '') { } elseif ($join->alias !== '') {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias); $orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
} else { } else {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]); $orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
@ -206,7 +206,7 @@ class Model {
} }
foreach ($this->conditions as $cond) { foreach ($this->conditions as $cond) {
$method = 'where'; $method = 'where';
switch ($cond->sym) { switch ($cond->symb) {
case '<': case '<':
$method = 'whereLt'; $method = 'whereLt';
break; break;
@ -363,10 +363,13 @@ class Model {
return $results; return $results;
} }
public function array(): ?array { public function array(): ?array {
$results = $this->build()->findArray(); $results = $this->many();
if (!$results) { if (!$results or $results === null) {
return null; return null;
} }
array_walk($results, function(&$item) {
$item = $item->toArray();
});
return $results; return $results;
} }