polyfony-inc/polyfony

View on GitHub
Private/Polyfony/Query/Conditions.php

Summary

Maintainability
C
7 hrs
Test Coverage
<?php

namespace Polyfony\Query;

class Conditions extends Base {

    public function whereIn(array $conditions, bool $invert=false) :self {
        // for each provided strict condition
        foreach($conditions as $column => $values) {
            // if we have to conditions
            if(!is_array($values) || empty($values)) {
                Throw new \Polyfony\Exception('Unsafe ->in() query with empty list of values');
            }
            // list of placeholders
            $placeholders = [];
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder(
                $this->Quote ,
                $column, 
                false, 
                $invert ? 'where_not_in' : 'where_in'
            );
            // for each possible values
            foreach($values as $index => $value) {
                list(,$placeholder) = Convert::columnToPlaceholder(
                    $this->Quote ,
                    $column.'_'.$index,
                    false,
                    $invert ? 'where_not_in' : 'where_in'
                );
                // save the value
                $this->Values[":{$placeholder}"] = $value;
                // save the placeholder
                $placeholders[] = ':'.$placeholder;
            }
            // save the condition
            $this->Conditions[] = 
                "{$this->Operator} ( {$column} ".
                ($invert ? 'NOT ' : '').
                "IN ( ".implode(', ', $placeholders)." ) )";
        }
        // return self to the next method
        return $this;
    }

    public function whereNotIn(array $conditions) :self {
        // same as the in method, but inverted
        return $this->whereIn($conditions, true);
    }
    
    // whereIdenticalTo
    // add a condition
    public function where(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} = :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = $value;
        }
        // return self to the next method
        return $this;
    }


    // where NotIdenticalTo
    // add a condition
    public function whereNot(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_not');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} <> :{$placeholder} OR {$column} IS NULL )";
            // save the value
            $this->Values[":{$placeholder}"] = $value;
        }
        // return self to the next method
        return $this;
    }
    

    public function whereStartsWith(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_starts_with');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} LIKE :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = "{$value}%";
        }
        // return self to the next method
        return $this;
    }


    public function whereEndsWith(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_ends_with');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} LIKE :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = "%$value";
        }
        // return self to the next method
        return $this;
    }


    public function whereContains(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_contains');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} LIKE :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = "%{$value}%";
        }
        // return self to the next method
        return $this;
    }


    public function whereMatch(array $conditions) :self {    
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_match');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} MATCH :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = $value;
        }
        // return self to the next method
        return $this;
    }

    // legacy alias
    public function whereHigherThan(array $conditions) :self {
        return $this->whereGreaterThan($conditions);
    }

    public function whereGreaterThan(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_greater_than');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} > :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = $value;
        }
        // return self to the next method
        return $this;
    }

    // legacy alias
    public function whereLowerThan(array $conditions) :self {
        return $this->whereLessThan($conditions);
    }

    public function whereLessThan(array $conditions) :self {
        // for each provided strict condition
        foreach($conditions as $column => $value) {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_less_than');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} < :{$placeholder} )";
            // save the value
            $this->Values[":{$placeholder}"] = $value;
        }
        // return self to the next method
        return $this;
    }


    public function whereBetween(
        string $column, 
        float $lower, 
        float $higher
    ) :self {
        // secure the column name
        list(
            $column, 
            $placeholder
        ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_between');
        // save the condition
        $this->Conditions[] = "{$this->Operator} ( {$column} BETWEEN :min_{$placeholder} AND :max_{$placeholder} )";
        // add the min value
        $this->Values[":min_{$placeholder}"] = $lower;
        // add the max value
        $this->Values[":max_{$placeholder}"] = $higher;
        // return self to the next method
        return $this;
    }

    // this should be renamed whereBlank
    // we are still supporting NON-array parameter, this will be removed at some point in time
    public function whereEmpty($conditions) :self {
        // if provided conditions are an array
        if(is_array($conditions)) {
            // for each condition
            foreach($conditions as $column) {
                // add the condition
                $this->whereEmpty($column);
            }
        }
        else {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$conditions, false, 'where_empty');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} == :empty_{$placeholder} OR {$column} IS NULL )";
            // add the empty value
            $this->Values[":empty_{$placeholder}"] = '';
        }
        // return self to the next method
        return $this;
    }

    // this should be renamed whereNotBlank
    // we are still supporting NON-array parameter, this will be removed at some point in time
    public function whereNotEmpty($conditions) :self {
        // if provided conditions are an array
        if(is_array($conditions)) {
            // for each condition
            foreach($conditions as $column) {
                // add the condition
                $this->whereNotEmpty($column);
            }
        }
        else {
            // secure the column name
            list(
                $column, 
                $placeholder
            ) = Convert::columnToPlaceholder($this->Quote ,$conditions, false, 'where_not_empty');
            // save the condition
            $this->Conditions[] = "{$this->Operator} ( {$column} <> :empty_{$placeholder} AND {$column} IS NOT NULL )";
            // add the empty value
            $this->Values[":empty_{$placeholder}"] = '';
        }
        // return self to the next method
        return $this;
    }

    // this should only accept arrays... 
    // this behavior is not coherent with the rest of the class
    public function whereNull($column) :self {
        // secure the column name
        list(
            $column, 
            $placeholder
        ) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_null');
        // save the condition
        $this->Conditions[] = "{$this->Operator} ( {$column} IS NULL )";
        // return self to the next method
        return $this;
    }

    // this should only accept arrays... 
    // this behavior is not coherent with the rest of the class
    public function whereNotNull($column) :self {
        // secure the column name
        list($column) = Convert::columnToPlaceholder($this->Quote ,$column, false, 'where_not_null');
        // save the condition
        $this->Conditions[] = "{$this->Operator} ( {$column} IS NOT NULL )";
        // return self to the next method
        return $this;
    }


}