<commit_before><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Ldap
 */

namespace Zend\Ldap;

use DateTime;

/**
 * Zend\Ldap\Attribute is a collection of LDAP attribute related functions.
 *
 * @category   Zend
 * @package    Zend_Ldap
 */
class Attribute
{
    const PASSWORD_HASH_MD5   = 'md5';
    const PASSWORD_HASH_SMD5  = 's<PASSWORD>';
    const PASSWORD_HASH_SHA   = 'sha';
    const PASSWORD_HASH_SSHA  = '<PASSWORD>';
    const PASSWORD_UNICODEPWD = '<PASSWORD>';

    /**
     * Sets a LDAP attribute.
     *
     * @param  array                     $data
     * @param  string                    $attribName
     * @param  string|array|\Traversable $value
     * @param  boolean                   $append
     * @return void
     */
    public static function setAttribute(array &$data, $attribName, $value, $append = false)
    {
        $attribName = strtolower($attribName);
        $valArray   = array();
        if (is_array($value) || ($value instanceof \Traversable)) {
            foreach ($value as $v) {
                $v = self::valueToLdap($v);
                if ($v !== null) {
                    $valArray[] = $v;
                }
            }
        } elseif ($value !== null) {
            $value = self::valueToLdap($value);
            if ($value !== null) {
                $valArray[] = $value;
            }
        }

        if ($append === true && isset($data[$attribName])) {
            if (is_string($data[$attribName])) {
                $data[$attribName] = array($data[$attribName]);
            }
            $data[$attribName] = array_merge($data[$attribName], $valArray);
        } else {
            $data[$attribName] = $valArray;
        }
    }

    /**
     * Gets a LDAP attribute.
     *
     * @param  array   $data
     * @param  string  $attribName
     * @param  integer $index
     * @return array|mixed
     */
    public static function getAttribute(array $data, $attribName, $index = null)
    {
        $attribName = strtolower($attribName);
        if ($index === null) {
            if (!isset($data[$attribName])) {
                return array();
            }
            $retArray = array();
            foreach ($data[$attribName] as $v) {
                $retArray[] = self::valueFromLDAP($v);
            }
            return $retArray;
        } elseif (is_int($index)) {
            if (!isset($data[$attribName])) {
                return null;
            } elseif ($index >= 0 && $index < count($data[$attribName])) {
                return self::valueFromLDAP($data[$attribName][$index]);
            } else {
                return null;
            }
        }

        return null;
    }

    /**
     * Checks if the given value(s) exist in the attribute
     *
     * @param array       $data
     * @param string      $attribName
     * @param mixed|array $value
     * @return boolean
     */
    public static function attributeHasValue(array &$data, $attribName, $value)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return false;
        }

        if (is_scalar($value)) {
            $value = array($value);
        }

        foreach ($value as $v) {
            $v = self::valueToLDAP($v);
            if (!in_array($v, $data[$attribName], true)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Removes duplicate values from a LDAP attribute
     *
     * @param array  $data
     * @param string $attribName
     * @return void
     */
    public static function removeDuplicatesFromAttribute(array &$data, $attribName)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return;
        }
        $data[$attribName] = array_values(array_unique($data[$attribName]));
    }

    /**
     * Remove given values from a LDAP attribute
     *
     * @param array       $data
     * @param string      $attribName
     * @param mixed|array $value
     * @return void
     */
    public static function removeFromAttribute(array &$data, $attribName, $value)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return;
        }

        if (is_scalar($value)) {
            $value = array($value);
        }

        $valArray = array();
        foreach ($value as $v) {
            $v = self::valueToLDAP($v);
            if ($v !== null) {
                $valArray[] = $v;
            }
        }

        $resultArray = $data[$attribName];
        foreach ($valArray as $rv) {
            $keys = array_keys($resultArray, $rv);
            foreach ($keys as $k) {
                unset($resultArray[$k]);
            }
        }
        $resultArray       = array_values($resultArray);
        $data[$attribName] = $resultArray;
    }

    /**
     * @param  mixed $value
     * @return string|null
     */
    private static function valueToLdap($value)<commit_msg>change valueToLDAP to valueToLdap and valueFromLDAP to valueFromLdap<commit_after><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Ldap
 */

namespace Zend\Ldap;

use DateTime;

/**
 * Zend\Ldap\Attribute is a collection of LDAP attribute related functions.
 *
 * @category   Zend
 * @package    Zend_Ldap
 */
class Attribute
{
    const PASSWORD_HASH_MD5   = 'md5';
    const PASSWORD_HASH_SMD5  = 'smd5';
    const PASSWORD_HASH_SHA   = 'sha';
    const PASSWORD_HASH_SSHA  = 'ssha';
    const PASSWORD_UNICODEPWD = '<PASSWORD>';

    /**
     * Sets a LDAP attribute.
     *
     * @param  array                     $data
     * @param  string                    $attribName
     * @param  string|array|\Traversable $value
     * @param  boolean                   $append
     * @return void
     */
    public static function setAttribute(array &$data, $attribName, $value, $append = false)
    {
        $attribName = strtolower($attribName);
        $valArray   = array();
        if (is_array($value) || ($value instanceof \Traversable)) {
            foreach ($value as $v) {
                $v = self::valueToLdap($v);
                if ($v !== null) {
                    $valArray[] = $v;
                }
            }
        } elseif ($value !== null) {
            $value = self::valueToLdap($value);
            if ($value !== null) {
                $valArray[] = $value;
            }
        }

        if ($append === true && isset($data[$attribName])) {
            if (is_string($data[$attribName])) {
                $data[$attribName] = array($data[$attribName]);
            }
            $data[$attribName] = array_merge($data[$attribName], $valArray);
        } else {
            $data[$attribName] = $valArray;
        }
    }

    /**
     * Gets a LDAP attribute.
     *
     * @param  array   $data
     * @param  string  $attribName
     * @param  integer $index
     * @return array|mixed
     */
    public static function getAttribute(array $data, $attribName, $index = null)
    {
        $attribName = strtolower($attribName);
        if ($index === null) {
            if (!isset($data[$attribName])) {
                return array();
            }
            $retArray = array();
            foreach ($data[$attribName] as $v) {
                $retArray[] = self::valueFromLdap($v);
            }
            return $retArray;
        } elseif (is_int($index)) {
            if (!isset($data[$attribName])) {
                return null;
            } elseif ($index >= 0 && $index < count($data[$attribName])) {
                return self::valueFromLdap($data[$attribName][$index]);
            } else {
                return null;
            }
        }

        return null;
    }

    /**
     * Checks if the given value(s) exist in the attribute
     *
     * @param array       $data
     * @param string      $attribName
     * @param mixed|array $value
     * @return boolean
     */
    public static function attributeHasValue(array &$data, $attribName, $value)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return false;
        }

        if (is_scalar($value)) {
            $value = array($value);
        }

        foreach ($value as $v) {
            $v = self::valueToLdap($v);
            if (!in_array($v, $data[$attribName], true)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Removes duplicate values from a LDAP attribute
     *
     * @param array  $data
     * @param string $attribName
     * @return void
     */
    public static function removeDuplicatesFromAttribute(array &$data, $attribName)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return;
        }
        $data[$attribName] = array_values(array_unique($data[$attribName]));
    }

    /**
     * Remove given values from a LDAP attribute
     *
     * @param array       $data
     * @param string      $attribName
     * @param mixed|array $value
     * @return void
     */
    public static function removeFromAttribute(array &$data, $attribName, $value)
    {
        $attribName = strtolower($attribName);
        if (!isset($data[$attribName])) {
            return;
        }

        if (is_scalar($value)) {
            $value = array($value);
        }

        $valArray = array();
        foreach ($value as $v) {
            $v = self::valueToLdap($v);
            if ($v !== null) {
                $valArray[] = $v;
            }
        }

        $resultArray = $data[$attribName];
        foreach ($valArray as $rv) {
            $keys = array_keys($resultArray, $rv);
            foreach ($keys as $k) {
                unset($resultArray[$k]);
            }
        }
        $resultArray       = array_values($resultArray);
        $data[$attribName] = $resultArray;
    }

    /**
     * @param  mixed $value
     * @return string|null
     */
    private static function valueToLdap($value)
{'fraction_non_alphanumeric': 0.13496119628998676, 'fraction_numerical': 0.002555366269165247, 'mean_word_length': 1.7319027921406411, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3286963', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3121, 'n_words': 696}