1. Go to this page and download the library: Download dimaxz/repository library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dimaxz / repository example snippets
$userRepository = new UserRepository;
$criteria = new UserCriteria();
$criteria->setFilterById(1);
$user = $userRepository->findByCriteria($criteria)->current();
//или
$user = $userRepository->findById(1);
$userRepository = new UserRepository;
$criteria = new UserCriteria();
$criteria->setFilterByName("Vova");
$users = $userRepository->findByCriteria($criteria);
class UserRepository implements CrudRepositoryInterface
{
protected $data = [
[
"id" => 1,
"name" => "Vova"
],
[
"id" => 2,
"name" => "Petia"
]
];
public function findByCriteria(RepositoryCriteriaInterface $criteria): CollectionInterface{
$collection = new UserCollection;
foreach($this->data as $row){
$user = (new User)->setId($row["id"])->setName($row["name"]);
//фильтруем по id
if($criteria->getFilterByid()!==null && $criteria->getFilterByid() != $user->getId()){
continue;
}
$collection->add($user);
}
return $collection;
}
public function findById(int $id) :? EntityInterface{
$criteria = new UserCriteria();
$criteria->setFilterById($id);
$user = $this->findByCriteria($criteria)->current();
return is_object($user) ? $user : null;
}
}
class User implements EntityInterface{
protected $id;
protected $name;
public function getId():?int{
return $this->id;
}
public function getName():?string{
return $this->name;
}
public function setId(?int $id){
$this->id = $id;
return $this;
}
public function setName(?string $name){
$this->id = $name;
return $this;
}
}
class UserCollection implements CollectionInterface{
public function getClassName():string{
return User::class;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.