Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1684 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;

use LeadersLinked\Model\Comment;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Insert;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Model\Feed;


class CommentMapper extends MapperCommon
{
    const _TABLE = 'tbl_comments';

    
    /**
     *
     * @var CommentMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @param LoggerInterface $logger
     * @param int $user_id
     * @return \LeadersLinked\Mapper\CommentMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new CommentMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     *
     * @param int $uuid
     * @return Comment
     */
    public function fetchOneByUuid($uuid)
    {
        $prototype = new Comment;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     * 
     * @param int $feed_id
     * @return int
     */
    public function fetchCountCommentByFeedId($feed_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)') ]);
        $select->where->equalTo('feed_id', $feed_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $feed_id
     * @return Comment[]
     */
    public function fetchAllByFeedId($feed_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('feed_id', $feed_id);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param int $feed_id
     * @return Comment[]
     */
    public function fetchAllPublishedByFeedId($feed_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('feed_id', $feed_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $id
     * @return Comment
     */
    public function fetchOne($id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     * 
     * @param Comment $comment
     * @return boolean
     */
    public function insert($comment)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($comment);
        $values = $this->removeEmpty($values);
        
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        if($result) {
           $comment->id = $this->getLastInsertId();
        }
        
        return $result;
        
    }
    
    
    /**
     *
     * @param Comment $comment
     * @return boolean
     */
    public function update($comment)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($comment);
        $values = $this->removeEmpty($values);
        
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $comment->id);
        
        

        
        return $this->executeUpdate($update);
    }
    
    
    /**
     *
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
       
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status' => Comment::STATUS_DELETED
        ]);
        $update->where->equalTo('id', $id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     * 
     * @param int $feed_id
     * @return boolean
     */
    public function deleteAllByFeedId($feed_id)
    {
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status' => Comment::STATUS_DELETED
        ]);
        $update->where->equalTo('feed_id', $feed_id);
        
        return $this->executeUpdate($update);
    }
}