Example #1
0
 /**
  * Get notified of an event
  *
  * @param  Model   $instance
  * @param  string  $event
  */
 public static function orm_notify(Model $instance, $event)
 {
     if (!array_key_exists($event, static::$events)) {
         return;
     }
     $event_type = static::$events[$event];
     $properties = $instance->properties();
     foreach ($properties as $p => $settings) {
         if (empty($settings['data_type']) || in_array($p, $instance->primary_key())) {
             continue;
         }
         if ($instance->{$p} === null) {
             if (array_key_exists('null', $settings) and $settings['null'] === false) {
                 throw new InvalidContentType('The property "' . $p . '" cannot be NULL.');
             }
             continue;
         }
         foreach (static::$type_methods as $match => $method) {
             if (is_array($method)) {
                 $method = !empty($method[$event_type]) ? $method[$event_type] : false;
             }
             if ($method === false) {
                 continue;
             }
             if ($method and preg_match($match, $settings['data_type']) > 0) {
                 $instance->{$p} = call_user_func($method, $instance->{$p}, $settings);
                 continue;
             }
         }
     }
 }
Example #2
0
 /**
  * Get notified of an event
  *
  * @param  Model   $instance
  * @param  string  $event
  */
 public static function orm_notify(Model $instance, $event)
 {
     // if we don't serve this event, bail out immediately
     if (array_key_exists($event, static::$events)) {
         // get the event type of the event that triggered us
         $event_type = static::$events[$event];
         // fetch the model's properties
         $properties = $instance->properties();
         // and check if we need to do any datatype conversions
         foreach ($properties as $p => $settings) {
             // the property is part of the primary key, skip it
             if (in_array($p, $instance->primary_key())) {
                 continue;
             }
             $instance->{$p} = static::typecast($p, $instance->{$p}, $settings, $event_type);
         }
     }
 }
Example #3
0
 /**
  * Get notified of an event
  *
  * @param  Model   $instance
  * @param  string  $event
  */
 public static function orm_notify(Model $instance, $event)
 {
     if (!array_key_exists($event, static::$events)) {
         return;
     }
     $event_type = static::$events[$event];
     $properties = $instance->properties();
     foreach ($properties as $p => $settings) {
         if (empty($settings['type']) || in_array($p, $instance->primary_key())) {
             continue;
         }
         if ($instance->{$p} === null) {
             continue;
         }
         foreach (static::$type_methods as $match => $method) {
             if (is_array($method)) {
                 $method = !empty($method[$event_type]) ? $method[$event_type] : false;
             }
             if ($method and preg_match($match, $settings['type']) > 0) {
                 $instance->{$p} = call_user_func($method, $instance->{$p}, $settings['type']);
             }
         }
     }
 }