Elgg\Database\EntityTable::getEntityAttributeWhereSql PHP Method

getEntityAttributeWhereSql() public method

Get the join and where clauses for working with entity attributes
public getEntityAttributeWhereSql ( array $options = [] ) : false | array
$options array
return false | array False on fail, array('joins', 'wheres')
    public function getEntityAttributeWhereSql(array $options = array())
    {
        if (!isset($options['types'])) {
            throw new InvalidArgumentException("The entity type must be defined for elgg_get_entities_from_attributes()");
        }
        if (is_array($options['types']) && count($options['types']) !== 1) {
            throw new InvalidArgumentException("Only one type can be passed to elgg_get_entities_from_attributes()");
        }
        // type can be passed as string or array
        $type = $options['types'];
        if (is_array($type)) {
            $type = $type[0];
        }
        // @todo the types should be defined somewhere (as constant on \ElggEntity?)
        if (!in_array($type, array('group', 'object', 'site', 'user'))) {
            throw new InvalidArgumentException("Invalid type '{$type}' passed to elgg_get_entities_from_attributes()");
        }
        $type_table = "{$this->db->prefix}{$type}s_entity";
        $return = array('joins' => array(), 'wheres' => array());
        // short circuit if nothing requested
        if ($options['attribute_name_value_pairs'] == ELGG_ENTITIES_ANY_VALUE) {
            return $return;
        }
        if (!is_array($options['attribute_name_value_pairs'])) {
            throw new InvalidArgumentException("attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()");
        }
        $wheres = array();
        // check if this is an array of pairs or just a single pair.
        $pairs = $options['attribute_name_value_pairs'];
        if (isset($pairs['name']) || isset($pairs['value'])) {
            $pairs = array($pairs);
        }
        $pair_wheres = array();
        foreach ($pairs as $index => $pair) {
            // must have at least a name and value
            if (!isset($pair['name']) || !isset($pair['value'])) {
                continue;
            }
            if (isset($pair['operand'])) {
                $operand = sanitize_string($pair['operand']);
            } else {
                $operand = '=';
            }
            if (is_numeric($pair['value'])) {
                $value = sanitize_string($pair['value']);
            } else {
                if (is_array($pair['value'])) {
                    $values_array = array();
                    foreach ($pair['value'] as $pair_value) {
                        if (is_numeric($pair_value)) {
                            $values_array[] = sanitize_string($pair_value);
                        } else {
                            $values_array[] = "'" . sanitize_string($pair_value) . "'";
                        }
                    }
                    $operand = 'IN';
                    if ($values_array) {
                        $value = '(' . implode(', ', $values_array) . ')';
                    }
                } else {
                    $value = "'" . sanitize_string($pair['value']) . "'";
                }
            }
            $name = sanitize_string($pair['name']);
            // case sensitivity can be specified per pair
            $pair_binary = '';
            if (isset($pair['case_sensitive'])) {
                $pair_binary = $pair['case_sensitive'] ? 'BINARY ' : '';
            }
            $pair_wheres[] = "({$pair_binary}type_table.{$name} {$operand} {$value})";
        }
        if ($where = implode(" {$options['attribute_name_value_pairs_operator']} ", $pair_wheres)) {
            $return['wheres'][] = "({$where})";
            $return['joins'][] = "JOIN {$type_table} type_table ON e.guid = type_table.guid";
        }
        return $return;
    }