本文整理汇总了PHP中Magento\Framework\Api\FilterBuilder类的典型用法代码示例。如果您正苦于以下问题:PHP FilterBuilder类的具体用法?PHP FilterBuilder怎么用?PHP FilterBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FilterBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _renderFiltersBefore
/**
* @inheritdoc
*/
protected function _renderFiltersBefore()
{
$this->getSearchCriteriaBuilder();
$this->getFilterBuilder();
$this->getSearch();
if ($this->queryText) {
$this->filterBuilder->setField('search_term');
$this->filterBuilder->setValue($this->queryText);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
}
$priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if ($priceRangeCalculation) {
$this->filterBuilder->setField('price_dynamic_algorithm');
$this->filterBuilder->setValue($priceRangeCalculation);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
}
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchCriteria->setRequestName($this->searchRequestName);
$this->searchResult = $this->search->search($searchCriteria);
$temporaryStorage = $this->temporaryStorageFactory->create();
$table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
$this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
$this->_totalRecords = $this->searchResult->getTotalCount();
if ($this->order && 'relevance' === $this->order['field']) {
$this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
}
return parent::_renderFiltersBefore();
}
示例2: load
/**
* Load search results
*
* @return $this
*/
public function load()
{
$result = [];
if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
$this->setResults($result);
return $this;
}
$this->searchCriteriaBuilder->setCurrentPage($this->getStart());
$this->searchCriteriaBuilder->setPageSize($this->getLimit());
$searchFields = ['firstname', 'lastname', 'company'];
$filters = [];
foreach ($searchFields as $field) {
$filters[] = $this->filterBuilder->setField($field)->setConditionType('like')->setValue($this->getQuery() . '%')->create();
}
$this->searchCriteriaBuilder->addFilters($filters);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->customerRepository->getList($searchCriteria);
foreach ($searchResults->getItems() as $customer) {
$customerAddresses = $customer->getAddresses();
/** Look for a company name defined in default billing address */
$company = null;
foreach ($customerAddresses as $customerAddress) {
if ($customerAddress->getId() == $customer->getDefaultBilling()) {
$company = $customerAddress->getCompany();
break;
}
}
$result[] = ['id' => 'customer/1/' . $customer->getId(), 'type' => __('Customer'), 'name' => $this->_customerViewHelper->getCustomerName($customer), 'description' => $company, 'url' => $this->_adminhtmlData->getUrl('customer/index/edit', ['id' => $customer->getId()])];
}
$this->setResults($result);
return $this;
}
示例3: testLike
/**
* Test a search using 'like' condition
*/
public function testLike()
{
$attributeCode = 'description';
$attributeCodeId = 42;
$attribute = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false);
$attribute->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
$this->eavConfig->expects($this->once())->method('getAttribute')->with(Product::ENTITY, $attributeCodeId)->willReturn($attribute);
$filtersData = ['catalog_product_entity_text' => [$attributeCodeId => ['like' => 'search text']]];
$this->filterBuilder->expects($this->once())->method('setField')->with($attributeCode)->willReturn($this->filterBuilder);
$this->filterBuilder->expects($this->once())->method('setValue')->with('search text')->willReturn($this->filterBuilder);
$filter = $this->getMock('Magento\\Framework\\Api\\Filter');
$this->filterBuilder->expects($this->once())->method('create')->willReturn($filter);
$criteria = $this->getMock('Magento\\Framework\\Api\\Search\\SearchCriteria', [], [], '', false);
$this->criteriaBuilder->expects($this->once())->method('create')->willReturn($criteria);
$criteria->expects($this->once())->method('setRequestName')->with('advanced_search_container');
$tempTable = $this->getMock('Magento\\Framework\\DB\\Ddl\\Table', [], [], '', false);
$temporaryStorage = $this->getMock('Magento\\Framework\\Search\\Adapter\\Mysql\\TemporaryStorage', [], [], '', false);
$temporaryStorage->expects($this->once())->method('storeApiDocuments')->willReturn($tempTable);
$this->temporaryStorageFactory->expects($this->once())->method('create')->willReturn($temporaryStorage);
$searchResult = $this->getMock('Magento\\Framework\\Api\\Search\\SearchResultInterface', [], [], '', false);
$this->search->expects($this->once())->method('search')->willReturn($searchResult);
// addFieldsToFilter will load filters,
// then loadWithFilter will trigger _renderFiltersBefore code in Advanced/Collection
$this->assertSame($this->advancedCollection, $this->advancedCollection->addFieldsToFilter($filtersData)->loadWithFilter());
}
示例4: testGetByIdentifierNamespace
public function testGetByIdentifierNamespace()
{
$userId = 1;
$namespace = 'some_namespace';
$identifier = 'current';
$this->userContext->expects($this->once())->method('getUserId')->willReturn($userId);
$fieldUserId = new Filter([Filter::KEY_FIELD => 'user_id', Filter::KEY_VALUE => $userId, Filter::KEY_CONDITION_TYPE => 'eq']);
$fieldIdentifier = new Filter([Filter::KEY_FIELD => 'identifier', Filter::KEY_VALUE => $identifier, Filter::KEY_CONDITION_TYPE => 'eq']);
$fieldNamespace = new Filter([Filter::KEY_FIELD => 'namespace', Filter::KEY_VALUE => $namespace, Filter::KEY_CONDITION_TYPE => 'eq']);
$bookmarkId = 1;
$bookmark = $this->getMockBuilder('Magento\\Ui\\Api\\Data\\BookmarkInterface')->getMockForAbstractClass();
$bookmark->expects($this->once())->method('getId')->willReturn($bookmarkId);
$searchCriteria = $this->getMockBuilder('Magento\\Framework\\Api\\SearchCriteriaInterface')->getMockForAbstractClass();
$this->filterBuilder->expects($this->at(0))->method('create')->willReturn($fieldUserId);
$this->filterBuilder->expects($this->at(1))->method('create')->willReturn($fieldIdentifier);
$this->filterBuilder->expects($this->at(2))->method('create')->willReturn($fieldNamespace);
$this->searchCriteriaBuilder->expects($this->once())->method('addFilters')->with([$fieldUserId, $fieldIdentifier, $fieldNamespace]);
$this->searchCriteriaBuilder->expects($this->once())->method('create')->willReturn($searchCriteria);
$searchResult = $this->getMockBuilder('Magento\\Ui\\Api\\Data\\BookmarkSearchResultsInterface')->getMockForAbstractClass();
$searchResult->expects($this->once())->method('getTotalCount')->willReturn(1);
$searchResult->expects($this->once())->method('getItems')->willReturn([$bookmark]);
$this->bookmarkRepository->expects($this->once())->method('getList')->with($searchCriteria)->willReturn($searchResult);
$this->bookmarkRepository->expects($this->once())->method('getById')->with($bookmarkId)->willReturn($bookmark);
$this->assertEquals($bookmark, $this->bookmarkManagement->getByIdentifierNamespace($identifier, $namespace));
}
示例5: testGetAllOptions
/**
* Run test getAllOptions method
*
* @param bool $isEmpty
* @param array $expected
* @dataProvider dataProviderGetAllOptions
*/
public function testGetAllOptions($isEmpty, array $expected)
{
$filterMock = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false);
$searchCriteriaMock = $this->getMock('Magento\\Framework\\Api\\SearchCriteria', [], [], '', false);
$searchResultsMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassSearchResultsInterface', [], '', false, true, true, ['getItems']);
$taxClassMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassInterface', ['getClassId', 'getClassName'], '', false, true, true);
$this->filterBuilderMock->expects($this->once())->method('setField')->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)->willReturnSelf();
$this->filterBuilderMock->expects($this->once())->method('setValue')->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)->willReturnSelf();
$this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
$this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->with([$filterMock])->willReturnSelf();
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
$this->taxClassRepositoryMock->expects($this->once())->method('getList')->with($searchCriteriaMock)->willReturn($searchResultsMock);
if (!$isEmpty) {
$taxClassMock->expects($this->once())->method('getClassId')->willReturn(10);
$taxClassMock->expects($this->once())->method('getClassName')->willReturn('class-name');
$items = [$taxClassMock];
$searchResultsMock->expects($this->once())->method('getItems')->willReturn($items);
// checking of a lack of re-initialization
for ($i = 10; --$i;) {
$result = $this->customer->getAllOptions();
$this->assertEquals($expected, $result);
}
} else {
$items = [];
$searchResultsMock->expects($this->once())->method('getItems')->willReturn($items);
// checking exception
$this->assertEmpty($this->customer->getAllOptions());
}
}
示例6: testPrepare
/**
* Run test prepare method
*
* @param array $data
* @param array $filterData
* @param array|null $expectedCondition
* @dataProvider getPrepareDataProvider
* @return void
*/
public function testPrepare($data, $filterData, $expectedCondition)
{
$name = $data['name'];
/** @var UiComponentInterface $uiComponent */
$uiComponent = $this->getMockForAbstractClass('Magento\\Framework\\View\\Element\\UiComponentInterface', [], '', false);
$uiComponent->expects($this->any())->method('getContext')->willReturn($this->contextMock);
$this->contextMock->expects($this->any())->method('getNamespace')->willReturn(Select::NAME);
$this->contextMock->expects($this->any())->method('addComponentDefinition')->with(Select::NAME, ['extends' => Select::NAME]);
$this->contextMock->expects($this->any())->method('getFiltersParams')->willReturn($filterData);
/** @var DataProviderInterface $dataProvider */
$dataProvider = $this->getMockForAbstractClass('Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\DataProviderInterface', ['addFilter'], '', false);
$this->contextMock->expects($this->any())->method('getDataProvider')->willReturn($dataProvider);
if ($expectedCondition !== null) {
$filterMock = $this->getMock('Magento\\Framework\\Api\\Filter');
$this->filterBuilderMock->expects($this->any())->method('setConditionType')->with($expectedCondition)->willReturnSelf();
$this->filterBuilderMock->expects($this->any())->method('setField')->with($name)->willReturnSelf();
$this->filterBuilderMock->expects($this->any())->method('setValue')->willReturnSelf();
$this->filterBuilderMock->expects($this->any())->method('create')->willReturn($filterMock);
$dataProvider->expects($this->any())->method('addFilter')->with($filterMock);
}
/** @var \Magento\Framework\Data\OptionSourceInterface $selectOptions */
$selectOptions = $this->getMockForAbstractClass('Magento\\Framework\\Data\\OptionSourceInterface', [], '', false);
$this->uiComponentFactory->expects($this->any())->method('create')->with($name, Select::COMPONENT, ['context' => $this->contextMock, 'options' => $selectOptions])->willReturn($uiComponent);
$date = new Select($this->contextMock, $this->uiComponentFactory, $this->filterBuilderMock, $this->filterModifierMock, $selectOptions, [], $data);
$date->prepare();
}
示例7: isAssignedToObjects
/**
* {@inheritdoc}
*/
public function isAssignedToObjects()
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(CustomerGroup::TAX_CLASS_ID)->setValue($this->getId())->create()])->create();
$result = $this->customerGroupRepository->getList($searchCriteria);
$items = $result->getItems();
return !empty($items);
}
示例8: testApplyFilterModifierWith
/**
* @param $filterModifier
* @param $filterName
* @param $conditionType
* @param $value
* @return void
* @dataProvider getApplyFilterModifierDataProvider
*/
public function testApplyFilterModifierWith($filterModifier, $filterName, $conditionType, $value)
{
$filter = $this->getMock('Magento\\Framework\\Api\\Filter');
$this->request->expects($this->once())->method('getParam')->with(\Magento\Ui\Component\Filters\FilterModifier::FILTER_MODIFIER)->willReturn($filterModifier);
$this->filterBuilder->expects($this->once())->method('setConditionType')->with($conditionType)->willReturnSelf();
$this->filterBuilder->expects($this->once())->method('setField')->with($filterName)->willReturnSelf();
$this->filterBuilder->expects($this->once())->method('setValue')->with($value)->willReturnSelf();
$this->filterBuilder->expects($this->once())->method('create')->with()->willReturn($filter);
$this->dataProvider->expects($this->once())->method('addFilter')->with($filter);
$this->unit->applyFilterModifier($this->dataProvider, $filterName);
}
示例9: execute
/**
* @return void
*/
public function execute()
{
$filter = $this->filterBuilder->setField('parent_id')->setValue($this->_getCheckout()->getCustomer()->getId())->setConditionType('eq')->create();
$addresses = (array) $this->addressRepository->getList($this->searchCriteriaBuilder->addFilters([$filter])->create())->getItems();
/**
* if we create first address we need reset emd init checkout
*/
if (count($addresses) === 1) {
$this->_getCheckout()->reset();
}
$this->_redirect('*/checkout/addresses');
}
示例10: applyFilterModifier
/**
* Apply modifiers for filters
*
* @param DataProviderInterface $dataProvider
* @param string $filterName
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function applyFilterModifier(DataProviderInterface $dataProvider, $filterName)
{
$filterModifier = $this->request->getParam(self::FILTER_MODIFIER);
if (isset($filterModifier[$filterName]['condition_type'])) {
$conditionType = $filterModifier[$filterName]['condition_type'];
if (!in_array($conditionType, $this->allowedConditionTypes)) {
throw new \Magento\Framework\Exception\LocalizedException(__('Condition type "%1" is not allowed', $conditionType));
}
$value = isset($filterModifier[$filterName]['value']) ? $filterModifier[$filterName]['value'] : null;
$filter = $this->filterBuilder->setConditionType($conditionType)->setField($filterName)->setValue($value)->create();
$dataProvider->addFilter($filter);
}
}
示例11: getByIdentifierNamespace
/**
* {@inheritdoc}
*/
public function getByIdentifierNamespace($identifier, $namespace)
{
$this->searchCriteriaBuilder->addFilters([$this->filterBuilder->setField('user_id')->setConditionType('eq')->setValue($this->userContext->getUserId())->create(), $this->filterBuilder->setField('identifier')->setConditionType('eq')->setValue($identifier)->create(), $this->filterBuilder->setField('namespace')->setConditionType('eq')->setValue($namespace)->create()]);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->bookmarkRepository->getList($searchCriteria);
if ($searchResults->getTotalCount() > 0) {
foreach ($searchResults->getItems() as $searchResult) {
$bookmark = $this->bookmarkRepository->getById($searchResult->getId());
return $bookmark;
}
}
return null;
}
示例12: getAddress
/**
* Get a list of current customer addresses.
*
* @return \Magento\Customer\Api\Data\AddressInterface[]
*/
public function getAddress()
{
$addresses = $this->getData('address_collection');
if ($addresses === null) {
try {
$filter = $this->filterBuilder->setField('parent_id')->setValue($this->_multishipping->getCustomer()->getId())->setConditionType('eq')->create();
$addresses = (array) $this->addressRepository->getList($this->searchCriteriaBuilder->addFilters([$filter])->create())->getItems();
} catch (NoSuchEntityException $e) {
return [];
}
$this->setData('address_collection', $addresses);
}
return $addresses;
}
示例13: getAllOptions
/**
* Retrieve all customer tax classes as an options array.
*
* @return array
* @throws StateException
*/
public function getAllOptions()
{
if (empty($this->_options)) {
$options = [];
$filter = $this->filterBuilder->setField(TaxClass::KEY_TYPE)->setValue(TaxClassManagementInterface::TYPE_CUSTOMER)->create();
$searchCriteria = $this->searchCriteriaBuilder->addFilter([$filter])->create();
$searchResults = $this->taxClassRepository->getList($searchCriteria);
foreach ($searchResults->getItems() as $taxClass) {
$options[] = ['value' => $taxClass->getClassId(), 'label' => $taxClass->getClassName()];
}
$this->_options = $options;
}
return $this->_options;
}
示例14: getDataSourceData
/**
* {@inheritdoc}
*/
public function getDataSourceData()
{
$dataSource = [];
$id = $this->getContext()->getRequestParam($this->getContext()->getDataProvider()->getRequestFieldName());
if ($id) {
$filter = $this->filterBuilder->setField($this->getContext()->getDataProvider()->getPrimaryFieldName())->setValue($id)->create();
$this->getContext()->getDataProvider()->addFilter($filter);
$data = $this->getContext()->getDataProvider()->getData();
if (isset($data[$id])) {
$dataSource = ['data' => $data[$id]];
}
}
return $dataSource;
}
示例15: getTaxClassId
/**
* {@inheritdoc}
*/
public function getTaxClassId($taxClassKey, $taxClassType = self::TYPE_PRODUCT)
{
if (!empty($taxClassKey)) {
switch ($taxClassKey->getType()) {
case TaxClassKeyInterface::TYPE_ID:
return $taxClassKey->getValue();
case TaxClassKeyInterface::TYPE_NAME:
$searchCriteria = $this->searchCriteriaBuilder->addFilters([$this->filterBuilder->setField(ClassModel::KEY_TYPE)->setValue($taxClassType)->create()])->addFilters([$this->filterBuilder->setField(ClassModel::KEY_NAME)->setValue($taxClassKey->getValue())->create()])->create();
$taxClasses = $this->classRepository->getList($searchCriteria)->getItems();
$taxClass = array_shift($taxClasses);
return null == $taxClass ? null : $taxClass->getClassId();
}
}
return null;
}