Skip to content
comhon-project edited this page Aug 24, 2017 · 31 revisions

Table of content

  1. How it works ?
  2. Model
    1. Simple Model
    2. Complex Model
    3. Container Model
  3. ModelManager
  4. Comhon Object
    1. Object Unique
    2. Object Array
    3. Extends from Object
    4. Comhon DateTime Object
  5. Code Example

How it works ?

Comhon! has two main concepts to understand : Object and Model.

  • A Model contain properties
    • A property has a name and a type
    • A type can be (string, boolean, another model ...)
  • An Object contain values and a model
    • Values is a map of values [a_property_name => a_value]

In other words map of values replace class attributes and model properties replace attributes types.

For exemple, a Model Person might have properties first_name, last_name, age ... and an Object with model Person might have values [first_name => 'John', last_name => 'Doe', age => 21]

Model

Simple Model

list of simple model managed :

  • boolean
  • integer
  • index
  • float
  • string
  • dateTime
  • percentage

Complex Model

A complex model is described by a manifest (please take a look at Manifest page). It contain a map of properties and each property has it own model

Model Array

ModelArray is a specific model that you will use when you will instanciate an ObjectArray. ModelArray is actually a container that contain it own Model (could be simple model or complex model).
You have to specify the name of each array element. For example in a model Person we should have a property children and each element should have the name child.

$modelArray = new ModelArray($model, 'element_name'); // $model is an instanciated simple model or complex model

ModelManager

Instanciating models has a cost (due to manifest loading) so we need to instanciate them only one time. The singleton ModelManager manage all models instances so we don't have to know if model is already instanciated or not.

To get an instance of a model you just have to call getInstanceModel() :

$model = ModelManager::getInstance()->getInstanceModel('a_model_name');

Comhon Object

First, we have to describe how different object classes are organized

                            ComhonObject (abstract)
                       __________|__________
                      |                     |
            ObjectUnique (abstract)    ObjectArray (final)
            __________|__________
           |                     |
      Object (final)          ExtendableObject (abstract)

Object Unique

Like we said before, an Object contain a Model so when you instanciate an Object you have to specify an instance of Model or a model name.

$model = ModelManager::getInstance()->getInstanceModel('a_model_name');
$object = new Object($model);
// or
$object = new Object('a_model_name');

you can also use model and call function getObjectInstance().

$model = ModelManager::getInstance()->getInstanceModel('a_model_name');
$object = $model->getObjectInstance();
// return an instance of Object if no associated class
// return an instance of ExtendableObject otherwise

To verify if object has a value set you have to call function hasValue :

$object->hasValue('property_name'); // return boolean

To access value you have to call function getValue :

$object->getValue('property_name'); // return null if object doesn't have specified value set

To set a value you have to call function setValue :

$object->setValue('property_name', a_value);

To get a new instance of complex property you can call function getInstanceValue :

$complexValue = $object->getInstanceValue('property_name'); // return instance of ComhonObject

To initialize (instanciate and affect) a complex value you can call function initValue :

$complexValue = $object->initValue('property_name'); // return instance of ComhonObject

Object Array

in Comhon! framework, arrays are not simple php arrays. Arrays are objects that contain a ModelArray. An ObjectArray is like a typed array and implements Iterator.
You have four ways to instanciate an ObjectArray yourself :

// 1. by specifying a ModelArray 
$model = ModelManager::getInstance()->getInstanceModel('a_model_name');
$modelArray = new ModelArray($model, 'element_name');
$objectArray = new ObjectArray($modelArray);

// 2. by specifying a Model (the ModelArray is instanciated automaticaly)
$model = ModelManager::getInstance()->getInstanceModel('a_model_name');
$objectArray = new ObjectArray($model);

// 3. by specifying a Model name (the ModelArray is instanciated automaticaly)
$objectArray = new ObjectArray('a_model_name');

// 4. by using ModelArray
$objectArray = $modelArray->getObjectInstance(); // $modelArray is an instanciated ModelArray

To access value you have to call getter

$objectArray->getValue('a_key_or_index');

To set a value you have to call setter

$objectArray->setValue('index_or_key', a_value); // set element in values at specified index/key
$objectArray->pushValue(a_value); // Push one element onto the end of values
$objectArray->unshiftValue(a_value); // Prepend one element to the beginning of values

Extends from Object

if you want to associate a class to your object you can create one and define it in your manifest (see Manifest page). Your class must extends from ExtendableObject class and contain _getModelName method that return model name associated to your class.

use Comhon\Object\ExtendableObject;

class Person Extends ExtendableObject {

  protected function _getModelName() {
    return 'person';
  }

  public function getName() {
    return $this->getValue('name');
  }
  
  public function setAge($age) {
    $this->setValue('age', $age);
  }
}

Now to get automatically an instance of Person you can use model and call function getObjectInstance().

$personModel = ModelManager::getInstance()->getInstanceModel('Person');
$person = $personModel->getObjectInstance(); // return an instance of Person if you have defined it in manifest

Comhon DateTime Object

ComhonDateTime is the Object linked to model dateTime. ComhonDateTime extends from DateTime.
For example if we have defined a property birth_date with type dateTime on model person:

$dateTime = new ComhonDateTime('1976-03-20 07:14:33');
// set ComhonDateTime value
$person->setValue('birth_date', $dateTime);

Code Example

// first way to instanciate a Comhon object
$personModel = ModelManager::getInstance()->getInstanceModel('person');
$person = $personModel->getObjectInstance();

// second way to instanciate a Comhon object
$person = new Object('person');

// third way to instanciate a comhon object only if you have defined a class
$person = new Person();

// set Comhon object values
$person->setValue('age', 21);
$person->setValue('foo', 'bar'); // will not work because person doesn't have property 'foo'

// get Comhon object value
$age = $person->getValue('age');

Clone this wiki locally