/**
  * test primitive variables
  */
 function testPrimitiveVars()
 {
     // make an eptTest object
     $this->assertTrue($o = new eptTest("XXX", "YYY", "ZZZ"));
     // wrap it with epObject
     $this->assertTrue($w = new epObject($o));
     // test new object is not dirty
     $this->assertFalse($w->epIsDirty());
     // check vars
     $this->assertTrue($vars = $w->epGetVars());
     // x, a, o are in vars
     $this->assertTrue(array_key_exists("x", $vars));
     $this->assertTrue(array_key_exists("a", $vars));
     $this->assertTrue(array_key_exists("o", $vars));
     // y protected is not in vars
     $this->assertFalse(array_key_exists("y", $vars));
     $this->assertFalse(array_key_exists("z", $vars));
     /**
      * test primitive vars
      */
     // test epGet('x') against direct object property access ($o->x)
     $this->assertTrue($w->epGet('x') === $o->x);
     // test epSet('x'), epGet('x'), and $w->x (overloading __get())
     $value = md5($w->epGet('x'));
     $this->assertTrue($w->epSet('x', $value));
     $this->assertTrue($o->x === $value);
     $this->assertTrue($w->epGet('x') === $o->x);
     $this->assertTrue($w->x === $o->x);
     $this->assertTrue($w->epIsDirty());
     // check if 'x' is in modified list
     $this->assertTrue($modified = $w->epGetModifiedVars());
     $this->assertTrue(array_key_exists('x', $modified));
     $this->assertTrue($modified['x'] === $value);
     // test getter/setter (overloading __call())
     $value = md5($w->epGet('x'));
     $this->assertTrue($w->setX($value));
     $this->assertTrue($w->getX() === $o->x);
     $this->assertTrue($w->getX() === $value);
     // check which 'x' is in modified list
     $this->assertTrue($modified = $w->epGetModifiedVars());
     $this->assertTrue(array_key_exists('x', $modified));
     $this->assertTrue($modified['x'] === $value);
     // check magic method __get() and __set()
     $this->assertTrue($w->x === $value);
     $value = md5($w->x);
     $this->assertTrue($w->x = $value);
     $this->assertTrue($w->x === $value);
     // getting protected 'y' gets exception
     try {
         $this->assertTrue($w->epGet('y') === false);
     } catch (Exception $e) {
         // !!!simpletest seem to ignore assert in catch block
         $this->assertTrue($e instanceof epExceptionObject);
     }
     // setting protected 'y' gets exception
     try {
         $this->assertTrue($w->epSet('y', 'YYY|YYY') === false);
     } catch (Exception $e) {
         // !!!simpletest seem to ignore assert in catch block
         $this->assertTrue($e instanceof epExceptionObject);
     }
     // getting protected 'z' gets exception
     try {
         $this->assertTrue($w->epGet('z') === false);
     } catch (Exception $e) {
         // !!!simpletest seem to ignore assert in catch block
         $this->assertTrue($e instanceof epExceptionObject);
     }
     // setting protected 'z' gets exception
     try {
         $this->assertTrue($w->epSet('z', 'ZZZ|ZZZ') === false);
     } catch (Exception $e) {
         // !!!simpletest seem to ignore assert in catch block
         $this->assertTrue($e instanceof epExceptionObject);
     }
 }
Example #2
0
 /**
  * Combines the same functionality that {@link createFromArray()} and 
  * {@link updateFromArray} have.
  *
  * @param epObject $o 
  * @param array $array
  * @param boolean $dispatch_event whether to dispatch events
  * @param boolean $clean
  * @return null|epObject|string (error message if string)
  * @throws epExceptionManagerBase
  */
 protected function &_fromArray(&$o, $array, $dispatch_event, $clean = true)
 {
     // get class from object
     $class = $o->epGetClass();
     // copy array values to object
     foreach ($array as $var => $val) {
         // skip oid
         if ($var == 'oid') {
             $o->epSetObjectId($val);
             continue;
         }
         // check if var exists in class
         if (!$o->epIsVar($var)) {
             // unrecognized var
             throw new epExceptionManagerBase('Variable [' . $var . '] does not exist in class [' . $class . ']');
             return self::$null;
         }
         //
         // a primitive var?
         //
         if ($o->epIsPrimitive($var)) {
             // primitive must be a scalar
             if (!is_scalar($val)) {
                 throw new epExceptionManagerBase('Value is not scalar for primitive var [' . $class . '::' . $var . ']');
                 return self::$null;
             }
             // assign value to var in object
             $o[$var] = $val;
             continue;
         }
         //
         // a relationship var
         //
         // get class of var
         $class = $o->epGetClassOfVar($var);
         //
         // a single-valued var
         //
         if ($o->epIsSingle($var)) {
             // recursion if value is an array
             if (is_array($val)) {
                 // recursion:  object to var in object
                 $o->epSet($var, $this->createFromArray($class, $val, $dispatch_event, $clean));
                 continue;
             }
             // if value is int, it might be an oid of an existing object
             if (is_integer($val) || ctype_digit($val)) {
                 // get the corresponding object
                 if ($sub =& $this->get($class, $val)) {
                     // yep, it exists
                     $o->epSet($var, $sub);
                     continue;
                 }
             }
             // if it is an object, it might be an existing object
             if ($val instanceof epObject) {
                 $o->epSet($var, $val);
                 continue;
             }
             throw new epExceptionManagerBase('Value is not array or object id for relationship var [' . $class . '::' . $var . ']');
             continue;
         }
         //
         // a many-valued var
         //
         // value should be either an integer (oid) or an array.
         $vals = is_array($val) ? $val : array($val);
         // check if value is multiple. arrayize it if not.
         $vals = isset($val[0]) ? $val : array($val);
         // create objects for many-valued field
         $os = array();
         foreach ($vals as $val) {
             // recursion:  object to var in object
             if (is_array($val)) {
                 $os[] =& $this->createFromArray($class, $val, $dispatch_event, $clean);
                 continue;
             }
             // if value is int, it might be an oid of an existing object
             if (is_integer($val) || ctype_digit($val)) {
                 if ($sub =& $this->get($class, $val)) {
                     // yep, it exists
                     $os[] =& $sub;
                     continue;
                 }
             }
             // if it is an object, it might be an existing object
             if ($val instanceof epObject) {
                 $os[] =& $val;
                 continue;
             }
             throw new epExceptionManagerBase('Value is not array or object id for relationship var [' . $class . '::' . $var . ']');
             continue;
         }
         $o->epSet($var, $os);
     }
     return $o;
 }