Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Diff Class Reference

Object oriented interface to Paul Butler's simple diff. More...

List of all members.

Public Member Functions

 compare ($left, $right, P4Cms_Diff_Options $options=null)
 Compare two values using the most appropriate comparison method for the value type.
 compareArrays (array $left, array $right, P4Cms_Diff_Options $options=null)
 Compare two arrays.
 compareBinaries ($left, $right, P4Cms_Diff_Options $options=null)
 Compare two binary values.
 compareModels (P4Cms_Model $left, P4Cms_Model $right, P4Cms_Diff_OptionsCollection $options=null)
 Compare two fielded models.
 compareStrings ($left, $right, P4Cms_Diff_Options $options=null)
 Compare two strings.

Detailed Description

Object oriented interface to Paul Butler's simple diff.

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

P4Cms_Diff::compare ( left,
right,
P4Cms_Diff_Options options = null 
)

Compare two values using the most appropriate comparison method for the value type.

If the values are of differing types, the left value will be cast to the type of the right.

Parameters:
mixed$leftthe left-hand input
mixed$rightthe right-hand input
P4Cms_Diff_Options$optionsoptions to augment comparison behavior.
Returns:
P4Cms_Diff_Result list of differences.
    {
        // normalize options.
        $options = !is_null($options) ? $options : new P4Cms_Diff_Options;
        
        // cast unsupported types to string.
        $types = array('array', 'string');
        $left  = !in_array(gettype($left),  $types) ? (string) $left  : $left;
        $right = !in_array(gettype($right), $types) ? (string) $right : $right;

        // ensure left/right types are the same.
        settype($left, gettype($right));
        
        // use appropriate comparison function.
        switch (gettype($right))
        {
            case 'string':
                if ($options->isBinaryDiff()) {
                    return $this->compareBinaries($left, $right, $options);
                } else {
                    return $this->compareStrings($left, $right, $options);
                }
            case 'array':
                return $this->compareArrays($left, $right, $options);
        }
    }
P4Cms_Diff::compareArrays ( array $  left,
array $  right,
P4Cms_Diff_Options options = null 
)

Compare two arrays.

Parameters:
array$leftthe left-hand array
array$rightthe right-hand array
P4Cms_Diff_Options$optionsoptions to augment comparison behavior.
Returns:
P4Cms_Diff_Result list of differences
    {
        // normalize options.
        $options = !is_null($options) ? $options : new P4Cms_Diff_Options;

        // run simplediff.
        $result = diff($left, $right);

        // simplediff seems to report empty difference blocks
        // (no deletion, no insertion), filter out these artifacts.
        $artifact = array('d' => array(), 'i' => array());
        $result   = array_filter(
            $result,
            function($diff) use ($artifact)
            {
                return $diff !== $artifact;
            }
        );
        
        return new P4Cms_Diff_Result($result, $options);
    }
P4Cms_Diff::compareBinaries ( left,
right,
P4Cms_Diff_Options options = null 
)

Compare two binary values.

Unlike compare strings, the left/right values are not exploded prior to calling compareArrays().

Parameters:
string$leftthe left-hand binary string
string$rightthe right-hand binary string
P4Cms_Diff_Options$optionsoptions to augment comparison behavior.
Returns:
P4Cms_Diff_Result list of differences
    {
        return $this->compareArrays(
            strlen($left)  ? array($left)  : array(),
            strlen($right) ? array($right) : array(),
            $options
        );
    }
P4Cms_Diff::compareModels ( P4Cms_Model left,
P4Cms_Model right,
P4Cms_Diff_OptionsCollection options = null 
)

Compare two fielded models.

Walks over every field in each model and compares the values. Puts results in a diff result collection.

Parameters:
P4Cms_Model$leftthe left hand model
P4Cms_Model$rightthe right-hand model
P4Cms_Diff_OptionsCollection$optionsper-field options to augment compare.
Returns:
P4Cms_Diff_ResultCollection collection with one diff result per field.
    {
        // normalize options to a collection.
        $options = !is_null($options) ? $options : new P4Cms_Diff_OptionsCollection;

        // get list of all unique fields across models.
        $fields = array_unique(array_merge($left->getFields(), $right->getFields()));

        // compare each field value.
        $results = array();
        foreach ($fields as $field) {

            // normalize field options.
            $fieldOptions = isset($options[$field]) 
                ? $options[$field]
                : new P4Cms_Diff_Options;

            // skip diffing undesired fields
            if ($fieldOptions->isSkipped()) {
                continue;
            }

            // do the compare.
            $results[$field] = $this->compare(
                $left->hasField($field)  ? $left->getValue($field)  : null,
                $right->hasField($field) ? $right->getValue($field) : null,
                $fieldOptions
            );
        }

        return new P4Cms_Diff_ResultCollection($results, $options);
    }
P4Cms_Diff::compareStrings ( left,
right,
P4Cms_Diff_Options options = null 
)

Compare two strings.

Splits on lines by default.

Parameters:
string$leftthe left-hand string
string$rightthe right-hand string
P4Cms_Diff_Options$optionsoptions to augment comparison behavior.
Returns:
P4Cms_Diff_Result list of differences
    {
        // normalize options.
        $options = !is_null($options) ? $options : new P4Cms_Diff_Options;

        // determine what pattern and flags to use to split input strings.
        $args  = is_array($options->getSplitArgs())
            ? $options->getSplitArgs()
            : array(P4Cms_Diff_Options::PATTERN_LINES, 0);

        $left  = preg_split($args[0], $left, -1, $args[1]);
        $right = preg_split($args[0], $right, -1, $args[1]);

        return $this->compareArrays($left, $right, $options);
    }

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