SynonymsBehavior.interface.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Interfaces of synonyms behaviors that are shipped with Synonyms module.
  5. */
  6. /**
  7. * General interface of a synonyms behavior.
  8. *
  9. * All synonyms behaviors must extend this interface.
  10. */
  11. interface SynonymsBehavior {
  12. }
  13. /**
  14. * Interface of "synonyms" behavior.
  15. *
  16. * The most basic synonyms behavior.
  17. */
  18. interface SynonymsSynonymsBehavior extends SynonymsBehavior {
  19. /**
  20. * Extract synonyms from a field attached to an entity.
  21. *
  22. * We try to pass as many info about context as possible, however, normally
  23. * you will only need $items to extract the synonyms.
  24. *
  25. * @param array $items
  26. * Array of items
  27. * @param array $field
  28. * Array of field definition according to Field API
  29. * @param array $instance
  30. * Array of instance definition according to Field API
  31. * @param object $entity
  32. * Fully loaded entity object to which the $field and $instance with $item
  33. * values is attached to
  34. * @param string $entity_type
  35. * Type of the entity $entity according to Field API definition of entity
  36. * types
  37. *
  38. * @return array
  39. * Array of synonyms extracted from $items
  40. */
  41. public function extractSynonyms($items, $field, $instance, $entity, $entity_type);
  42. /**
  43. * Add an entity as a synonym into a field of another entity.
  44. *
  45. * Basically this method should be called when you want to add some entity
  46. * as a synonym to another entity (for example when you merge one entity
  47. * into another and besides merging want to add synonym of the merged entity
  48. * into the trunk entity). You should extract synonym value (according to what
  49. * value is expected in this field) and return it. We try to provide you with
  50. * as much of context as possible, but normally you would only need
  51. * $synonym_entity and $synonym_entity_type parameters. Return an empty array
  52. * if entity of type $synonym_entity_type cannot be converted into a format
  53. * expected by $field.
  54. *
  55. * @param array $items
  56. * Array items that already exist in the field into which new synonyms is to
  57. * be added
  58. * @param array $field
  59. * Field array definition according to Field API of the field into which new
  60. * synonym is to be added
  61. * @param array $instance
  62. * Instance array definition according to Field API of the instance into
  63. * which new synonym is to be added
  64. * @param object $synonym_entity
  65. * Fully loaded entity object which has to be added as synonym
  66. * @param string $synonym_entity_type
  67. * Entity type of $synonym_entity
  68. *
  69. * @return array
  70. * Array of extra items to be merged into the items that already exist in
  71. * field values
  72. */
  73. public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type);
  74. /**
  75. * Hash a field item that is enabled as synonym.
  76. *
  77. * Your hash function must return such hash that for 2 items that yield the
  78. * same synonyms their hash must be the same. There is no limit on minimal or
  79. * maximum hash length, but keep it reasonable, something below 512 symbols.
  80. * Also, your hash function should strive to minimize hash collisions, i.e.
  81. * when 2 different items yield the same hash.
  82. *
  83. * @param array $item
  84. * Field item whose hash is requested
  85. * @param array $field
  86. * Field from which the $item comes from
  87. * @param array $instance
  88. * Instance from which the $item comes from
  89. *
  90. * @return string
  91. * Hash of the provided $item
  92. */
  93. public function synonymItemHash($item, $field, $instance);
  94. /**
  95. * Look up entities by their synonyms within a provided field.
  96. *
  97. * You are provided with a SQL condition that you should apply to the storage
  98. * of synonyms within the provided field. And then return result: what
  99. * entities match by the provided condition through what synonyms.
  100. *
  101. * @param QueryConditionInterface $condition
  102. * Condition that defines what to search for. It may contain a placeholder
  103. * of AbstractSynonymsSynonymsBehavior::COLUMN_PLACEHOLDER which you should
  104. * replace by the column name where the synonyms data for your field is
  105. * stored in plain text. For it to do, you may extend the
  106. * AbstractSynonymsSynonymsBehavior class and then just invoke the
  107. * AbstractSynonymsSynonymsBehavior->synonymsFindProcessCondition() method,
  108. * so you won't have to worry much about it
  109. * @param array $field
  110. * Field API field definition array of the field within which the search
  111. * for synonyms should be performed
  112. * @param array $instance
  113. * Field API instance definition array of the instance within which the
  114. * search for synonyms should be performed
  115. *
  116. * @return Traversable
  117. * Traversable result set of found synonyms and entity IDs to which those
  118. * belong. Each element in the result set should be an object and will have
  119. * the following structure:
  120. * - synonym: (string) Synonym that was found and which satisfies the
  121. * provided condition
  122. * - entity_id: (int) ID of the entity to which the found synonym belongs
  123. */
  124. public function synonymsFind(QueryConditionInterface $condition, $field, $instance);
  125. }
  126. /**
  127. * Exception thrown by implementations of SynonymsSynonymsBehavior interface.
  128. */
  129. class SynonymsSynonymsBehaviorException extends Exception {}
  130. /**
  131. * Starting point for implementing SynonymsSynonymsBehavior interface.
  132. */
  133. abstract class AbstractSynonymsSynonymsBehavior implements SynonymsSynonymsBehavior {
  134. /**
  135. * Constant which denotes placeholder of a synonym column.
  136. *
  137. * @var string
  138. */
  139. const COLUMN_PLACEHOLDER = '***COLUMN***';
  140. /**
  141. * Process condition in 'synonymsFind' method.
  142. *
  143. * Process condition in 'synonymsFind' method replacing all references of
  144. * synonym column with the real name of that column.
  145. *
  146. * @param QueryConditionInterface $condition
  147. * Condition that should be processed
  148. * @param string $column
  149. * Real name of the synonym column
  150. */
  151. protected function synonymsFindProcessCondition(QueryConditionInterface $condition, $column) {
  152. $condition_array = &$condition->conditions();
  153. foreach ($condition_array as &$v) {
  154. if (is_array($v) && isset($v['field'])) {
  155. if ($v['field'] instanceof QueryConditionInterface) {
  156. // Recursively process this condition too.
  157. $this->synonymsFindProcessCondition($v['field'], $column);
  158. }
  159. else {
  160. $v['field'] = str_replace(self::COLUMN_PLACEHOLDER, $column, $v['field']);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * Interface of the autocomplete synonyms behavior.
  168. */
  169. interface AutocompleteSynonymsBehavior extends SynonymsSynonymsBehavior {
  170. }
  171. /**
  172. * Interface of the synonyms friendly select behavior.
  173. */
  174. interface SelectSynonymsBehavior extends SynonymsSynonymsBehavior {
  175. }