Last active
April 3, 2021 16:19
-
-
Save jcroot/567a964f319e4b9ae1728dcac7292809 to your computer and use it in GitHub Desktop.
Clase Abstracta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Database implements DatabaseCRUD { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface DatabaseCRUD | |
{ | |
public function fetchAll($where = null, $order = null, $limit = null); | |
public function find($id); | |
public function save(DatabaseModel $model, $params); | |
} | |
interface DatabaseModel { | |
public function insert($params); | |
public function update($id, $params); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Main extends Database | |
{ | |
public function saveUser() | |
{ | |
$params = [ | |
'id' => 1, | |
'name' => 'Fred', | |
'address' => 'Rocky Hard 112' | |
]; | |
$this->save(new User(), $params); | |
} | |
public function saveProduct() | |
{ | |
$params = [ | |
'id' => 1, | |
'brand' => 'Pepsico', | |
'name' => 'Lays' | |
]; | |
$this->save(new Product(), $params); | |
} | |
public function fetchAll($where = null, $order = null, $limit = null) | |
{ | |
// TODO: Implement fetchAll() method. | |
} | |
public function find($id) | |
{ | |
// TODO: Implement find() method. | |
} | |
public function save(DatabaseModel $model, $params) | |
{ | |
// TODO: Implement save() method. | |
if ($model->getId()){ | |
$model->update($model->getId(), $params); | |
}else{ | |
$model->insert($params); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Product implements DatabaseModel | |
{ | |
protected $id; | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setId($id) | |
{ | |
$this->id = $id; | |
} | |
public function insert($params) | |
{ | |
// TODO: Implement insert() method. | |
return $this->getDbTable->insert($params); | |
} | |
public function update($id, $params) | |
{ | |
// TODO: Implement update() method. | |
return $this->getDbTable()->update($id, $params); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User implements DatabaseModel | |
{ | |
protected $id; | |
protected $name; | |
protected $address; | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getAddress() | |
{ | |
return $this->address; | |
} | |
public function setId($id) | |
{ | |
$this->id = $id; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
public function setAddress($address) | |
{ | |
$this->address = $address; | |
} | |
public function insert($params) | |
{ | |
// TODO: Implement insert() method. | |
return $this->getDbTable->insert($params); | |
} | |
public function update($id, $params) | |
{ | |
// TODO: Implement update() method. | |
return $this->getDbTable()->update($id, $params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment