Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Diff_Chunk Class Reference

Wrapper for chunks of values in a diff result. More...

List of all members.

Public Member Functions

 __construct (array $values=null)
 Create a new chunk from a list of unchanged (flat) or changed (multi-dimensional deleted/inserted) values.
 getChunkType ()
 Get the type of this chunk: same, insert, delete, change.
 getLeft ($index=null)
 Get the left-hand values for this diff chunk.
 getMaxValueCount ()
 Get the highest number of values in left/right.
 getRawValues ()
 Get the raw values array.
 getRight ($index=null)
 Get the right-hand values for this diff chunk.
 getSubDiff ($index, P4Cms_Diff_Options $options=null)
 Compare the left/right values at the specified index.
 isChange ()
 Check if this diff chunk is of type change.
 isDelete ()
 Check if this diff chunk is of type delete.
 isInsert ()
 Check if this diff chunk is of type insert.
 isSame ()
 Check if this diff chunk is of type same.
 isWhitespaceChange ($semantic=true)
 Check if this diff chunk a whitespace only change.
 setRawValues (array $values)
 Set the raw values array.

Public Attributes

const SIDE_LEFT = 'left'
const SIDE_RIGHT = 'right'
const TYPE_CHANGE = 'change'
const TYPE_DELETE = 'delete'
const TYPE_INSERT = 'insert'
const TYPE_SAME = 'same'

Protected Member Functions

 _getSide ($side, $index=null)
 Get the left or right-hand set of values for this diff chunk.

Protected Attributes

 $_values = null

Detailed Description

Wrapper for chunks of values in a diff result.

Each chunk is either a consecutive block of unchanged values, or a block of deleted/inserted values.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

P4Cms_Diff_Chunk::__construct ( array $  values = null)

Create a new chunk from a list of unchanged (flat) or changed (multi-dimensional deleted/inserted) values.

Parameters:
array$valuesoptional - flat array for chunks of unchanged values multi-dimensional deleted/inserted array for chunks of changed values.
    {
        $this->_values = is_array($values) ? $values : array();
    }

Member Function Documentation

P4Cms_Diff_Chunk::_getSide ( side,
index = null 
) [protected]

Get the left or right-hand set of values for this diff chunk.

Parameters:
string$sideleft or right-hand side.
null | int$indexoptional - the index of the value to get.
Returns:
array|string|null if no index is specified, returns an array of (zero or more) values - otherwise returns the value at $index (null if no value at index).
    {
        $values = !$this->isSame()
            ? $this->_values[($side == static::SIDE_LEFT ? 'd' : 'i')]
            : $this->_values;

        if ($index !== null) {
            return isset($values[$index]) ? $values[$index] : null;
        }

        return $values;
    }
P4Cms_Diff_Chunk::getChunkType ( )

Get the type of this chunk: same, insert, delete, change.

Returns:
string the type of this chunk.
    {
        $values = $this->_values;
        
        if (!isset($values['i'], $values['d'])) {
            return static::TYPE_SAME;
        }

        if (empty($values['d'])) {
            return static::TYPE_INSERT;
        }

        if (empty($values['i'])) {
            return static::TYPE_DELETE;
        }

        return static::TYPE_CHANGE;
    }
P4Cms_Diff_Chunk::getLeft ( index = null)

Get the left-hand values for this diff chunk.

Parameters:
null | int$indexoptional - the index of the value to get.
Returns:
array|string|null if no index is specified, returns an array of (zero or more) values - otherwise returns the value at $index (null if no value at index).
    {
        return $this->_getSide(static::SIDE_LEFT, $index);
    }
P4Cms_Diff_Chunk::getMaxValueCount ( )

Get the highest number of values in left/right.

Left and right may have a varying number of values; this will return whichever is greater.

Returns:
int the highest count of values in left/right.
    {
        return max(
            array(
                count($this->getLeft()),
                count($this->getRight())
            )
        );
    }
P4Cms_Diff_Chunk::getRawValues ( )

Get the raw values array.

Flat for chunks of unchanged values, multi-dimensional (deleted/inserted) for chunks of changed values.

Returns:
array raw values in this chunk.
    {
        return $this->_values;
    }
P4Cms_Diff_Chunk::getRight ( index = null)

Get the right-hand values for this diff chunk.

Parameters:
null | int$indexoptional - the index of the value to get.
Returns:
array|string|null if no index is specified, returns an array of (zero or more) values - otherwise returns the value at $index (null if no value at index).
    {
        return $this->_getSide(static::SIDE_RIGHT, $index);
    }
P4Cms_Diff_Chunk::getSubDiff ( index,
P4Cms_Diff_Options options = null 
)

Compare the left/right values at the specified index.

Useful for sub-line diffing. Splits on characters by default.

Parameters:
int$indexthe index of the left/right values to get.
P4Cms_Diff_Options$optionsoptions to augment comparison behavior.
Returns:
P4Cms_Diff_Result the result of the comparison.
    {
        // normalize options.
        $options = !is_null($options) ? $options : new P4Cms_Diff_Options;

        // set default split args (split on chars).
        if (!$options->getSplitArgs()) {
            $options->setSplitArgs(
                P4Cms_Diff_Options::PATTERN_CHARS,
                PREG_SPLIT_NO_EMPTY
            );
        }

        $diff  = new P4CMs_Diff;
        $left  = $this->getLeft($index);
        $right = $this->getRight($index);

        return $diff->compare($left, $right, $options);
    }
P4Cms_Diff_Chunk::isChange ( )

Check if this diff chunk is of type change.

Returns:
bool true if this is an change chunk; false otherwise.
    {
        return $this->getChunkType() === static::TYPE_CHANGE;
    }
P4Cms_Diff_Chunk::isDelete ( )

Check if this diff chunk is of type delete.

Returns:
bool true if this is an delete chunk; false otherwise.
    {
        return $this->getChunkType() === static::TYPE_DELETE;
    }
P4Cms_Diff_Chunk::isInsert ( )

Check if this diff chunk is of type insert.

Returns:
bool true if this is an insert chunk; false otherwise.
    {
        return $this->getChunkType() === static::TYPE_INSERT;
    }
P4Cms_Diff_Chunk::isSame ( )

Check if this diff chunk is of type same.

Returns:
bool true if this is an same chunk; false otherwise.
    {
        return $this->getChunkType() === static::TYPE_SAME;
    }
P4Cms_Diff_Chunk::isWhitespaceChange ( semantic = true)

Check if this diff chunk a whitespace only change.

Parameters:
bool$semanticoptional - defaults to true - consider semantic changes (e.g. splitting one word or joining two) a non-whitespace change.
Returns:
bool true if this chunk only differs by whitespace.
    {
        if ($this->isSame()) {
            return false;
        }

        // use the semantic flag to control whether we collapse
        // all whitespace or maintain word/line boundaries with a space.
        $semantic = $semantic ? " " : "";

        $left  = implode($semantic, $this->getLeft());
        $right = implode($semantic, $this->getRight());

        // normalize whitespace.
        $left  = trim(preg_replace("/\s*/", $semantic, $left));
        $right = trim(preg_replace("/\s*/", $semantic, $right));

        return $left == $right;
    }
P4Cms_Diff_Chunk::setRawValues ( array $  values)

Set the raw values array.

Flat for chunks of unchanged values, multi-dimensional (deleted/inserted) for chunks of changed values.

Parameters:
array$valuesoptional - flat array for chunks of unchanged values multi-dimensional deleted/inserted array for chunks of changed values.
Returns:
P4Cms_Diff_Chunk provides fluent interface.
    {
        $this->_values = $values;
    }

Member Data Documentation

P4Cms_Diff_Chunk::$_values = null [protected]

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