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;
             }
         }
     }
     if ($event_type == 'after') {
         $instance->_update_original();
     }
 }