webCOMAND

COMAND Content Type Classes

By default, objects retrieved from the repository will be an instance of the COMAND object class, which provides access to the fields and methods defined in the repository, as if a PHP class was defined with the same fields and methods. While this works well in many situations, it is often useful to define a content type class to represent an object loaded from the repository, providing content type specific PHP variables and methods.

A content type class is a standard PHP class file that meets the following criteria.

  • Namespace - The class file namespace should always be \comand\classes\content_type.
  • Class Name - The class name must match the identifier of the corresponding content type. PHP class names are not case sensitive. However, it is a best practice to match the case as well.
  • Extends - The class must extend the \comand\repository\object class.
  • Filename - The filename must match the identifier of the corresponding content type. Filenames are not case-sensitive on all operating systems. However, it is a best practice to match the case, even when the operating system does not require it.
  • File Path - The file should be saved to a valid Content Type Class folder.

Content Type Class Folders

Content Type Class files can be saved in two different types of folders.

Application Content Type Classes Folder

The application classes folder makes it easy to add Content Type Classes that will be automatically detected and used when objects of a given Content Type are loaded from the repository. The folder can be customized, but is "application/classes" by default.

Package Content Type Classes Folders

In addition to the Application Classes folder, Content Type Class files can also be located in a folder within a package, which is useful when Content Type Class are provided as part of a package, or if a Content Type Class should only be utilized for a specific Content Type namespace. In other words, if multiple Content Types with the same Identifier exist, the Content Type's Namespace and Package can be used to associate those content types with their own unique Content Type Class files.

Simple Example

The following example PHP class will be used for objects of a content type with the identifier "Contact".

<?php

namespace comand\classes\content_type

/**
 * Class to be used for objects loaded from a COMAND repository with
 * the content type Contact.
 */
class Contact extends \comand\repository\object {
	/**
	 * Custom method to print the name of a Contact.
	 */
	function print_name() {
		print( "$this->FirstName $this->LastName" );
	}
}

Hooks

In addition to the definition of arbitrary custom variables and methods, COMAND hooks can also be defined for COMAND to utilize in difference scenarios.

Field Validation

Field validations can be defined as an associative array named $_field_validations with keys that correspond to field identifiers and the value is an array of functions to call sequentially to validate the field value upon submission. If the function names are given as the key to an associative array value, the value represents the parameter or an array of parameters. The provided function names will be processed as follows:

  • Class _validate_ - Check for method in this class with same name preceded by "_validate_" (ie. "email" would check for "_validate_email").
  • Class _prepare_ - Check for method in this class with same name preceded by "_prepare_", which can alter the field value before it is saved (returns altered value).
  • Extension _validate_ - Check for method in loaded extensions (last loaded checked first) with the same name preceded by "_validate_".
  • Extension _prepare_ - Check for method in loaded extensions (last loaded checked first) with the same name preceded by "_prepare_".
  • Built-in PHP Function - Check for standard PHP function and use it to alter the field value, like "_prepare_" methods (ie. trim).
Field validations are processed between after field value type validation occurs and before the Validate method is processed.

Field Validation Example

<?php

namespace comand\classes\content_type

/**
 * Class to be used for objects loaded from a COMAND repository with
 * the content type Contact.
 */
class Contact extends \comand\repository\object
{
	public static $_field_validations = array(
		'FirstName'=>array( 'trim', 'required', 'min_length'=>10 )
	);
}

Recognized Methods

A complete list of methods utilized by COMAND, see recognized content type methods.

Example

The following example PHP class will be used for objects of a content type with the identifier "Contact".

<?php

namespace comand\classes\content_type

/**
 * Class to be used for objects loaded from a COMAND repository with
 * the content type Contact.
 */
class Contact extends \comand\repository\object
{
	/**
	 * This method will be called when a new Contact object is created.
	 */
	function New() {
		// set a default FirstName field value
		$this->FirstName = 'John';

		// do whatever the content type Contact would normally do,
		// which includes calling New() on content types it extends.
		return parent::New();
	}
}

Example Template

New content type classes can be created based on the following example skeleton.

<?php

namespace comand\classes\content_type

/**
 * Class to be used for objects loaded from a COMAND repository with
 * the content type Contact.
 */
class Contact extends \comand\repository\object
{
	public static $_field_validations = array(
		'Name'=>array( 'Trim', 'Required', 'MinLength'=>10 )
	);

	/**
	 * This method will be called when a new Contact object is created.
	 */
	function New() {
		// set a default FirstName field value
		$this->FirstName = 'John';

		// do whatever the content type Contact would normally do,
		// which includes calling New() on content types it extends.
		return parent::New();
	}
}