|
Perforce Chronicle 2012.2/486814
API Documentation
|
Abstracts operations against the Perforce triggers table. More...
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' |
Abstracts operations against the Perforce triggers table.
| 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.
| array | $array | The single Trigger entry to validate and convert to string |
| InvalidArgumentException | If 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.
| array | $array | A single Trigger entry in array format |
{
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.
| string | $entry | A single Trigger entry in string format |
| InvalidArgumentException | If 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' ) )
{
$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.
| array | $triggers | array of Trigger entries in array or raw string format. |
{
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;
}
P4_Triggers::$_accessors [static, protected] |
array(
'Triggers' => 'getTriggers'
)
Reimplemented from P4_SpecAbstract.
P4_Triggers::$_mutators [static, protected] |
array(
'Triggers' => 'setTriggers'
)
Reimplemented from P4_SpecAbstract.
P4_Triggers::$_specType = 'triggers' [static, protected] |
Reimplemented from P4_SpecAbstract.