Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1295 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;


use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
use Laminas\Log\LoggerInterface;
use Laminas\Hydrator\ArraySerializableHydrator;


use LeadersLinked\Model\CompanyLocation;
use LeadersLinked\Mapper\Common\MapperCommon;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;

class CompanyLocationMapper extends MapperCommon
{
    const _TABLE = 'tbl_company_locations';

    
    /**
     *
     * @var CompanyLocationMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return CompanyLocationMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new CompanyLocationMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @param int $company_id
     * @return array
     */
    public function fetchAllLocationByCompanyId($company_id)
    {
        $select = $this->sql->select();
        $select->columns(['formatted_address', 'country']);
        $select->from(['l' => LocationMapper::_TABLE]);
        $select->join(['cl' => self::_TABLE], 'l.id = cl.location_id', ['company_location_id' => 'id', 'company_location_uuid' => 'uuid', 'location_id', 'is_main']);
        $select->where->equalTo('company_id', $company_id);
        $select->order('is_main, formatted_address');
        
        //echo $select->getSqlString($this->adapter->platform);
        
        return $this->executeFetchAllArray($select);
        
    }
    
    /**
     * 
     * @param int $company_id
     * @return CompanyLocation[]
     */
    public function fetchAllByCompanyId($company_id)
    {
        $prototype = new CompanyLocation();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        
        return $this->executeFetchAllObject($select, $prototype);
        
    }
    
    /**
     * 
     * @param int $id
     * @return CompanyLocation
     */
    public function fetchOne($id)
    {
        $prototype = new CompanyLocation();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
        
        
    }
    
    /**
     *
     * @param string $uuid
     * @return CompanyLocation
     */
    public function fetchOneByUuid($uuid)
    {
        $prototype = new CompanyLocation();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
        
        
    }
    
    /**
     *
     * @param int $company_id
     * @return CompanyLocation
     */
    public function fetchOneMainLocationByCompanyId($company_id)
    {
        $prototype = new CompanyLocation();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('is_main', CompanyLocation::IS_MAIN_YES);
        
        return $this->executeFetchOneObject($select, $prototype);
    }

    /**
     * 
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $id);

        return $this->executeDelete($delete);
    }
    
    /**
     * 
     * @param CompanyLocation $companyLocation
     * @return true
     */
    public function insert($companyLocation)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($companyLocation);
        $values = $this->removeEmpty($values);
        

        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        if($result) {
            $companyLocation->id = $this->lastInsertId;
        }
        return $result;
        
    }
    
    /**
     *
     * @param CompanyLocation $companyLocation
     * @return true
     */
    public function update($companyLocation)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($companyLocation);
        $values = $this->removeEmpty($values);
        
        $update = $this->sql->update(self::_TABLE);
        $update->where->equalTo('id', $companyLocation->id);
        $update->set($values);
        
        return $this->executeUpdate($update);

    }

}