<commit_before><?php

/**
 * This model contains all methods for interacting with user meta tables
 *
 * @package     Nails
 * @subpackage  module-auth
 * @category    Model
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Auth\Model\User;

use Nails\Factory;

class Meta
{
    use \Nails\Common\Traits\Caching;

    // --------------------------------------------------------------------------

    /**
     * The Database service
     * @var \Nails\Common\Database
     */
    private $oDb;

    // --------------------------------------------------------------------------

    /**
     * Construct the model
     */
    public function __construct()
    {
        $this->oDb = Factory::service('Database');
    }

    // --------------------------------------------------------------------------

    /**
     * Fetches a record from a user_meta_* table
     * @param  string   $sTable   The table to fetch from
     * @param  integer  $iUserId  The ID of the user the record belongs to
     * @param  array    $aColumns Any specific columns to select
     * @return \stdClass
     */
    public function get($sTable, $iUserId, $aColumns = array())
    {
        //  Check cache
        $sCacheKey = 'user-meta-' . $sTable . '-' . $iUserId;
        $oCache = $this->getCache($sCacheKey);

        if (!empty($oCache)) {

            return $oCache;
        }

        if (!empty($aColumns)) {

            $this->oDb->select($aColumns);
        }

        $this->oDb->where('user_id', $iUserId);
        $aResult = $this->oDb->get($sTable)->result();

        if (empty($aResult)) {

            $mOut = null;

        } else {

            $mOut = $aResult[0];
        }

        $this->setCache($sCacheKey, $mOut);

        return $mOut;
    }

    // --------------------------------------------------------------------------

    /**
     * Fetches records from a user_meta_table which may have many rows
     * @param  string   $sTable   The table to fetch from
     * @param  integer  $iUserId  The ID of the user the records belongs to
     * @param  array    $aColumns Any specific columns to select
     * @return array
     */
    public function getMany($sTable, $iUserId, $aColumns = array())
    {
        //  Check cache
        $sCacheKey = 'user-meta-many-' . $sTable . '-' . $iUserId;
        $oCache = $this->getCache($sCacheKey);

        if (!empty($oCache)) {

            return $oCache;
        }

        if (!empty($aColumns)) {

            $this->oDb->select($aColumns);
        }

        $this->oDb->where('user_id', $iUserId);
        $aResult = $this->oDb->get($sTable)->result();

        $this->setCache($sCacheKey, $aResult);

        return $aResult;
    }

    // --------------------------------------------------------------------------

    /**
     * Updates a user_meta_* table
     * @param  string  $sTable  The table to update
     * @param  integer $iUserId The ID of the user the record belongs to
     * @param  array   $aData   The data to set
     * @return boolean
     */
    public function update($sTable, $iUserId, $aData)
    {
        //  Safety: Ensure that the user_id is not overridden
        $aData['user_id'] = $iUserId;

        $this->oDb->where('user_id', $iUserId);
        if ($this->oDb->count_all_results($sTable)) {

            $this->oDb->set($aData);
            $this->oDb->where('user_id', $iUserId);
            $bResult = $this->oDb->update($sTable);

        } else {

            $this->oDb->set($aData);
            $bResult = $this->oDb->insert($sTable);
        }

        if ($bResult) {

            $this->unsetCache('user-meta-' . $sTable . '-' . $iUserId);
        }

        return $bResult;
    }

    // --------------------------------------------------------------------------

    /**
     * Updates a user_meta_* table
     * @param  string  $sTable  The table to update
     * @param  integer $iUserId The ID of the user ID being updated
     * @param  array   $aData   An array of rows to update
     * @return boolean
     */
    public function updateMany($sTable, $iUserId, $aData)
    {
        $this->oDb->trans_begin();
        foreach ($aData as $aRow) {

            if (empty($aRow['id'])) {

                //  Safety: Don't allow setting of the row ID
                unset($aRow['id']);
                //  Safety: overrwrite any user_id which may be passed
                $aRow['user_id'] = $iUserId;
                $this->oDb->set($aRow);
                if (!$this->oDb->insert($sTable)) {
                    $this->oDb->trans_rollback();
                    return false;
                }

            } else {

                //  Safety: Ensure that the row ID, and User ID are not overridden
                unset($aRow['id']);
                unset($aRow['user_id']);

                $this->oDb->set($aRow);
                if (!$this->oDb->update($sTable)) {
                    $this->oDb->trans_rollback();
                    return false;
                }
            }
        }

        $this->oDb->trans_commit();
        $this->unsetCache('user-meta-many-' . $sTable . '-' . $iUserId);
        return true;
    }
}
<commit_msg>Added `delete()` and `deleteMany()` methods<commit_after><?php

/**
 * This model contains all methods for interacting with user meta tables
 *
 * @package     Nails
 * @subpackage  module-auth
 * @category    Model
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Auth\Model\User;

use Nails\Factory;

class Meta
{
    use \Nails\Common\Traits\Caching;

    // --------------------------------------------------------------------------

    /**
     * The Database service
     * @var \Nails\Common\Database
     */
    private $oDb;

    // --------------------------------------------------------------------------

    /**
     * Construct the model
     */
    public function __construct()
    {
        $this->oDb = Factory::service('Database');
    }

    // --------------------------------------------------------------------------

    /**
     * Fetches a record from a user_meta_* table
     * @param  string   $sTable   The table to fetch from
     * @param  integer  $iUserId  The ID of the user the record belongs to
     * @param  array    $aColumns Any specific columns to select
     * @return \stdClass
     */
    public function get($sTable, $iUserId, $aColumns = array())
    {
        //  Check cache
        $sCacheKey = 'user-meta-' . $sTable . '-' . $iUserId;
        $oCache = $this->getCache($sCacheKey);

        if (!empty($oCache)) {

            return $oCache;
        }

        if (!empty($aColumns)) {

            $this->oDb->select($aColumns);
        }

        $this->oDb->where('user_id', $iUserId);
        $aResult = $this->oDb->get($sTable)->result();

        if (empty($aResult)) {

            $mOut = null;

        } else {

            $mOut = $aResult[0];
        }

        $this->setCache($sCacheKey, $mOut);

        return $mOut;
    }

    // --------------------------------------------------------------------------

    /**
     * Fetches records from a user_meta_table which may have many rows
     * @param  string   $sTable   The table to fetch from
     * @param  integer  $iUserId  The ID of the user the records belongs to
     * @param  array    $aColumns Any specific columns to select
     * @return array
     */
    public function getMany($sTable, $iUserId, $aColumns = array())
    {
        //  Check cache
        $sCacheKey = 'user-meta-many-' . $sTable . '-' . $iUserId;
        $oCache = $this->getCache($sCacheKey);

        if (!empty($oCache)) {

            return $oCache;
        }

        if (!empty($aColumns)) {

            $this->oDb->select($aColumns);
        }

        $this->oDb->where('user_id', $iUserId);
        $aResult = $this->oDb->get($sTable)->result();

        $this->setCache($sCacheKey, $aResult);

        return $aResult;
    }

    // --------------------------------------------------------------------------

    /**
     * Updates a user_meta_* table
     * @param  string  $sTable  The table to update
     * @param  integer $iUserId The ID of the user the record belongs to
     * @param  array   $aData   The data to set
     * @return boolean
     */
    public function update($sTable, $iUserId, $aData)
    {
        //  Safety: Ensure that the user_id is not overridden
        $aData['user_id'] = $iUserId;

        $this->oDb->where('user_id', $iUserId);
        if ($this->oDb->count_all_results($sTable)) {

            $this->oDb->set($aData);
            $this->oDb->where('user_id', $iUserId);
            $bResult = $this->oDb->update($sTable);

        } else {

            $this->oDb->set($aData);
            $bResult = $this->oDb->insert($sTable);
        }

        if ($bResult) {

            $this->unsetCache('user-meta-' . $sTable . '-' . $iUserId);
        }

        return $bResult;
    }

    // --------------------------------------------------------------------------

    /**
     * Updates a user_meta_* table
     * @param  string  $sTable  The table to update
     * @param  integer $iUserId The ID of the user ID being updated
     * @param  array   $aData   An array of rows to update
     * @return boolean
     */
    public function updateMany($sTable, $iUserId, $aData)
    {
        $this->oDb->trans_begin();
        foreach ($aData as $aRow) {

            if (empty($aRow['id'])) {

                //  Safety: Don't allow setting of the row ID
                unset($aRow['id']);
                //  Safety: overrwrite any user_id which may be passed
                $aRow['user_id'] = $iUserId;
                $this->oDb->set($aRow);
                if (!$this->oDb->insert($sTable)) {
                    $this->oDb->trans_rollback();
                    return false;
                }

            } else {

                //  Safety: Ensure that the row ID, and User ID are not overridden
                unset($aRow['id']);
                unset($aRow['user_id']);

                $this->oDb->set($aRow);
                if (!$this->oDb->update($sTable)) {
                    $this->oDb->trans_rollback();
                    return false;
                }
            }
        }

        $this->oDb->trans_commit();
        $this->unsetCache('user-meta-many-' . $sTable . '-' . $iUserId);
        return true;
    }

    // --------------------------------------------------------------------------

    /**
     * Deletes a user_meta_* row
     * @param  string  $sTable  The table to delete from
     * @param  integer $iUserId The ID of the user the record belongs to
     * @return boolean
     */
    public function delete($sTable, $iUserId)
    {
        $this->oDb->where('user_id', $iUserId);
        if (!$this->oDb->delete($sTable)) {
            return false;
        }

        $this->unsetCache('user-meta-' . $sTable . '-' . $iUserId);
        return $bResult;
    }

    // --------------------------------------------------------------------------

    /**
     * Deletes from a user_meta_* table
     * @param  string        $sTable  The table to delete from
     * @param  integer       $iUserId The ID of the user the record belongs to
     * @param  integer|array $mRowIds An array of row IDs to delete (or a single row ID)
     * @return boolean
     */
    public function deleteMany($sTable, $iUserId, $mRowIds = null)
    {
        $this->oDb->where('user_id', $iUserId);

        if (is_numeric($mRowIds)) {

            $this->oDb->where('id', $mRowIds);

        } elseif (is_array($mRowIds)) {

            $this->oDb->where_in('id', $mRowIds);
        }

        if (!$this->oDb->delete($sTable)) {
            return false;
        }

        $this->unsetCache('user-meta-many-' . $sTable . '-' . $iUserId);
        return $bResult;
    }
}

{'fraction_non_alphanumeric': 0.21750887273956396, 'fraction_numerical': 0.0001690045631232043, 'mean_word_length': 1.3329390892962745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2230477', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3375, 'n_words': 816}