/**
  * {@inheritdoc}
  */
 public function match(ContactInterface $contact)
 {
     $ids = array();
     $fields = $contact->getFieldDefinitions();
     $results = array();
     $configuration_rules = $this->getConfigurationItem('rules') ?: [];
     foreach ($configuration_rules as $name => $rules) {
         if (isset($fields[$name])) {
             $rules['field'] = $fields[$name];
             if (!$this->pluginManager->hasDefinition($rules['field']->getType())) {
                 continue;
             }
             /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $field_handler */
             $field_handler = $this->pluginManager->createInstance($rules['field']->getType(), $rules);
             foreach ($field_handler->getPropertyNames() as $name) {
                 $results += $field_handler->match($contact, $name);
             }
         }
     }
     foreach ($results as $id => $rule_matches) {
         $total_score = array_sum($rule_matches);
         if ($total_score >= $this->getConfigurationItem('threshold')) {
             $ids[] = $id;
         }
     }
     return $ids;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function match(ContactInterface $contact, $property = 'value')
 {
     $ids = array();
     $field = $this->field->getName();
     $needle = $contact->get($field)->{$property};
     if (!empty($needle)) {
         $this->query->condition('type', $contact->bundle());
         if ($contact->id()) {
             $this->query->condition('contact_id', $contact->id(), '<>');
         }
         if ($field instanceof FieldConfigInterface) {
             $field .= '.' . $property;
         }
         $this->query->condition($field, $needle, $this->getOperator($property));
         $ids = $this->query->execute();
     }
     // Get the score for this field/propery.
     $score = array($this->field->getName() . '.' . $property => $this->getScore($property));
     // Returning an array holding the score as value and the contact id as key.
     return array_fill_keys($ids, $score);
 }