/** * @inheritdoc */ public function getSearchableAttributes($recursive = true) { /** @var \im\base\types\EntityTypesRegister $typesRegister */ $typesRegister = Yii::$app->get('typesRegister'); /** @var \im\search\components\SearchManager $searchManager */ $searchManager = Yii::$app->get('searchManager'); $model = $this->getModel(); if ($model instanceof AttributeValue) { return []; } $entityType = $typesRegister->getEntityType($model); $attributes = $model->attributes(); $labels = $model->attributeLabels(); $searchableAttributes = []; $key = 0; // Attributes foreach ($attributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute, 'label' => isset($labels[$attribute]) ? $labels[$attribute] : $model->generateAttributeLabel($attribute)]); $key++; } // EAV $eavAttributes = Attribute::findByEntityType($entityType); foreach ($eavAttributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute->getName() . '_attr', 'label' => $attribute->getPresentation()]); $key++; } // Relations $class = new \ReflectionClass($model); foreach ($class->getMethods() as $method) { if ($method->isPublic()) { $methodName = $method->getName(); if (strpos($methodName, 'get') === 0) { $math = false; $name = substr($methodName, 3); if (substr($name, -8) === 'Relation') { $name = substr($name, 0, -8); $math = true; } elseif (preg_match('/@return.*ActiveQuery/i', $method->getDocComment())) { $math = true; } if ($math && $name) { /** @var ActiveQuery $relation */ $relation = $model->{$methodName}(); $modelClass = $relation->modelClass; $searchableType = $searchManager->getSearchableTypeByClass($modelClass); if (!$searchableType) { $reflector = new ReflectionClass($modelClass); /** @var SearchableInterface $searchableType */ $searchableType = Yii::createObject(['class' => 'im\\search\\components\\service\\db\\SearchableType', 'type' => Inflector::camel2id($reflector->getShortName(), '_'), 'modelClass' => $modelClass]); } $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => Inflector::variablize(substr($methodName, 3)), 'label' => Inflector::titleize($name), 'value' => function ($model) use($methodName) { return $model->{$methodName}(); }, 'type' => $searchableType->getType()]); $key++; if ($recursive) { foreach ($searchableType->getSearchableAttributes(false) as $attribute) { $attribute->label = Inflector::titleize($name) . ' >> ' . $attribute->label; $attribute->name = Inflector::variablize(substr($methodName, 3)) . '.' . $attribute->name; $searchableAttributes[$key] = $attribute; $key++; } } } } } } return $searchableAttributes; }
/** * @param string $entityType * @return AttributeDescriptor[] */ protected function getSearchableEAVAttributes($entityType) { $searchableAttributes = []; $eavAttributes = Attribute::findByEntityType($entityType); foreach ($eavAttributes as $attribute) { $searchableAttributes[] = new AttributeDescriptor(['name' => $attribute->getName() . '_attr', 'label' => $attribute->getPresentation()]); } return $searchableAttributes; }