From 230faaf3ccd1ed47c4781eacd4ffa79e47618bbd Mon Sep 17 00:00:00 2001 From: Aldarien Date: Thu, 8 Sep 2022 21:35:33 -0400 Subject: [PATCH] Abstracts --- src/Alias/Model.php | 64 ++++++++++++++++++ src/Alias/Model/Repository.php | 119 +++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 src/Alias/Model.php create mode 100644 src/Alias/Model/Repository.php diff --git a/src/Alias/Model.php b/src/Alias/Model.php new file mode 100644 index 0000000..b96b4c2 --- /dev/null +++ b/src/Alias/Model.php @@ -0,0 +1,64 @@ +factory = $factory; + return $this; + } + public function getFactory(): Factory + { + return $this->factory; + } + protected int $id; + public function setId(int $id): ModelInterface + { + $this->id = $id; + return $this; + } + public function getId(): int + { + return $this->id; + } + protected bool $new; + public function setNew(): ModelInterface + { + $this->new = true; + return $this; + } + public function isNew(): bool + { + return $this->new ?? false; + } + protected bool $dirty; + public function setDirty(): ModelInterface + { + $this->dirty = true; + return $this; + } + public function isDirty(): bool + { + return $this->dirty ?? $this->isNew(); + } + + public function save(): void + { + if ($this->isDirty()) { + $this->getFactory()->get(get_class($this))->save($this); + } + } + public function edit(array $data): void + { + foreach ($data as $key => $val) { + $m = 'set' . ucwords($key); + $this->{$m}($val); + } + $this->isDirty(); + } +} diff --git a/src/Alias/Model/Repository.php b/src/Alias/Model/Repository.php new file mode 100644 index 0000000..69767b3 --- /dev/null +++ b/src/Alias/Model/Repository.php @@ -0,0 +1,119 @@ +setConnection($connection) + ->setQueryBuilder($builder) + ->setFactory($factory) + ->setup(); + } + + protected Connection $connection; + public function setConnection(Connection $connection): Repository + { + $this->connection = $connection; + return $this; + } + public function getConnection(): Connection + { + return $this->connection; + } + protected QueryBuilder $builder; + public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface + { + $this->builder = $builder; + return $this; + } + public function getQueryBuilder(): QueryBuilder + { + return $this->builder; + } + protected Factory $factory; + public function setFactory(Factory $factory): RepositoryInterface + { + $this->factory = $factory; + return $this; + } + public function getFactory(): Factory + { + return $this->factory; + } + protected string $table; + public function setTable(string $table): RepositoryInterface + { + $this->table = $table; + return $this; + } + public function getTable(): string + { + return $this->table; + } + protected array $columns; + public function setColumns(array $columns): RepositoryInterface + { + foreach ($columns as $column) { + $this->addColumn($column); + } + return $this; + } + public function addColumn(string $column): RepositoryInterface + { + $this->columns []= $column; + return $this; + } + public function getColumns(): array + { + return $this->columns; + } + + public function save(Model $model): void + { + if (!$model->isDirty() and !$model->isNew()) { + return; + } + $cols = []; + $values = []; + foreach ($this->getColumns() as $column) { + $m = 'get' . ucwords($column); + $cols []= '?'; + $values []= $model->{$m}(); + } + $query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols); + $this->getConnection()->execute($query, $values); + } + public function edit(Model $model, array $data): Model + { + foreach ($this->getColumns() as $col) { + if (isset($data[$col])) { + $m = 'set' . ucwords($col); + $model->{$m}($data[$col]); + } + } + return $model; + } + public function fetchById(int $id): Model + { + $query = $this->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where([['id', '?']]) + ->limit(1); + return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]); + } + public function fetchAll(): array + { + $query = $this->getQueryBuilder() + ->select() + ->from($this->getTable()); + return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray()); + } +}