SynonymsBehavior.interface.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Extract synonyms from an entity within a specific behavior implementation.
  14. *
  15. * @param object $entity
  16. * Entity from which to extract synonyms
  17. *
  18. * @return array
  19. * Array of synonyms extracted from $entity
  20. */
  21. public function extractSynonyms($entity);
  22. /**
  23. * Add an entity as a synonym into another entity.
  24. *
  25. * Basically this method should be called when you want to add some entity as
  26. * a synonym to another entity (for example when you merge one entity into
  27. * another and besides merging want to add synonym of the merged entity into
  28. * the trunk entity). You should update $trunk_entity in such a way that it
  29. * holds $synonym_entity as a synonym (it all depends on how data is stored in
  30. * your behavior implementation, but probably you will store entity label or
  31. * its ID as you cannot literally store an entity inside of another entity).
  32. * If entity of type $synonym_entity_type cannot be converted into a format
  33. * expected by your behavior implementation, just do nothing.
  34. *
  35. * @param object $trunk_entity
  36. * Entity into which another one should be added as synonym
  37. * @param object $synonym_entity
  38. * Fully loaded entity object which has to be added as synonym
  39. * @param string $synonym_entity_type
  40. * Entity type of $synonym_entity
  41. */
  42. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type);
  43. /**
  44. * Look up entities by their synonyms within a behavior implementation.
  45. *
  46. * You are provided with a SQL condition that you should apply to the storage
  47. * of synonyms within the provided behavior implementation. And then return
  48. * result: what entities match by the provided condition through what
  49. * synonyms.
  50. *
  51. * @param QueryConditionInterface $condition
  52. * Condition that defines what to search for. Apart from normal SQL
  53. * conditions as known in Drupal, it may contain the following placeholders:
  54. * - AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER: to denote
  55. * synonyms column which you should replace with the actual column name
  56. * where the synonyms data for your provider is stored in plain text.
  57. * - AbstractSynonymsBehavior::COLUMN_ENTITY_ID_PLACEHOLDER: to denote
  58. * column that holds entity ID. You are supposed to replace this
  59. * placeholder with actual column name that holds entity ID in your case.
  60. * For ease of work with these placeholders, you may extend the
  61. * AbstractSynonymsBehavior class and then just invoke the
  62. * AbstractSynonymsBehavior->synonymsFindProcessCondition() method, so you
  63. * won't have to worry much about it
  64. *
  65. * @return Traversable
  66. * Traversable result set of found synonyms and entity IDs to which those
  67. * belong. Each element in the result set should be an object and will have
  68. * the following structure:
  69. * - synonym: (string) Synonym that was found and which satisfies the
  70. * provided condition
  71. * - entity_id: (int) ID of the entity to which the found synonym belongs
  72. */
  73. public function synonymsFind(QueryConditionInterface $condition);
  74. /**
  75. * Collect info on features pipe during invocation of hook_features_export().
  76. *
  77. * If your synonyms provider depends on some other features components, this
  78. * method should return them.
  79. *
  80. * @return array
  81. * Array of features pipe as per hook_features_export() specification
  82. */
  83. public function featuresExportPipe();
  84. }
  85. /**
  86. * Exception thrown by implementations of SynonymsBehavior interface.
  87. */
  88. class SynonymsBehaviorException extends Exception {}
  89. /**
  90. * Starting point for implementing SynonymsBehavior interface.
  91. */
  92. abstract class AbstractSynonymsBehavior implements SynonymsBehavior {
  93. /**
  94. * Constant which denotes placeholder of a synonym column.
  95. *
  96. * @var string
  97. */
  98. const COLUMN_SYNONYM_PLACEHOLDER = '***COLUMN***';
  99. /**
  100. * Constant which denotes placeholder of an entity ID column.
  101. *
  102. * @var string
  103. */
  104. const COLUMN_ENTITY_ID_PLACEHOLDER = '***ENTITY_ID***';
  105. /**
  106. * Behavior implementation on which this class was initialized.
  107. *
  108. * @var array
  109. */
  110. protected $behavior_implementation;
  111. public function __construct($behavior_implementation) {
  112. $this->behavior_implementation = $behavior_implementation;
  113. }
  114. public function featuresExportPipe() {
  115. return array();
  116. }
  117. /**
  118. * Process condition in 'synonymsFind' method.
  119. *
  120. * Process condition in 'synonymsFind' method replacing all references of
  121. * synonym and entity ID columns with the real names of those columns.
  122. *
  123. * @param QueryConditionInterface $condition
  124. * Condition that should be processed
  125. * @param string $column_synonym
  126. * Real name of the synonym column
  127. * @param string $column_entity_id
  128. * Real name of the entity ID column
  129. */
  130. protected function synonymsFindProcessCondition(QueryConditionInterface $condition, $column_synonym, $column_entity_id) {
  131. $condition_array = &$condition->conditions();
  132. foreach ($condition_array as &$v) {
  133. if (is_array($v) && isset($v['field'])) {
  134. if ($v['field'] instanceof QueryConditionInterface) {
  135. // Recursively process this condition too.
  136. $this->synonymsFindProcessCondition($v['field'], $column_synonym, $column_entity_id);
  137. }
  138. else {
  139. $replace = array(
  140. self::COLUMN_SYNONYM_PLACEHOLDER => $column_synonym,
  141. self::COLUMN_ENTITY_ID_PLACEHOLDER => $column_entity_id,
  142. );
  143. $v['field'] = str_replace(array_keys($replace), array_values($replace), $v['field']);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Interface of the autocomplete synonyms behavior.
  151. */
  152. interface AutocompleteSynonymsBehavior extends SynonymsBehavior {
  153. }
  154. /**
  155. * Interface of the synonyms friendly select behavior.
  156. */
  157. interface SelectSynonymsBehavior extends SynonymsBehavior {
  158. }