Perforce Chronicle 2012.2/486814
API Documentation
|
Provides a base for singular spec models such as protections, triggers, typemap, etc. More...
Public Member Functions | |
getFields () | |
Get all of the spec field names. | |
getRequiredFields () | |
Get all of the required fields. | |
getSpecDefinition () | |
Gets the definition of this specification from Perforce. | |
getValue ($field) | |
Get field value. | |
getValues () | |
Get all of the spec field values. | |
hasField ($field) | |
Check if this spec has a particular field. | |
save () | |
Save this spec to Perforce. | |
setValue ($field, $value) | |
Set field value. | |
setValues ($values) | |
Set several of the spec's values at once. | |
Static Public Member Functions | |
static | fetch (P4_Connection_Interface $connection=null) |
Get this spec from Perforce. | |
Protected Member Functions | |
_deferPopulate ($reset=false) | |
Schedule populate to run when data is requested (lazy-load). | |
_getDefaultValue ($field) | |
Get a field's default value. | |
_getSpecData () | |
Get raw spec data direct from Perforce. | |
_getValue ($field) | |
Get a field's raw value. | |
_getValues () | |
Get all of the raw field values. | |
_populate () | |
Get the values for this spec from Perforce and set them in the instance. | |
_setValue ($field, $value) | |
Set a field's raw value. | |
_setValues ($values) | |
Set several of the spec's raw values at once. | |
_validateRequiredFields ($values=null) | |
Ensure that all required fields have values. | |
Static Protected Member Functions | |
static | _getSpecType () |
Get the type of this spec. | |
Protected Attributes | |
$_needsPopulate = false | |
$_specDefinition = null | |
$_values = array() | |
Static Protected Attributes | |
static | $_accessors = array() |
static | $_mutators = array() |
static | $_specType = '' |
Provides a base for singular spec models such as protections, triggers, typemap, etc.
to extend.
Keyed specs such as changes, jobs, users, etc. should extend P4_Spec_KeyedAbstract.
When extending this class, be sure to set the _specType to the name of the Perforce Specification Type (e.g. protect, typemap, etc.)
To provide custom field accessor methods, add entries to the _accessors array in the form of: '<field>' => '<function>'. Accessor methods take no parameters and must return a value.
Similarly, to provide custom field mutator methods, add entries to the _mutators array in the same format. Mutator methods must accept a single value parameter. It is recommended that mutator methods return $this to provide a fluent interface.
P4_SpecAbstract::_deferPopulate | ( | $ | reset = false | ) | [protected] |
Schedule populate to run when data is requested (lazy-load).
bool | $reset | optionally clear instance values. |
Reimplemented in P4_Spec_PluralAbstract.
{ $this->_needsPopulate = true; if ($reset) { $this->_values = array(); } }
P4_SpecAbstract::_getDefaultValue | ( | $ | field | ) | [protected] |
Get a field's default value.
string | $field | the name of the field to get the default value of. |
{ $definition = $this->getSpecDefinition(); $field = $definition->getField($field); if (isset($field['default'])) { return $definition::expandDefault($field['default'], $this->getConnection()); } else { return null; } }
P4_SpecAbstract::_getSpecData | ( | ) | [protected] |
Get raw spec data direct from Perforce.
No caching involved.
Reimplemented in P4_Change, and P4_Spec_PluralAbstract.
{ $result = $this->getConnection()->run(static::_getSpecType(), "-o"); return $result->expandSequences()->getData(0); }
static P4_SpecAbstract::_getSpecType | ( | ) | [static, protected] |
Get the type of this spec.
P4_Spec_Exception | if the spec type is unset. |
{ // if spec type not defined, throw. if (!is_string(static::$_specType) || !trim(static::$_specType)) { throw new P4_Spec_Exception('No type is defined for this specification.'); } return static::$_specType; }
P4_SpecAbstract::_getValue | ( | $ | field | ) | [protected] |
Get a field's raw value.
string | $field | the name of the field to get the value of. |
P4_Spec_Exception | if the field does not exist. |
Reimplemented in P4_Spec_PluralAbstract.
{ // if field doesn't exist, throw exception. if (!$this->hasField($field)) { throw new P4_Spec_Exception("Can't get the value of a non-existant field."); } // if field has not been set, populate. if (!array_key_exists($field, $this->_values)) { $this->_populate(); } // if field has a value, return it. if (array_key_exists($field, $this->_values)) { return $this->_values[$field]; } // get default value if field is required - return null for // optional fields so that they don't get values automatically. // optional field defaults are best handled by the server. if ($this->getSpecDefinition($this->getConnection())->isRequiredField($field)) { return $this->_getDefaultValue($field); } else { return null; } }
P4_SpecAbstract::_getValues | ( | ) | [protected] |
P4_SpecAbstract::_populate | ( | ) | [protected] |
Get the values for this spec from Perforce and set them in the instance.
Won't clobber existing values.
Reimplemented in P4_Spec_PluralAbstract.
{ // early exit if populate not needed. if (!$this->_needsPopulate) { return; } // get spec data from Perforce. $data = $this->_getSpecData(); // ensure fields is an array. if (!is_array($data)) { throw new P4_Spec_Exception("Failed to populate spec. Perforce result invalid."); } // copy field values to instance without clobbering. foreach ($data as $key => $value) { if (!array_key_exists($key, $this->_values)) { $this->_values[$key] = $value; } } // clear needs populate flag. $this->_needsPopulate = false; }
P4_SpecAbstract::_setValue | ( | $ | field, |
$ | value | ||
) | [protected] |
Set a field's raw value.
string | $field | the name of the field to set the value of. |
mixed | $value | the value to set in the field. |
P4_Spec_Exception | if the field does not exist. |
Reimplemented in P4_Spec_PluralAbstract.
{ // if field doesn't exist, throw exception. if (!$this->hasField($field)) { throw new P4_Spec_Exception("Can't set the value of a non-existant field."); } // if field is read-only, throw exception. if ($this->getSpecDefinition()->isReadOnlyField($field)) { throw new P4_Spec_Exception("Can't set the value of a read-only field."); } $this->_values[$field] = $value; return $this; }
P4_SpecAbstract::_setValues | ( | $ | values | ) | [protected] |
Set several of the spec's raw values at once.
DOES NOT use custom mutators.
array | $values | associative array of raw field values. |
{ foreach ($values as $field => $value) { if ($this->hasField($field)) { $this->_values[$field] = $value; } } return $this; }
P4_SpecAbstract::_validateRequiredFields | ( | $ | values = null | ) | [protected] |
Ensure that all required fields have values.
array | $values | optional - set of values to validate against defaults to instance values. |
P4_Spec_Exception | if any required fields are missing values. |
{ $values = (array) $values ?: $this->_getValues(); // check that each required field has a value. foreach ($this->getRequiredFields() as $field) { $value = isset($values[$field]) ? $values[$field] : null; // in order to satisfy a required field, array values // must have elements and all values must have string length. if ((is_array($value) && !count($value)) || (!is_array($value) && !strlen($value))) { $missing[] = $field; } } if (isset($missing)) { throw new P4_Spec_Exception( "Cannot save spec. Missing required fields: " . implode(", ", $missing) ); } }
static P4_SpecAbstract::fetch | ( | P4_Connection_Interface $ | connection = null | ) | [static] |
Get this spec from Perforce.
Creates a new spec instance and schedules a populate.
P4_Connection_Interface | $connection | optional - a specific connection to use. |
InvalidArgumentException | if no id is given. |
{ $spec = new static($connection); $spec->_deferPopulate(); return $spec; }
P4_SpecAbstract::getFields | ( | ) |
Get all of the spec field names.
Implements P4_ModelInterface.
{ $fields = $this->getSpecDefinition()->getFields(); return array_keys($fields); }
P4_SpecAbstract::getRequiredFields | ( | ) |
Get all of the required fields.
{ $fields = array(); $spec = $this->getSpecDefinition(); foreach ($this->getFields() as $field) { if ($spec->isRequiredField($field)) { $fields[] = $field; } } return $fields; }
P4_SpecAbstract::getSpecDefinition | ( | ) |
Gets the definition of this specification from Perforce.
The specification definition provides: field names, field types, field options, preset values, comments, etc.
Only fetches it once per instance. Additionally, the spec definition object has a per-process (static) cache.
{ // load the spec definition if we haven't already done so. if (!$this->_specDefinition instanceof P4_Spec_Definition) { $this->_specDefinition = P4_Spec_Definition::fetch( static::_getSpecType(), $this->getConnection() ); } return $this->_specDefinition; }
P4_SpecAbstract::getValue | ( | $ | field | ) |
Get field value.
If a custom field accessor exists, it will be used.
string | $field | the name of the field to get the value of. |
P4_Spec_Exception | if the field does not exist. |
Implements P4_ModelInterface.
Reimplemented in P4_Job.
{ // if field doesn't exist, throw exception. if (!$this->hasField($field)) { throw new P4_Spec_Exception("Can't get the value of a non-existant field."); } // if field has custom accessor, use it. if (isset(static::$_accessors[$field])) { return $this->{static::$_accessors[$field]}(); } return $this->_getValue($field); }
P4_SpecAbstract::getValues | ( | ) |
P4_SpecAbstract::hasField | ( | $ | field | ) |
Check if this spec has a particular field.
string | $field | the field to check for the existence of. |
Implements P4_ModelInterface.
{ return in_array((string)$field, $this->getFields()); }
P4_SpecAbstract::save | ( | ) |
Save this spec to Perforce.
Reimplemented in P4_Client, P4_Job, P4_Stream, and P4_User.
{ // ensure all required fields have values. $this->_validateRequiredFields(); $this->getConnection()->run( static::_getSpecType(), "-i", $this->_getValues() ); // should re-populate (server may change values). $this->_deferPopulate(true); return $this; }
P4_SpecAbstract::setValue | ( | $ | field, |
$ | value | ||
) |
Set field value.
If a custom field mutator exists, it will be used.
string | $field | the name of the field to set the value of. |
mixed | $value | the value to set in the field. |
P4_Spec_Exception | if the field does not exist. |
Reimplemented in P4_Job.
{ // if field doesn't exist, throw exception. if (!$this->hasField($field)) { throw new P4_Spec_Exception("Can't set the value of a non-existant field."); } // if field has custom mutator, use it. if (isset(static::$_mutators[$field])) { return $this->{static::$_mutators[$field]}($value); } $this->_setValue($field, $value); return $this; }
P4_SpecAbstract::setValues | ( | $ | values | ) |
Set several of the spec's values at once.
Uses custom mutators where available.
array | $values | associative array of field values. |
InvalidArgumentException | if values is not an array. |
{ if (!is_array($values)) { throw new InvalidArgumentException("Values must be passed as an array."); } foreach ($values as $field => $value) { try { $this->setValue($field, $value); } catch (P4_Spec_Exception $e) { } } return $this; }
P4_SpecAbstract::$_accessors = array() [static, protected] |
Reimplemented in P4_Branch, P4_Change, P4_Client, P4_Depot, P4_Group, P4_Job, P4_Label, P4_Protections, P4_Stream, P4_Triggers, P4_Typemap, and P4_User.
P4_SpecAbstract::$_mutators = array() [static, protected] |
Reimplemented in P4_Branch, P4_Change, P4_Client, P4_Depot, P4_Group, P4_Job, P4_Label, P4_Protections, P4_Stream, P4_Triggers, P4_Typemap, and P4_User.
P4_SpecAbstract::$_needsPopulate = false [protected] |
P4_SpecAbstract::$_specDefinition = null [protected] |
P4_SpecAbstract::$_specType = '' [static, protected] |
Reimplemented in P4_Branch, P4_Change, P4_Client, P4_Depot, P4_Group, P4_Job, P4_Label, P4_Protections, P4_Stream, P4_Triggers, P4_Typemap, and P4_User.
P4_SpecAbstract::$_values = array() [protected] |