Class Ismo_Core_Object_Plain

Description

This is the Ismo_Core_Object_Plain superclass, you should subclass your own classes from this one.

This is class is here to make life easy for you, as most of ismo is. By subclassing from this class you get quite many nice benefits:

  • You can automatically fill in an object based on the data available form e.g. a submitted form or another object or some other source with the setFrom() method.
  • Class attributes which names are listed in the _private array are private attributes and they won't be considered when setting the attributes with the setFrom() method.
  • You can make validation methods for each attribute and the object can then be validated with the validate() method.
Example:

  • abstract:
  • access: public
  • author: Joakim Andersson <ja@morrdusk.net>
  • todo:

    update the examples.

    1. class MyObject extends Ismo_Core_Object_Plain
    2. {
    3. var $_private = array("id");
    4.  
    5. var $id;
    6. var $name;
    7. var $phoneNo;
    8.  
    9. function validateName()
    10. {
    11. if (strcmp($this->name, "valid") == 0) {
    12. return true;
    13. }
    14. else {
    15. return "the name has to be 'valid' and nothing else";
    16. }
    17. }
    18.  
    19. function validatePhoneNo()
    20. {
    21. if ($this->phoneNo == 1) {
    22. return true;
    23. }
    24. else {
    25. return "only '1' is a valid phone number";
    26. }
    27. }
    28. }

Located in /Object/Plain.php (line 83)


	
			
Variable Summary
array $errors
array $_private
Method Summary
Ismo_Core_Object_Plain Ismo_Core_Object_Plain ([mixed $src = null])
void setFrom (mixed &$obj)
boolean validate (mixed 0)
string _createSetterMethodName (string $attributeName)
Variables
array $errors = NULL (line 137)

An array containing a list errors resulting from the validate() method

This array will be populated by the validate() method by setting the key to the attribute name, and the value to the error returned by the specify validate method. You can then use these in the template engine to either show errors for a specific field or iterate thru the array to show the list of errors in the data to be validated.

Example:

  1. $mo = new MyObject();
  2. $mo->set_from($_POST);
  3. if(!$mo->validate())
  4. {
  5. var_dump($mo->errors);
  6. }

This sets the $mo attributes from the $_POST variables and calls the validate methods that have been defined. If validate() returns false we dump the $errors array with the errors our validate methods produced.

  • access: public
array $_private = NULL (line 105)

An array containing a list of all the private attributes' names.

The attributes named in this array won't be exposed to the template engine. It might for example be wise to not expose a user's real password to the templates.

Example:

  1. $_private = array("id", "user_information_id");

This specifies that id and user_information_id are private attributes.

  • access: private
Methods
Constructor Ismo_Core_Object_Plain (line 171)

The constructor.

It is possible to pass in either an array or an Ismo_Core_Object_Plain and if so setFrom() is called with that parameter.

  • access: public
Ismo_Core_Object_Plain Ismo_Core_Object_Plain ([mixed $src = null])
  • mixed $src: an array or Ismo_Core_Object_Plain to fill in this object from.
getArrayRepresentation (line 209)

Returns an array representation of the class's attributes.

Returns the class's attributes in an associative array, with the attribute's name as key and the value as the value. This if useful for example when exposing the object to the template engine.

The attribute listed in the _private array are not included.

Note: In versions prior to PHP 4.2.0, if the variables declared in the class of which the obj is an instance, have not been assigned a value, those will not be returned in the array. In versions after PHP 4.2.0, the key will be assigned with a NULL value.

Example:

  1. $p = new Person();
  2. $p->name = "name";
  3. $p->phone_no = "034-34343";
  4. $hash = $p->getArrayRepresentation();
This results in $hash being an array containing the key-value pairs of the attributes that have a value. It will containg the following "name" => "name" and "phone_no" => "034-34343".

  • return: the resulting array
  • access: public
array getArrayRepresentation ()
setFrom (line 262)

Tries to "fill" in the current object from the array or objects given as parameter.

This method updates the current object with the values available in the given Ismo_Core_Object or array. Note that attributes listed in the _private attribute are not updated.

Example:

  1. $p = new Person();
  2. $a = array("name" => "Ismo", "phone_no" => "456");
  3. $p->setFrom($a);
This sets $p's name attribute to 'Ismo' and phone_no to '456'.

  1. $p1 = new Person();
  2. $p1->id = 1;
  3. $p1->name = "name1";
  4. $p1->phone_no = "name2";
  5.  
  6. $p2 = new Person();
  7. $p2->id = 2;
  8. $p2->setFrom($p1);

This sets $p2's name and phone_no attributes to the same as in p1. But the id attributes will still be different.

  • access: private
void setFrom (mixed &$obj)
  • mixed $obj: the object to fill in from, it's either an array or an Ismo_Core_Object
validate (line 387)

Validates this object.

The validation is done by calling a method named validate[class attribute name] for each class attribute. If the method doesn't exist no validation will be done for that attribute.

Any amount of parameters can be given to this method. All paramters given will be given to each of the validate methods when they are called. If the amount of parameters do not match you will get a PHP error.

Please note that the attribute's mentioned in the _private attribute, i.e. the private attributes, won't be validated.

By using require_once() to include PEAR's Validate class in a validate methode one can use those functions to validate the data.

Example:

  1. $p = new Person();
  2. $p->name = "Name";
  3. $p->phoneNo = "0123456";
  4. $p->country = "qwerty";
  5. if ($p->validate() != true)
  6. {
  7. foreach ($p->errors as $k => $v)
  8. {
  9. echo "Attribute " . $k . " isn't valid due to " . $v . "<br/>";
  10. }
  11. }

This will validate the attribute values of the Person class instance by calling the validateName(), validatePhoneNo() and validateCountry() methods if they exist.

Another example showing how the multiple parameters work:

  1. class Foo extends Ismo_Core_Object
  2. {
  3. var $name;
  4. var $address;
  5.  
  6. function Foo($src = null)
  7. {
  8. parent::Ismo_Core_Object($src);
  9. }
  10.  
  11. function validateName($country, $language)
  12. {
  13. if ($country == 'sweden' &&
  14. $language == 'swedish')
  15. {
  16. // do swedish check ...
  17. }
  18. else if ($country == 'finland' &&
  19. $language == 'finnish')
  20. {
  21. // do finnish check ...
  22. }
  23. else
  24. {
  25. // do other check
  26. }
  27. }
  28.  
  29. // and similar other validate methods
  30. }
  31.  
  32.  
  33. // now when validating Foo objects one has to give a
  34. // country and language. Of course those attributes could
  35. // have been assigned default values, i.e.
  36. // function validate_name($country = null, $language = null)
  37.  
  38.  
  39. $a = new Foo();
  40. if ($a->validate('sweden', 'swedish') != true)
  41. {
  42. foreach ($a->errors as $k => $v)
  43. {
  44. echo "Attribute " . $k . " isn't valid due to " . $v . "<br/>";
  45. }
  46. }

  • return: true if the everything validated ok. false if it did not and $this->errors will be set with an associative array see $this->errors documentation for more details
  • access: public
boolean validate (mixed 0)
  • mixed 0: any amount (including none) of parameters can be given. They will all be given to each of the validate methods when they are called.
_createSetterMethodName (line 152)

Creates the setter method name for a given attribute.

Given the attribute name this method returns the name of the setter method which can be used to set the value of the attribute.

  • return: the name of the setter method for the attribute
  • access: private
string _createSetterMethodName (string $attributeName)
  • string $attributeName: the name of the attribute to generate the setter method name for

Documentation generated on Mon, 14 Jun 2004 11:59:30 +0200 by phpDocumentor 1.3.0RC3