Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Record_RegisteredType Class Reference

This model provides a way to register record implementations (sub-classes) with the system as named record 'types' via pub/sub: More...

Inheritance diagram for P4Cms_Record_RegisteredType:
P4Cms_Model P4Cms_ModelInterface

List of all members.

Public Member Functions

 fetchRecord ($id, $options=null, P4Cms_Record_Adapter $adapter=null)
 Get a specific record by id using the types record class.
 getRecordClass ()
 Get the record class name for this type.
 getUri ($id, $action= 'view', $params=array())
 Get the URI for the given id of this type.
 getUriCallback ()
 Returns the current URI callback if one has been set.
 hasUriCallback ()
 Determines if a valid URI callback has been set.
 isValid ()
 Verifies this type has specified a class and id.
 setRecordClass ($recordClass)
 Set the record class name for this type.
 setUriCallback ($function)
 Set the function to use when generating URI's for this type.

Static Public Member Functions

static fetch ($id)
 Get a specific record type.
static fetchAll ()
 Get all of the record types that have been registered.

Static Protected Attributes

static $_fields

Detailed Description

This model provides a way to register record implementations (sub-classes) with the system as named record 'types' via pub/sub:

p4cms.record.registeredTypes

The subscribed callback should return one or more configured P4Cms_Record_RegisteredType instances. Each registered type needs a unique type identifier. It must also set the name of the concrete record class it is representing.

The primary goal of this infrastructure is to allow for a record to be fetched given only a record type and entry id (e.g. type: 'content', id: '123').

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

static P4Cms_Record_RegisteredType::fetch ( id) [static]

Get a specific record type.

Parameters:
string$idthe id of the type to get.
Returns:
P4Cms_Record_RegisteredType the registered record type.
Exceptions:
P4Cms_Model_NotFoundExceptionif an unknown type is requested.
    {
        $types = static::fetchAll();

        // verify requested type is registered.
        if (!isset($types[$id])) {
            throw new P4Cms_Model_NotFoundException(
                "Cannot fetch record type. The requested type is not registered."
            );
        }

        return $types[$id];
    }
static P4Cms_Record_RegisteredType::fetchAll ( ) [static]

Get all of the record types that have been registered.

Types that do not pass the isValid() test, are ignored.

Returns:
P4Cms_Model_Iterator all registered record types.

p4cms.record.registeredTypes Return a P4Cms_Record_RegisteredType (or array of Registered Types) to be included in the registered type fetchAll results. The last subscriber to return a given ID wins. If an ID is specified but the type is invalid, this is considered a request to remove any previously registered type with that ID.

    {
        $types    = new P4Cms_Model_Iterator;
        $feedback = P4Cms_PubSub::publish('p4cms.record.registeredTypes');
        foreach ($feedback as $providedTypes) {
            if (!is_array($providedTypes)) {
                $providedTypes = array($providedTypes);
            }

            foreach ($providedTypes as $type) {
                // skip clearly incorrect entries
                if (!$type instanceof P4Cms_Record_RegisteredType) {
                    continue;
                }

                // if they specified an id but the type is not valid
                // (e.g. does not specify a record class), consider this
                // a request to remove a previously registered type.
                if (!$type->isValid() && strlen($type->getId())) {
                    unset($types[$type->getId()]);
                }

                // regardless of id, skip over invalid types at this point
                if (!$type->isValid()) {
                    continue;
                }

                $types[$type->getId()] = $type;
            }
        }

        return $types;
    }
P4Cms_Record_RegisteredType::fetchRecord ( id,
options = null,
P4Cms_Record_Adapter adapter = null 
)

Get a specific record by id using the types record class.

Parameters:
string | int$idthe id of the record to fetch.
P4Cms_Record_Query | array | null$optionsoptional - query options to augment result.
P4Cms_Record_Adapter$adapteroptional - storage adapter to use.
Returns:
P4Cms_Record the requested record.
Exceptions:
P4Cms_Record_NotFoundExceptionif the requested record can't be found
P4Cms_Model_Exceptionif no record class has been set on this type
    {
        if ($this->getRecordClass() === null) {
            throw new P4Cms_Model_Exception(
                'Cannot fetch entry, no record class has been specified.'
            );
        }

        return call_user_func_array(
            array($this->getRecordClass(), 'fetch'),
            array($id, $options, $adapter)
        );
    }
P4Cms_Record_RegisteredType::getRecordClass ( )

Get the record class name for this type.

Returns:
string the record class name for this type.
    {
        return $this->_getValue('recordClass');
    }
P4Cms_Record_RegisteredType::getUri ( id,
action = 'view',
params = array() 
)

Get the URI for the given id of this type.

If an action is provided, will return a URI to perform the given action.

Parameters:
int | string$idthe id of the entry.
string$actionoptional - action to perform - defaults to 'view'.
array$paramsoptional - additional params to add to the uri.
Returns:
string the uri of the content entry.
    {
        return call_user_func($this->getUriCallback(), $id, $action, $params);
    }
P4Cms_Record_RegisteredType::getUriCallback ( )

Returns the current URI callback if one has been set.

Returns:
callback The current URI callback.
Exceptions:
P4Cms_Record_ExceptionIf no URI callback has been set.
    {
        if (!$this->hasUriCallback()) {
            throw new P4Cms_Record_Exception(
                'Cannot get URI callback, no URI callback has been set.'
            );
        }

        return $this->_getValue('uriCallback');
    }
P4Cms_Record_RegisteredType::hasUriCallback ( )

Determines if a valid URI callback has been set.

Returns:
bool True if valid URI callback set, False otherwise.
    {
        return is_callable($this->_getValue('uriCallback'));
    }
P4Cms_Record_RegisteredType::isValid ( )

Verifies this type has specified a class and id.

Returns:
bool true if type is valid; false otherwise.
    {
        return $this->getRecordClass() !== null && strlen($this->getId());
    }
P4Cms_Record_RegisteredType::setRecordClass ( recordClass)

Set the record class name for this type.

Parameters:
string | P4Cms_Record | null$recordClassthe class to use or null to clear.
Returns:
P4Cms_Record_RegisteredType provides fluent interface.
    {
        // normalize any passed objects to a string
        if (is_object($recordClass)) {
            $recordClass = get_class($recordClass);
        }

        // verify the passed class is of the correct heritage or null
        if ($recordClass != 'P4Cms_Record'
            && !is_subclass_of($recordClass, 'P4Cms_Record')
            && $recordClass !== null
        ) {
            throw new InvalidArgumentException(
                'Record class must be a string, null or P4Cms_Record instance.'
            );
        }

        $this->_setValue('recordClass', $recordClass);

        return $this;
    }
P4Cms_Record_RegisteredType::setUriCallback ( function)

Set the function to use when generating URI's for this type.

Parameters:
null | callback$functionThe callback function for URI generation. The function should expect three parameters:

  • $id (Record ID)
  • $action (string)
  • $params (array) Returns a string (the uri).
Returns:
P4Cms_Record_RegisteredType provides fluent interface.
    {
        if (!is_callable($function) && $function !== null) {
            throw new InvalidArgumentException(
                'Cannot set URI callback. Expected a callable function or null.'
            );
        }

        return $this->_setValue('uriCallback', $function);
    }

Member Data Documentation

P4Cms_Record_RegisteredType::$_fields [static, protected]
Initial value:
 array(
        'recordClass'   => array(
            'accessor'  => 'getRecordClass',
            'mutator'   => 'setRecordClass'
        ),
        'uriCallback'   => array(
            'accessor'  => 'getUriCallback',
            'mutator'   => 'setUriCallback'
        )
    )

Reimplemented from P4Cms_Model.


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