Perforce Chronicle 2012.2/486814
API Documentation

P4_Protections Class Reference

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

Inheritance diagram for P4_Protections:
P4_SpecAbstract P4_ModelAbstract P4_ConnectedAbstract P4_ModelInterface P4_ConnectedInterface

List of all members.

Public Member Functions

 addProtection ($mode, $type, $name, $host, $path)
 Add a protection table entry.
 getProtections ()
 Get protections in array form.
 setProtections ($protections)
 Set protections table entries.

Protected Member Functions

 _fromProtectArray ($array)
 Convert a protection array (single entry) into a string, see getProtections for format.
 _isValidProtectArray ($array)
 Validates a single protection entry in array format, see getProtections for format details.
 _toProtectArray ($entry)
 Convert a raw protections string (single entry) into an array, see getProtections for format.

Static Protected Attributes

static $_accessors
static $_mutators
static $_specType = 'protect'

Detailed Description

Abstracts operations against the Perforce protections 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_Protections::_fromProtectArray ( array) [protected]

Convert a protection array (single entry) into a string, see getProtections for format.

Will validate input array and throw on errors.

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

        $protect =          $array['mode'] ." ".
                            $array['type'] ." ".
                            $array['name'] ." ".
                            $array['host'] ." ".
                    '"'.    $array['path'] .'"';

        return $protect;
    }
P4_Protections::_isValidProtectArray ( array) [protected]

Validates a single protection entry in array format, see getProtections for format details.

Parameters:
array$arrayA single protect 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('mode', 'type', 'name', 'host');
        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) || trim($array['path']) === '') {
            return false;
        }

        return true;
    }
P4_Protections::_toProtectArray ( entry) [protected]

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

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

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

        return array_combine($keys, $protection);
    }
P4_Protections::addProtection ( mode,
type,
name,
host,
path 
)

Add a protection table entry.

Parameters:
string$modethe access level (e.g. read, write, super, etc.)
string$typethe type of protection (user or group)
string$namethe user or group name
string$hostthe host restriction
string$paththe path to apply the protection to.
Returns:
P4_Protections provides a fluent interface.
Exceptions:
InvalidArgumentExceptionif any inputs are invalid.
    {
        if (!is_string($mode)
            || !is_string($type)
            || !is_string($name)
            || !is_string($host)
            || !is_string($path)
        ) {
            throw new InvalidArgumentException(
                "Cannot add protection. All parameters must be in string form."
            );
        }

        // add to protections array.
        $protections   = $this->_getValue('Protections');
        $protections[] = $this->_fromProtectArray(
            array(
                'mode' => $mode,
                'type' => $type,
                'name' => $name,
                'host' => $host,
                'path' => $path,
            )
        );

        $this->_setValue('Protections', $protections);

        return $this;
    }
P4_Protections::getProtections ( )

Get protections in array form.

Format of array is as follows:

array ( array ( 'mode' => 'super', 'type' => 'user', 'name' => '*', 'host' => '*', 'path' => '//...' ) )

Returns:
array array of protect entries.
    {
        $protections = array();
        foreach ((array) $this->_getValue('Protections') as $line) {
            $protections[] = $this->_toProtectArray($line);
        }

        return $protections;
    }
P4_Protections::setProtections ( protections)

Set protections table entries.

Individual protection entries may be specified in array or raw string format for convienence. See getProtections() for format.

Parameters:
array$protectionsarray of protect entries in array or raw string format.
Returns:
P4_Protections provides a fluent interface.
    {
        if (!is_array($protections)) {
            throw new InvalidArgumentException(
                'Protections must be passed as an array'
            );
        }

        $strings = array();
        foreach ($protections as $protect) {
            // Normalize protection entries to array format for validation
            if (is_string($protect)) {
                $protect = $this->_toProtectArray($protect);
            }

            $strings[] = $this->_fromProtectArray($protect);
        }

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

        return $this;
    }

Member Data Documentation

P4_Protections::$_accessors [static, protected]
Initial value:
 array(
        'Protections' => 'getProtections'
    )

Reimplemented from P4_SpecAbstract.

P4_Protections::$_mutators [static, protected]
Initial value:
 array(
        'Protections' => 'setProtections'
    )

Reimplemented from P4_SpecAbstract.

P4_Protections::$_specType = 'protect' [static, protected]

Reimplemented from P4_SpecAbstract.


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