Perforce Chronicle 2012.2/486814
API Documentation

P4_Validate_KeyName Class Reference

Validates string for suitability as a Perforce key name. More...

Inheritance diagram for P4_Validate_KeyName:
P4_Validate_Abstract P4_Validate_AttributeName P4_Validate_CounterName P4_Validate_GroupName P4_Validate_SpecName P4_Validate_StreamName P4_Validate_UserName P4Cms_Validate_RecordField P4Cms_Validate_ContentTypeElementName

List of all members.

Public Member Functions

 allowCommas ($allowed)
 Control if comma character ',' is permitted.
 allowPercent ($allowed)
 Control if percent character '' is permitted.
 allowPositional ($allowed)
 Control if positional specifiers are permitted (values containing '%%", as in '%1').
 allowPurelyNumeric ($allowed)
 Control if purely numeric key names are permitted (values consisting of only characters 0-9).
 allowSlashes ($allowed)
 Control if forward slashes '/' are permitted.
 isValid ($value)
 Checks if the given string is a valid perforce spec name.

Public Attributes

const COMMAS = 'commas'
const HAS_WHITESPACE = 'hasSpaces'
const INVALID_TYPE = 'invalidType'
const IS_EMPTY = 'isEmpty'
const IS_NUMERIC = 'isNumeric'
const LEADING_MINUS = 'leadingMinus'
const PERCENT = 'percent'
const POSITIONAL_SPECIFIERS = 'positional'
const RELATIVE = 'relative'
const REVISION_CHARACTERS = 'revision'
const SLASHES = 'slashes'
const UNPRINTABLE_CHARACTERS = 'unprintable'
const WILDCARDS = 'wildcards'

Protected Attributes

 $_allowCommas = true
 $_allowPercent = true
 $_allowPositional = true
 $_allowPurelyNumeric = false
 $_allowRelative = true
 $_allowSlashes = false
 $_messageTemplates

Detailed Description

Validates string for suitability as a Perforce key name.

By default disallows:

  • whitespace
  • purely numeric names
  • revision characters ('#', '@')
  • wildcards ('*', '...')
  • slashes ('/')
  • non-printable characters
  • leading minus ('-')

By default allows, but can block:

  • percent character ('')
  • positional specifiers ('%x')
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_Validate_KeyName::allowCommas ( allowed)

Control if comma character ',' is permitted.

Parameters:
bool$allowedpass true (default) to allow commas ',', false to disallow.
    {
        $this->_allowCommas = (bool) $allowed;
    }
P4_Validate_KeyName::allowPercent ( allowed)

Control if percent character '' is permitted.

Parameters:
bool$allowedpass true (default) to allow percent '', false to disallow.
    {
        $this->_allowPercent = (bool) $allowed;
    }
P4_Validate_KeyName::allowPositional ( allowed)

Control if positional specifiers are permitted (values containing '%%", as in '%1').

Parameters:
bool$allowedpass true to allow positional specifiers, false to disallow.
    {
        $this->_allowPositional = (bool) $allowed;
    }
P4_Validate_KeyName::allowPurelyNumeric ( allowed)

Control if purely numeric key names are permitted (values consisting of only characters 0-9).

Parameters:
bool$allowedpass true to allow purely numeric names, false to disallow.
    {
        $this->_allowPurelyNumeric = (bool) $allowed;
    }
P4_Validate_KeyName::allowSlashes ( allowed)

Control if forward slashes '/' are permitted.

Parameters:
bool$allowedpass true to allow forward slashes, false (default) to disallow.
    {
        $this->_allowSlashes = (bool) $allowed;
    }
P4_Validate_KeyName::isValid ( value)

Checks if the given string is a valid perforce spec name.

Parameters:
string | int$valuespec name value to validate.
Returns:
boolean true if value is a valid spec name, false otherwise.

Reimplemented in P4_Validate_StreamName, P4Cms_Validate_ContentTypeElementName, and P4Cms_Validate_RecordField.

    {
        $this->_setValue($value);

        // permit ints if allowPurelyNumeric is true.
        if ($this->_allowPurelyNumeric && is_int($value)) {
            $value = (string) $value;
        }

        // test for valid type.
        if (!is_string($value)) {
            $this->_error(static::INVALID_TYPE);
            return false;
        }

        // test for unprintable characters.
        if (preg_match('/[\x00-\x1F\x80-\xFF]/', $value)) {
            $this->_error(static::UNPRINTABLE_CHARACTERS);
            return false;
        }

        // test for purely numeric name.
        if (!$this->_allowPurelyNumeric && preg_match('/^[0-9]+$/', $value)) {
            $this->_error(static::IS_NUMERIC);
            return false;
        }

        // test for empty value.
        if ($value === '') {
            $this->_error(static::IS_EMPTY);
            return false;
        }

        // test for whitespace.
        if (preg_match('/\s/', $value)) {
            $this->_error(static::HAS_WHITESPACE);
            return false;
        }

        // test for revision characters.
        if (preg_match('/@|#/', $value)) {
            $this->_error(static::REVISION_CHARACTERS);
            return false;
        }

        // test for wildcard characters.
        if (preg_match('/\*|\.\.\./', $value)) {
            $this->_error(static::WILDCARDS);
            return false;
        }

        // test for positional specifiers.
        if (!$this->_allowPositional && strpos($value, '%%') !== false) {
            $this->_error(static::POSITIONAL_SPECIFIERS);
            return false;
        }

        // test for percent character
        if (!$this->_allowPercent && strpos($value, '%') !== false) {
            $this->_error(static::PERCENT);
            return false;
        }

        // test for comma character
        if (!$this->_allowCommas && strpos($value, ',') !== false) {
            $this->_error(static::COMMAS);
            return false;
        }

        // test for leading minus ('-') character.
        if ($value[0] === "-") {
            $this->_error(static::LEADING_MINUS);
            return false;
        }

        // test for forward slash character.
        if (!$this->_allowSlashes && strpos($value, '/') !== false) {
            $this->_error(static::SLASHES);
            return false;
        }

        // If relative paths aren't allowed the following are blocked:
        //  two or more slashes after the first character
        //  containing '/./'
        //  containing '/../'
        //  ending in a slash
        //  ending in '/.'
        //  ending in '/..'
        if (!$this->_allowRelative && preg_match('#.+//|/\./|/\.\./|.+/$|/\.$|/\.\.$#', $value)) {
            $this->_error(static::RELATIVE);
            return false;
        }

        return true;
    }

Member Data Documentation

P4_Validate_KeyName::$_allowCommas = true [protected]

Reimplemented in P4_Validate_StreamName.

P4_Validate_KeyName::$_allowPercent = true [protected]

Reimplemented in P4_Validate_StreamName.

P4_Validate_KeyName::$_allowPositional = true [protected]
P4_Validate_KeyName::$_allowPurelyNumeric = false [protected]
P4_Validate_KeyName::$_allowRelative = true [protected]

Reimplemented in P4_Validate_StreamName.

P4_Validate_KeyName::$_allowSlashes = false [protected]

Reimplemented in P4_Validate_StreamName.

P4_Validate_KeyName::$_messageTemplates [protected]
Initial value:
 array(
        self::INVALID_TYPE              => "Invalid type given.",
        self::IS_EMPTY                  => "Is an empty string.",
        self::IS_NUMERIC                => "Purely numeric values are not allowed.",
        self::HAS_WHITESPACE            => "Whitespace is not permitted.",
        self::REVISION_CHARACTERS       => "Revision characters ('#', '@') are not permitted.",
        self::WILDCARDS                 => "Wildcards ('*', '...') are not permitted.",
        self::LEADING_MINUS             => "First character cannot be minus ('-').",
        self::UNPRINTABLE_CHARACTERS    => "Unprintable characters are not permitted.",
        self::SLASHES                   => "Slashes ('/') are not permitted.",
        self::COMMAS                    => "Commas (',') are not permitted.",
        self::PERCENT                   => "Percent ('%') is not permitted.",
        self::POSITIONAL_SPECIFIERS     => "Positional specifiers ('%%x') are not permitted.",
        self::RELATIVE                  => "Relative paths are not permitted."
    )

Reimplemented from P4_Validate_Abstract.

const P4_Validate_KeyName::COMMAS = 'commas'
const P4_Validate_KeyName::INVALID_TYPE = 'invalidType'
const P4_Validate_KeyName::IS_EMPTY = 'isEmpty'
const P4_Validate_KeyName::IS_NUMERIC = 'isNumeric'
const P4_Validate_KeyName::LEADING_MINUS = 'leadingMinus'
const P4_Validate_KeyName::PERCENT = 'percent'
const P4_Validate_KeyName::RELATIVE = 'relative'
const P4_Validate_KeyName::SLASHES = 'slashes'
const P4_Validate_KeyName::WILDCARDS = 'wildcards'

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