Perforce Chronicle 2012.2/486814
API Documentation

P4_Triggers Class Reference

Abstracts operations against the Perforce triggers table. More...

Inheritance diagram for P4_Triggers:
P4_SpecAbstract P4_ModelAbstract P4_ConnectedAbstract P4_ModelInterface P4_ConnectedInterface

List of all members.

Public Member Functions

 getTriggers ()
 Get Triggers in array form.
 setTriggers ($triggers)
 Set Triggers in array form.

Protected Member Functions

 _fromTriggerArray ($array)
 Convert a Trigger array (single entry) into a string, see getTriggers for format.
 _isValidTriggerArray ($array)
 Validates a single Trigger entry in array format, see getTriggers for format details.
 _toTriggerArray ($entry)
 Convert a raw Trigger string (single entry) into an array, see getTriggers for format.

Static Protected Attributes

static $_accessors
static $_mutators
static $_specType = 'triggers'

Detailed Description

Abstracts operations against the Perforce triggers table.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4_Triggers::_fromTriggerArray ( array) [protected]

Convert a Trigger array (single entry) into a string, see getTriggers for format.

Will validate input array and throw on errors.

Parameters:
array$arrayThe single Trigger entry to validate and convert to string
Returns:
string A single Trigger entry in string format
Exceptions:
InvalidArgumentExceptionIf input is poorly formatted
    {
        // Validate the array, will throw if invalid
        if (!$this->_isValidTriggerArray($array)) {
            throw new InvalidArgumentException(
                'Trigger array entry is invalid.'
            );
        }

        $entry =        $array['name']      ." ".
                        $array['type']      ." ".
                  '"'.  $array['path']      .'" '.
                  '"'.  $array['command']   .'"';

        return $entry;
    }
P4_Triggers::_isValidTriggerArray ( array) [protected]

Validates a single Trigger entry in array format, see getTriggers for format details.

Parameters:
array$arrayA single Trigger entry in array format
Returns:
bool True - Valid, False - Error(s) found
    {
        if (!is_array($array)) {
            return false;
        }

        // Validate all 'word' fields are present and don't contain spaces
        $fields = array('name', 'type');
        foreach ($fields as $key) {
            if (!array_key_exists($key, $array) ||
                trim($array[$key]) === '' ||
                preg_match('/\s/', $array[$key])) {
                return false;
            }
        }

        // Validate 'path' and 'command' fields are present, spaces are permitted
        $fields = array('path', 'command');
        foreach ($fields as $key) {
            if (!array_key_exists($key, $array) ||
                trim($array[$key]) === '') {
                return false;
            }
        }

        return true;
    }
P4_Triggers::_toTriggerArray ( entry) [protected]

Convert a raw Trigger string (single entry) into an array, see getTriggers for format.

Parameters:
string$entryA single Trigger entry in string format
Returns:
array A single Trigger entry array
Exceptions:
InvalidArgumentExceptionIf passed string is unparsable
    {
        $keys   = array('name', 'type', 'path', 'command');
        $type   = str_getcsv($entry, ' ');

        if (count($type) != count($keys)) {
            throw new InvalidArgumentException(
                'Trigger entry with missing field(s) encountered'
            );
        }

        return array_combine($keys, $type);
    }
P4_Triggers::getTriggers ( )

Get Triggers in array form.

Format of array is as follows:

array ( array ( 'name' => 'my-trigger', 'type' => 'form-in', 'path' => '//...', 'command' => '/path/to/my/script.sh' ) )

Returns:
array array of Trigger entries.
    {
        $triggers = array();
        // Go over each entry; defaults to empty array to avoid warnings on null
        foreach ($this->_getValue('Triggers') ?: array() as $line) {
            $triggers[] = $this->_toTriggerArray($line);
        }

        return $triggers;
    }
P4_Triggers::setTriggers ( triggers)

Set Triggers in array form.

See getTriggers() for format. Individual Trigger entries may also be specified in raw string format for convienence.

Parameters:
array$triggersarray of Trigger entries in array or raw string format.
Returns:
P4_Triggers provides a fluent interface.
    {
        if (!is_array($triggers)) {
            throw new InvalidArgumentException(
                'Triggers must be passed as an array'
            );
        }

        $strings = array();
        foreach ($triggers as $trigger) {
            // Normalize Trigger entries to array format for validation
            if (is_string($trigger)) {
                $trigger = $this->_toTriggerArray($trigger);
            }

            $strings[] = $this->_fromTriggerArray($trigger);
        }

        $this->_setValue('Triggers', $strings);

        return $this;
    }

Member Data Documentation

P4_Triggers::$_accessors [static, protected]
Initial value:
 array(
        'Triggers' => 'getTriggers'
    )

Reimplemented from P4_SpecAbstract.

P4_Triggers::$_mutators [static, protected]
Initial value:
 array(
        'Triggers' => 'setTriggers'
    )

Reimplemented from P4_SpecAbstract.

P4_Triggers::$_specType = 'triggers' [static, protected]

Reimplemented from P4_SpecAbstract.


The documentation for this class was generated from the following file: