Perforce Chronicle 2012.2/486814
API Documentation

P4_Typemap Class Reference

Abstracts operations against Perforce typemap entries. More...

Inheritance diagram for P4_Typemap:
P4_SpecAbstract P4_ModelAbstract P4_ConnectedAbstract P4_ModelInterface P4_ConnectedInterface

List of all members.

Public Member Functions

 getTypemap ()
 Get Typemap in array form.
 setTypemap ($typeMap)
 Set Typemap in array form.

Protected Member Functions

 _fromTypemapArray ($array)
 Convert a Typemap array (single entry) into a string, see getTypemap for format.
 _isValidTypemapArray ($array)
 Validates a single Typemap entry in array format, see getTypemap for format details.
 _toTypemapArray ($entry)
 Convert a raw Typemap string (single entry) into an array, see getTypemap for format.

Static Protected Attributes

static $_accessors
static $_mutators
static $_specType = 'typemap'

Detailed Description

Abstracts operations against Perforce typemap entries.

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_Typemap::_fromTypemapArray ( array) [protected]

Convert a Typemap array (single entry) into a string, see getTypemap for format.

Will validate input array and throw on errors.

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

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

        return $entry;
    }
P4_Typemap::_isValidTypemapArray ( array) [protected]

Validates a single Typemap entry in array format, see getTypemap for format details.

Parameters:
array$arrayA single Typemap 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('type');
        foreach ($fields as $key) {
            if (!array_key_exists($key, $array) ||
                trim($array[$key]) === '' ||
                preg_match('/\s/', $array[$key])) {
                return false;
            }
        }

        // Validate 'path' field is present, spaces are permitted
        if (!array_key_exists('path', $array) || $array['path'] === '') {
            return false;
        }

        return true;
    }
P4_Typemap::_toTypemapArray ( entry) [protected]

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

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

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

        return array_combine($keys, $type);
    }
P4_Typemap::getTypemap ( )

Get Typemap in array form.

Format of array is as follows:

array ( array ( 'type' => 'text', 'path' => '//...' ) )

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

        return $typeMap;
    }
P4_Typemap::setTypemap ( typeMap)

Set Typemap in array form.

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

Parameters:
array$typeMaparray of Typemap entries in array or raw string format.
Returns:
P4_Typemap provides a fluent interface.
    {
        if (!is_array($typeMap)) {
            throw new InvalidArgumentException(
                'Typemap must be passed as an array'
            );
        }

        $strings = array();
        foreach ($typeMap as $type) {
            // Normalize Typemap entries to array format for validation
            if (is_string($type)) {
                $type = $this->_toTypemapArray($type);
            }

            $strings[] = $this->_fromTypemapArray($type);
        }

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

        return $this;
    }

Member Data Documentation

P4_Typemap::$_accessors [static, protected]
Initial value:
 array(
        'TypeMap' => 'getTypemap'
    )

Reimplemented from P4_SpecAbstract.

P4_Typemap::$_mutators [static, protected]
Initial value:
 array(
        'TypeMap' => 'setTypemap'
    )

Reimplemented from P4_SpecAbstract.

P4_Typemap::$_specType = 'typemap' [static, protected]

Reimplemented from P4_SpecAbstract.


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