6.10 Writing Conditions

Custom conditions are datatypes that implement Phing\Task\System\Condition\Condition. For example a custom condition that returns true if a string is all upper case could be written as:

class AllUpperCaseCondition implements Condition
{
    private $value;

    // The setter for the "value" attribute
    public function setValue(string $value)
    {
        $this->value = $value;
    }

    // This method evaluates the condition
    public function evaluate()
    {
        if ($this->value === null) {
            throw new BuildException("value attribute is not set");
        }
        return strtoupper($this->value) === $this->value;
    }
}

Adding the condition to the system is achieved as follows:

<typedef
    name="alluppercase"
    classname="AllUpperCaseCondition"/>

This condition can now be used wherever a Core Phing condition is used.

<condition property="allupper">
    <alluppercase value="THIS IS ALL UPPER CASE"/>
</condition>