12 Commits

Author SHA1 Message Date
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
022ba575b7 Merge branch 'develop' into master 2021-06-04 21:15:26 -04:00
643c3e714f FIX: Find where in add 2021-05-23 21:27:13 -04:00
db2864395c FIX: Factory for find 2021-05-23 21:07:48 -04:00
1cd06d5fe6 Add and Edit Model 2021-05-23 21:03:10 -04:00
c913f65b91 Considera leer el dato y correccion en los nombres de los metodos 2021-03-16 00:22:13 -03:00
244d8cc414 FIX: null cases & method name 2021-03-16 00:19:51 -03:00
c8a7781c88 Merge branch 'develop' 2021-03-15 11:17:11 -03:00
630c971b45 Date traits 2021-03-15 11:14:10 -03:00
1505539e61 Merge branch 'develop' into master 2021-03-09 00:19:55 -03:00
c441d41a02 FIX: Not through enough 2021-03-09 00:19:32 -03:00
7 changed files with 75 additions and 3 deletions

View File

@ -2,7 +2,7 @@
namespace ProVM\Common\Alias;
use \Model as BaseModel;
use ProVM\Common\Alias\Model as ModelInterface;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Factory\Model as ModelFactory;
abstract class Model extends BaseModel implements ModelInterface {
@ -122,4 +122,34 @@ abstract class Model extends BaseModel implements ModelInterface {
public function toArray(): array {
return $this->asArray();
}
protected static function parseInput($input): array {
return array_intersect_key((array) $input, array_combine(static::$fields, static::$fields));
}
public static function add(ModelFactory $factory, $input): ?ModelInterface {
$data = static::parseInput($input);
$class = get_called_class();
if (method_exists($class, 'find')) {
$obj = static::find($factory, $input);
} else {
$where = $data;
$where = array_values(array_walk($where, function(&$item, $key) {
$item = [$key, $item];
}));
$obj = $factory->find($class)->where($where)->one();
}
if ($obj === null) {
$obj = $factory->create($class, $data);
}
return $obj;
}
public function edit($input): bool {
$data = static::parseInput($input);
foreach (static::$fields as $field) {
if ($this->{$field} != $data[$field]) {
$this->{$field} = $data[$field];
}
}
return $this->save();
}
}

View File

@ -11,4 +11,7 @@ interface Model {
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array;
public function toArray(): array;
public static function add(ModelFactory $factory, $input): ?Model;
public function edit($input): bool;
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Date {
public function date(\DateTime $date = null) {
if ($date === null) {
return Carbon::parse($this->date);
}
$this->date = $data->format('Y-m-d');
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait DateTime {
public function dateTime(\DateTime $date_time = null) {
if ($date_time === null) {
return Carbon::parse($this->date_time);
}
$this->date_time = $date_time->format('c');
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Time {
public function time(\DateTime $time = null) {
if ($time === null) {
return Carbon::parse($this->time);
}
$this->time = $time->format('H:i:s e');
}
}

View File

@ -3,7 +3,7 @@ namespace ProVM\Common\Factory;
use ORM;
use Model as BaseModel;
use ProVM\Common\Alias\Model as ModelInterface;
use ProVM\Common\Define\Model as ModelInterface;
class Model {
public function reset(): Model {

View File

@ -1,6 +1,6 @@
<?php
namespace ProVM\Common\Form;
use ProVM\Common\Define\Model as BaseModel;
use ProVM\Common\Alias\Model as BaseModel;
abstract class Model extends BaseModel {}