Rev 4911 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Mapper;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Paginator\Paginator;
use LeadersLinked\Model\Country;
use Laminas\Hydrator\ObjectPropertyHydrator;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Adapter\DbSelect;
class CountryMapper extends MapperCommon
{
const _TABLE = 'tbl_countries';
/**
*
* @var CountryMapper
*/
private static $_instance;
/**
*
* @param AdapterInterface $adapter
*/
private function __construct($adapter)
{
parent::__construct($adapter);
}
/**
*
* @param AdapterInterface $adapter
* @return CountryMapper
*/
public static function getInstance($adapter)
{
if(self::$_instance == null) {
self::$_instance = new CountryMapper($adapter);
}
return self::$_instance;
}
/**
*
* @param String $code
* @return Country
*/
public function fetchOneByCode($code)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('code', $code);
$prototype = new Country();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param String $country
* @return Country
*/
public function fetchOneByCountry($country)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('country', $country);
$prototype = new Country();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param String $search
* @return Country
*/
public function fetchOneByCodeOrCountry($search)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('country', $search)->or->equalTo('code', $search);
$prototype = new Country();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @return Country[]
*/
public function fetchAll()
{
$select = $this->sql->select(self::_TABLE);
$prototype = new Country();
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param string $search
* @param int $page
* @param int $records_per_page
* @param string $order_field
* @param string $order_direction
* @return Paginator
*/
public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'country', $order_direction = 'ASC')
{
$prototype = new Country();
$select = $this->sql->select(self::_TABLE);
if($search) {
$select->where->like('country', '%' . $search . '%')->or->like('code', '%' . $search . '%');
}
$select->order($order_field . ' ' . $order_direction);
$hydrator = new ObjectPropertyHydrator();
$resultset = new HydratingResultSet($hydrator, $prototype);
$adapter = new DbSelect($select, $this->sql, $resultset);
$paginator = new Paginator($adapter);
$paginator->setItemCountPerPage($records_per_page);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
/**
*
* @param Country $country
* @return boolean
*/
public function insert($country)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($country);
$values = $this->removeEmpty($values);
$insert = $this->sql->insert(self::_TABLE);
$insert->values($values);
return $this->executeInsert($insert);
}
/**
*
* @param Country $country
* @return boolean
*/
public function update($country)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($country);
$values = $this->removeEmpty($values);
$update = $this->sql->update(self::_TABLE);
$update->set($values);
$update->where->equalTo('code', $country->code);
return $this->executeUpdate($update);
}
/**
*
* @param string $code
* @return boolean
*/
public function delete($code)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('code', $code);
return $this->executeDelete($delete);
}
}