Hoa central
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes | Private Member Functions | Private Attributes | List of all members
Hoa\Model\Model Class Reference
Inheritance diagram for Hoa\Model\Model:

Public Member Functions

 __construct ()
 
 openMany (Array $constraints=array())
 
 open (Array $constraints=array())
 
 save ()
 
 __isset ($name)
 
 __set ($name, $value)
 
 __get ($name)
 
 offsetExists ($offset)
 
 offsetSet ($offset, $value)
 
 offsetGet ($offset)
 
 offsetUnset ($offset)
 
 getIterator ()
 
 count ()
 
 setEnableValidation ($enable)
 
 isValidationEnabled ()
 
 toArray ()
 

Static Public Member Functions

static getMappingLayer ($name= '_default')
 

Protected Member Functions

 construct ()
 
 map (Array $data, Array $map=null)
 
 mapRelation ($name, Array $data, Array $map=null)
 
 getConstraints ($defaultValues=false)
 

Static Protected Member Functions

static setMappingLayer ($layer, $name= '_default')
 

Static Protected Attributes

static $__mappingLayer = array('_default' => null)
 

Private Member Functions

 _offsetExists ($offset)
 
getAttribute ($name)
 

Private Attributes

 $__validation = true
 
 $__attributes = array()
 
 $__currentAccess = null
 

Detailed Description

Class .

Represent a model/document with attributes and relations.

Author
Ivan Enderlin ivan..nosp@m.ende.nosp@m.rlin@.nosp@m.hoa-.nosp@m.proje.nosp@m.ct.n.nosp@m.et

Definition at line 65 of file Model.php.

Constructor & Destructor Documentation

Hoa\Model\Model::__construct ( )
final

Initialize the model.

public

Returns
void

Definition at line 107 of file Model.php.

107  {
108 
109  $class = new \ReflectionClass($this);
110  $ucfirst = function ( Array $matches ) {
111 
112  return ucfirst($matches[1]);
113  };
114  $default = $class->getDefaultProperties();
115 
116  foreach($class->getProperties() as $property) {
117 
118  $_name = $property->getName();
119 
120  if( '_' !== $_name[0]
121  || '_' === $_name[1])
122  continue;
123 
124  $name = substr($_name, 1);
125  $comment = $property->getDocComment();
126  $comment = preg_replace('#^(\s*/\*\*\s*)#', '', $comment);
127  $comment = preg_replace('#(\s*\*/)#', '', $comment);
128  $comment = preg_replace('#^(\s*\*\s*)#m', '', $comment);
129  $relation = false !== strpos($comment, 'relation(');
130  $validator = 'validate' . ucfirst(preg_replace_callback(
131  '#_(.)#',
132  $ucfirst,
133  strtolower($name)
134  ));
135 
136  $this->__attributes[$name] = array(
137  'comment' => $comment ?: false,
138  'name' => $name,
139  '_name' => $_name,
140  'validator' => method_exists($this, $validator)
141  ? $validator
142  : null,
143  'contract' => null, // be lazy
144  'default' => $default[$_name],
145  'value' => &$this->$_name,
146  'relation' => $relation
147  );
148 
149  if(true === $relation && empty($this->$_name))
150  $this->$_name = array();
151  }
152 
153  $this->construct();
154 
155  return;
156  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Model\Model::__get (   $name)

Get an attribute value.

public

Parameters
string$nameName.
Returns
mixed

Definition at line 400 of file Model.php.

400  {
401 
402  if(!isset($this->$name))
403  return null;
404 
405  $attribute = &$this->getAttribute($name);
406 
407  if(true === $attribute['relation']) {
408 
409  $this->__currentAccess = '_' . $name;
410 
411  return $this;
412  }
413  elseif(null !== $this->__currentAccess)
414  $this->__currentAccess = null;
415 
416  return $this->{'_' . $name};
417  }
if(true===function_exists('mb_internal_encoding')) if(true===function_exists('mb_regex_encoding')) return
Definition: Core.php:181
& getAttribute($name)
Definition: Model.php:587

Here is the call graph for this function:

Hoa\Model\Model::__isset (   $name)

Check if an attribute exists and is set.

public

Parameters
string$nameAttribute name.
Returns
bool

Definition at line 312 of file Model.php.

312  {
313 
314  return array_key_exists($name, $this->__attributes);
315  }
Hoa\Model\Model::__set (   $name,
  $value 
)

Set a value to an attribute.

public

Parameters
string$nameName.
mixed$valueValue.
Returns
void
Exceptions

Definition at line 326 of file Model.php.

326  {
327 
328  if(!isset($this->$name))
329  return null;
330 
331  $_name = '_' . $name;
332  $attribute = &$this->getAttribute($name);
333 
334  if(true === $attribute['relation']) {
335 
336  if(!is_array($value))
337  $value = array($value);
338 
339  $this->__currentAccess = $_name;
340 
341  foreach($value as $k => $v)
342  $this->offsetSet($k, $v);
343 
344  $this->__currentAccess = null;
345 
346  return;
347  }
348 
349  if(is_numeric($value)) {
350 
351  if($value == $_value = (int) $value)
352  $value = $_value;
353  else
354  $value = (float) $value;
355  }
356 
357  if(false === $this->isValidationEnabled()) {
358 
359  $old = $this->$_name;
360  $this->$_name = $value;
361 
362  return $old;
363  }
364 
365  if(false !== $attribute['comment']) {
366 
367  if(null === $attribute['contract'])
368  $attribute['contract'] = \Hoa\Praspel::interpret($attribute['comment']);
369 
370  $verdict = $attribute['contract']
371  ->getClause('invariant')
372  ->getVariable($name)
373  ->predicate($value);
374 
375  if(false === $verdict)
376  throw new Exception(
377  'Try to set the %s attribute with an invalid data.',
378  0, $name);
379  }
380 
381  if( (null !== $validator = $attribute['validator'])
382  && false === $this->{$validator}($value))
383  throw new Exception(
384  'Try to set the %s attribute with an invalid data.',
385  1, $name);
386 
387  $old = $this->$_name;
388  $this->$_name = $value;
389 
390  return;
391  }
offsetSet($offset, $value)
Definition: Model.php:458
isValidationEnabled()
Definition: Model.php:620
& getAttribute($name)
Definition: Model.php:587

Here is the call graph for this function:

Hoa\Model\Model::_offsetExists (   $offset)
private

Check if an offset exists on an attribute.

public

Parameters
int$offsetOffset.
Returns
bool

Definition at line 426 of file Model.php.

426  {
427 
428  if(null === $this->__currentAccess)
429  return false;
430 
431  return array_key_exists($offset, $this->{$this->__currentAccess});
432  }

Here is the caller graph for this function:

Hoa\Model\Model::construct ( )
protected

User constructor.

protected

Returns
void

Definition at line 164 of file Model.php.

164  {
165 
166  return;
167  }

Here is the caller graph for this function:

Hoa\Model\Model::count ( )

Count number of attributes.

public

Returns
int

Definition at line 569 of file Model.php.

569  {
570 
571  if(null === $this->__currentAccess)
572  return count($this->__attributes);
573 
574  $out = count($this->{$this->__currentAccess});
575  $this->__currentAccess = null;
576 
577  return $out;
578  }
& Hoa\Model\Model::getAttribute (   $name)
private

Get an attribute in the bucket.

public

Parameters
string$nameName.
Returns
array

Definition at line 587 of file Model.php.

587  {
588 
589  if(!isset($this->$name)) {
590 
591  $out = null;
592 
593  return $out;
594  }
595 
596  return $this->__attributes[$name];
597  }

Here is the caller graph for this function:

Hoa\Model\Model::getConstraints (   $defaultValues = false)
protected

Get constraints, i.e. all attributes values that defer from their default values. We could include the default values if the only argument is set to true.

public

Parameters
bool$defaultValuesWhether we include default values.
Returns
array

Definition at line 292 of file Model.php.

292  {
293 
294  $out = array();
295 
296  foreach($this->__attributes as $name => $attribute)
297  if( false === $attribute['relation']
298  && (true === $defaultValues
299  || $attribute['default'] !== $attribute['value']))
300  $out[$name] = $attribute['value'];
301 
302  return $out;
303  }
Hoa\Model\Model::getIterator ( )

Iterate a relation.

public

Returns

Definition at line 555 of file Model.php.

555  {
556 
557  if(null === $this->__currentAccess)
558  return null;
559 
560  return new \ArrayIterator($this->{$this->__currentAccess});
561  }
static Hoa\Model\Model::getMappingLayer (   $name = '_default')
static

Get a mapping layer.

public

Parameters
string$nameName.
Returns
object

Definition at line 652 of file Model.php.

652  {
653 
654  if(!array_key_exists($name, static::$__mappingLayer))
655  return null;
656 
657  return static::$__mappingLayer[$name];
658  }
Hoa\Model\Model::isValidationEnabled ( )

Check if validation is enabled or not.

public

Returns
bool

Definition at line 620 of file Model.php.

620  {
621 
622  return $this->__validation;
623  }

Here is the caller graph for this function:

Hoa\Model\Model::map ( Array  $data,
Array  $map = null 
)
protected

Map data to attributes.

public

Parameters
array$dataData.
array$mapMap: data name to attribute name.
Returns

Definition at line 212 of file Model.php.

212  {
213 
214  if(null !== $this->__currentAccess)
215  return $this->mapRelation(
216  substr($this->__currentAccess, 1),
217  $data,
218  $map
219  );
220 
221  if(empty($map))
222  $map = array_combine($handle = array_keys($data), $handle);
223 
224  foreach($data as $name => $value) {
225 
226  if(array_key_exists($name, $map))
227  $name = $map[$name];
228 
229  if($value == $this->$name)
230  continue;
231 
232  $this->$name = $value;
233  }
234 
235  return $this;
236  }
mapRelation($name, Array $data, Array $map=null)
Definition: Model.php:247

Here is the call graph for this function:

Hoa\Model\Model::mapRelation (   $name,
Array  $data,
Array  $map = null 
)
protected

Map data to a relation attribute.

public

Parameters
array$nameRelation name.
array$dataData.
array$mapMap: data name to attribute name.
Returns

Definition at line 247 of file Model.php.

247  {
248 
249  if(!isset($this->$name))
250  throw new Exception(
251  'Cannot map relation %s because it does not exist.', 42, $name);
252 
253  $attribute = &$this->getAttribute($name);
254 
255  if(true !== $attribute['relation'])
256  throw new Exception(
257  'Cannot map relation %s because it is not a relation.',
258  43, $name);
259 
260  if(null === $attribute['contract'])
261  $attribute['contract'] = \Hoa\Praspel::interpret($attribute['comment']);
262 
263  $realdom = $attribute['contract']
264  ->getClause('invariant')
265  ->getVariable($name)
266  ->getDomains()
267  ->offsetGet(0);
268  $classname = $realdom['classname']->getConstantValue();
269  $_name = '_' . $name;
270 
271  foreach($data as $i => $d) {
272 
273  $this->__currentAccess = $_name;
274  $class = new $classname();
275  $this->offsetSet($i, $class->map($d, $map));
276  }
277 
278  $this->__currentAccess = null;
279 
280  return $this;
281  }
offsetSet($offset, $value)
Definition: Model.php:458
& getAttribute($name)
Definition: Model.php:587

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Model\Model::offsetExists (   $offset)

Check if an offset exists on an attribute.

public

Parameters
int$offsetOffset.
Returns
bool

Definition at line 441 of file Model.php.

441  {
442 
443  $out = $this->_offsetExists($offset);
444  $this->__currentAccess = null;
445 
446  return $out;
447  }
_offsetExists($offset)
Definition: Model.php:426

Here is the call graph for this function:

Hoa\Model\Model::offsetGet (   $offset)

Get a value from a specific offset of the current attribute.

public

Parameters
int$offsetOffset.
Returns
mixed

Definition at line 521 of file Model.php.

521  {
522 
523  if(false === $this->_offsetExists($offset))
524  return $this->__currentAccess = null;
525 
526  $out = &$this->{$this->__currentAccess}[$offset];
527  $this->__currentAccess = null;
528 
529  return $out;
530  }
_offsetExists($offset)
Definition: Model.php:426

Here is the call graph for this function:

Hoa\Model\Model::offsetSet (   $offset,
  $value 
)

Set a value to a specific offset of the current attribute.

public

Parameters
int$offsetOffset.
mixed$valueValue.
Returns
bool
Exceptions

Definition at line 458 of file Model.php.

458  {
459 
460  if(false === $this->isValidationEnabled()) {
461 
462  $this->{$this->__currentAccess}[$offset] = $value;
463 
464  return null;
465  }
466 
467  $oldOffset = false !== $this->_offsetExists($offset)
468  ? $this->{$this->__currentAccess}[$offset]
469  : null;
470 
471  $this->{$this->__currentAccess}[$offset] = $value;
472 
473  $name = substr($this->__currentAccess, 1);
474  $attribute = &$this->getAttribute($name);
475 
476  if(false !== $attribute['comment']) {
477 
478  if(null === $attribute['contract'])
479  $attribute['contract'] = \Hoa\Praspel::interpret($attribute['comment']);
480 
481  $verdict = $attribute['contract']
482  ->getClause('invariant')
483  ->getVariable($name)
484  ->predicate($this->{$this->__currentAccess});
485  }
486  else
487  $verdict = true;
488 
489  if(false === $verdict) {
490 
491  if(null !== $oldOffset)
492  $this->{$this->__currentAccess}[$offset] = $oldOffset;
493  else
494  unset($this->{$this->__currentAccess}[$offset]);
495 
496  throw new Exception(
497  'Try to set the %s attribute with an invalid data.', 2, $name);
498  }
499 
500  if( (null !== $validator = $attribute['validator'])
501  && false === $this->{$validator}($value)) {
502 
503  if(null !== $oldOffset)
504  $this->{$this->__currentAccess}[$offset] = $oldOffset;
505 
506  throw new Exception(
507  'Try to set the %s attribute with an invalid data.',
508  3, $name);
509  }
510 
511  return $this->__currentAccess = null;
512  }
_offsetExists($offset)
Definition: Model.php:426
isValidationEnabled()
Definition: Model.php:620
& getAttribute($name)
Definition: Model.php:587

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Model\Model::offsetUnset (   $offset)

Unset a specific offset of the current attribute.

public

Parameters
int$offsetOffset.
Returns
void

Definition at line 539 of file Model.php.

539  {
540 
541  if(false !== $this->_offsetExists($offset))
542  unset($this->__currentAccess[$offset]);
543 
544  $this->__currentAccess = null;
545 
546  return;
547  }
_offsetExists($offset)
Definition: Model.php:426

Here is the call graph for this function:

Hoa\Model\Model::open ( Array  $constraints = array())

Open one document.

public

Parameters
array$constraintsContraints.
Returns
void

Definition at line 188 of file Model.php.

188  {
189 
190  return;
191  }
Hoa\Model\Model::openMany ( Array  $constraints = array())

Open many documents.

public

Parameters
array$constraintsContraints.
Returns
void

Definition at line 176 of file Model.php.

176  {
177 
178  return;
179  }
Hoa\Model\Model::save ( )

Save the document.

public

Returns
void

Definition at line 199 of file Model.php.

199  {
200 
201  return;
202  }
Hoa\Model\Model::setEnableValidation (   $enable)

Enable validation or not (i.e. execute Praspel and validate*() methods).

public

Parameters
bool$enableEnable or not.
Returns
bool

Definition at line 606 of file Model.php.

606  {
607 
608  $old = $this->__validation;
609  $this->__validation = $enable;
610 
611  return $old;
612  }
static Hoa\Model\Model::setMappingLayer (   $layer,
  $name = '_default' 
)
staticprotected

Set a mapping layer.

public

Parameters
object$layerLayer (e.g. ).
string$nameName.
Returns
object

Definition at line 633 of file Model.php.

633  {
634 
635  $old = null;
636 
637  if(array_key_exists($name, static::$__mappingLayer))
638  $old = static::$__mappingLayer[$name];
639 
640  static::$__mappingLayer[$name] = $layer;
641 
642  return $old;
643  }
Hoa\Model\Model::toArray ( )

Transform data as an array.

public

Returns
array

Implements Hoa\Core\Data\Datable.

Definition at line 666 of file Model.php.

666  {
667 
668  if(null === $this->__currentAccess) {
669 
670  $out = array();
671 
672  foreach($this->__attributes as $attribute)
673  if(false === $attribute['relation'])
674  $out[$attribute['name']] = $attribute['value'];
675 
676  return $out;
677  }
678 
679  $out = array();
680 
681  foreach($this->{$this->__currentAccess} as $i => $value)
682  $out[$i] = $value->toArray();
683 
684  $this->__currentAccess = null;
685 
686  return $out;
687  }

Member Data Documentation

Hoa\Model\Model::$__attributes = array()
private

Definition at line 83 of file Model.php.

Hoa\Model\Model::$__currentAccess = null
private

Definition at line 90 of file Model.php.

Hoa\Model\Model::$__mappingLayer = array('_default' => null)
staticprotected

Definition at line 97 of file Model.php.

Hoa\Model\Model::$__validation = true
private

Definition at line 76 of file Model.php.


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