<commit_before><?php

/**
 * Handles database operations for Attribute
 *
 * @author <NAME> (<EMAIL>)
 */
class AttributeRepository extends BaseRepository
{
    const
        TYPE_TEXTFIELD = 1,
        TYPE_TEXTAREA = 2,
        TYPE_DATE = 3,
        TYPE_BOOLEAN = 4,
        TYPE_SELECT = 5,
        TYPE_SELECT_MULTIPLE = 6;

	/**
	 * Insert a new attribute; returns the ID of the newly created record
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $attribute Attribute
	 * @return int
	 */
	public function insertAttribute(Attribute &$attribute)
	{
        if ($attribute->attribute_srl) throw new Exception('A srl must NOT be specified');
        $attribute->attribute_srl = getNextSequence();
        if(count($attribute->values ) == 0) unset($attribute->values);
		$output = executeQuery('shop.insertAttribute', $attribute);
		self::check($output);
        $this->insertAttributeScope($attribute);
        return $output;
	}

    /**
     * Insert attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function  insertAttributeScope(Attribute &$attribute)
    {
        $args = new stdClass();
        $args->attribute_srl = $attribute->attribute_srl;
        foreach($attribute->category_scope as $category){
            $args->category_srl = $category;
            $output = executeQuery('shop.insertAttributeScope',$args);
            self::check($output);
        }
        return TRUE;
    }


    /**
     * Update an attribute
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @throws Exception
     * @return mixed
     */
    public function updateAttribute(Attribute $attribute)
    {
        if (!$attribute->attribute_srl) throw new Exception('Target srl must be specified');
        if (count($attribute->values ) == 0) unset($attribute->values);
        $output = executeQuery('shop.updateAttribute', $attribute);
        self::check($output);
        $this->updateAttributeScope($attribute);
        return $output;
    }

    /**
     * Update attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function updateAttributeScope(Attribute &$attribute)
    {
        $this->deleteAttributesScope($attribute);
        $this->insertAttributeScope($attribute);
        return TRUE;
    }


    /**
	 * Deletes one or more attributes by $attribute_srl or $module_srl
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $args array
	 */
	public function deleteAttributes($args)
	{
        if (!isset($args->attribute_srls)) {
            if (!isset($args->module_srl)) throw new Exception("Please provide attribute_srls or module_srl.");
            if (!is_array($args->attribute_srls)) throw new Exception("This query expects an array of attribute srls");
        }
        //delete attributes
		$output = executeQuery('shop.deleteAttributes', $args);
        self::check($output);
        //delete attributes scope
        $output = executeQuery('shop.deleteAttributesScope',$args);
        self::check($output);
        //delete product attributes
		$output = executeQuery('shop.deleteProductAttributes',$args);
		self::check($output);
        return $output;
	}

    /**
     * Delete attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function deleteAttributesScope(Attribute &$attribute)
    {
        $args = new stdClass();
        $args->attribute_srls[] = $attribute->attribute_srl;
        $output = executeQuery('shop.deleteAttributesScope',$args);
        self::check($output);
        return TRUE;
    }

	/**
	 * Retrieve an attribute object from the database given a list of attribute srls.
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $srls array
	 * @return mixed|array of attribute objects or only one attribute object if count($srl) is 1
	 */
	public function getAttributes(array $srls)
	{
		$args = new stdClass();
		$args->attribute_srls = $srls;
		$output = executeQuery('shop.getAttributesBySrls', $args);
		self::check($output);
		$rez = array();
        if (count($srls) == 1) {
            $attribute = new Attribute($output->data);
            $this->getAttributeScope($attribute);
            return $attribute;
        }
        foreach ($output->data as $data) {
            $rez[$data->attribute_srl] = new Attribute($data);
            $this->getAttributeScope($rez[key($rez)]);
        }
		if( empty($rez)) return false;
		else {
			foreach($rez as $att){
				$att->values  = explode('|',$att->values);
			}
			return $rez;
		}
	}

	/**
	 * Retrieve all value combinations of configurable attributes
	 *
	 * @author <NAME>(<EMAIL>)
	 * @param $product array
	 * @return array of combinations
	 */
	public function getValuesCombinations(array $attributes,$i=0)
	{
		if (!isset($attributes[$i])) {
			return array();
		}
		if ($i == count($attributes) - 1) {
			return $attributes[$i];
		}<commit_msg>Issue 59:	Cannot use object of type Attribute as array on Product page<commit_after><?php

/**
 * Handles database operations for Attribute
 *
 * @author <NAME> (<EMAIL>)
 */
class AttributeRepository extends BaseRepository
{
    const
        TYPE_TEXTFIELD = 1,
        TYPE_TEXTAREA = 2,
        TYPE_DATE = 3,
        TYPE_BOOLEAN = 4,
        TYPE_SELECT = 5,
        TYPE_SELECT_MULTIPLE = 6;

	/**
	 * Insert a new attribute; returns the ID of the newly created record
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $attribute Attribute
	 * @return int
	 */
	public function insertAttribute(Attribute &$attribute)
	{
        if ($attribute->attribute_srl) throw new Exception('A srl must NOT be specified');
        $attribute->attribute_srl = getNextSequence();
        if(count($attribute->values ) == 0) unset($attribute->values);
		$output = executeQuery('shop.insertAttribute', $attribute);
		self::check($output);
        $this->insertAttributeScope($attribute);
        return $output;
	}

    /**
     * Insert attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function  insertAttributeScope(Attribute &$attribute)
    {
        $args = new stdClass();
        $args->attribute_srl = $attribute->attribute_srl;
        foreach($attribute->category_scope as $category){
            $args->category_srl = $category;
            $output = executeQuery('shop.insertAttributeScope',$args);
            self::check($output);
        }
        return TRUE;
    }


    /**
     * Update an attribute
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @throws Exception
     * @return mixed
     */
    public function updateAttribute(Attribute $attribute)
    {
        if (!$attribute->attribute_srl) throw new Exception('Target srl must be specified');
        if (count($attribute->values ) == 0) unset($attribute->values);
        $output = executeQuery('shop.updateAttribute', $attribute);
        self::check($output);
        $this->updateAttributeScope($attribute);
        return $output;
    }

    /**
     * Update attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function updateAttributeScope(Attribute &$attribute)
    {
        $this->deleteAttributesScope($attribute);
        $this->insertAttributeScope($attribute);
        return TRUE;
    }


    /**
	 * Deletes one or more attributes by $attribute_srl or $module_srl
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $args array
	 */
	public function deleteAttributes($args)
	{
        if (!isset($args->attribute_srls)) {
            if (!isset($args->module_srl)) throw new Exception("Please provide attribute_srls or module_srl.");
            if (!is_array($args->attribute_srls)) throw new Exception("This query expects an array of attribute srls");
        }
        //delete attributes
		$output = executeQuery('shop.deleteAttributes', $args);
        self::check($output);
        //delete attributes scope
        $output = executeQuery('shop.deleteAttributesScope',$args);
        self::check($output);
        //delete product attributes
		$output = executeQuery('shop.deleteProductAttributes',$args);
		self::check($output);
        return $output;
	}

    /**
     * Delete attribute scope
     *
     * @author <NAME> (<EMAIL>)
     * @param $attribute Attribute
     * @return boolean
     */
    public function deleteAttributesScope(Attribute &$attribute)
    {
        $args = new stdClass();
        $args->attribute_srls[] = $attribute->attribute_srl;
        $output = executeQuery('shop.deleteAttributesScope',$args);
        self::check($output);
        return TRUE;
    }

	/**
	 * Retrieve an attribute object from the database given a list of attribute srls.
	 *
	 * @author <NAME> (<EMAIL>)
	 * @param $srls array
	 * @return mixed|array of attribute objects or only one attribute object if count($srl) is 1
	 */
	public function getAttributes(array $srls)
	{
		$args = new stdClass();
		$args->attribute_srls = $srls;
		$output = executeQueryArray('shop.getAttributesBySrls', $args);
		self::check($output);
		$rez = array();
        foreach ($output->data as $data) {
            $rez[$data->attribute_srl] = new Attribute($data);
            $this->getAttributeScope($rez[key($rez)]);
        }
		if( empty($rez)) return false;
		else {
			foreach($rez as $att){
				$att->values  = explode('|',$att->values);
			}
			return $rez;
		}
	}

	/**
	 * Retrieve all value combinations of configurable attributes
	 *
	 * @author <NAME>(<EMAIL>)
	 * @param $product array
	 * @return array of combinations
	 */
	public function getValuesCombinations(array $attributes,$i=0)
	{
		if (!isset($attributes[$i])) {
			return array();
		}
		if ($i == count($attributes) - 1) {
			return $attributes[$i];
		}
{'fraction_non_alphanumeric': 0.1364693980778958, 'fraction_numerical': 0.0025290844714213456, 'mean_word_length': 2.816988416988417, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 93, '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': '811517', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2747, 'n_words': 706}