本文整理汇总了PHP中Magento\Framework\Exception\InputException类的典型用法代码示例。如果您正苦于以下问题:PHP InputException类的具体用法?PHP InputException怎么用?PHP InputException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了InputException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* Validate user credentials
*
* @param string $username
* @param string $password
* @throws InputException
* @return void
*/
public function validate($username, $password)
{
$exception = new InputException();
if (!is_string($username) || strlen($username) == 0) {
$exception->addError(__('%fieldName is a required field.', ['fieldName' => 'username']));
}
if (!is_string($password) || strlen($password) == 0) {
$exception->addError(__('%fieldName is a required field.', ['fieldName' => 'password']));
}
if ($exception->wasErrorAdded()) {
throw $exception;
}
}
示例2: getList
/**
* {@inheritdoc}
*/
public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
if (!$entityTypeCode) {
throw InputException::requiredField('entity_type_code');
}
/** @var \Magento\Eav\Model\Resource\Entity\Attribute\Collection $attributeCollection */
$attributeCollection = $this->attributeCollectionFactory->create();
$attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]);
$attributeCollection->join(['entity_type' => $attributeCollection->getTable('eav_entity_type')], 'main_table.entity_type_id = entity_type.entity_type_id', []);
$attributeCollection->join(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []);
$attributeCollection->join(['additional_table' => $attributeCollection->getTable('catalog_eav_attribute')], 'main_table.attribute_id = additional_table.attribute_id', []);
//Add filters from root filter group to the collection
foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $attributeCollection);
}
/** @var \Magento\Framework\Api\SortOrder $sortOrder */
foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
$attributeCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC');
}
$totalCount = $attributeCollection->getSize();
// Group attributes by id to prevent duplicates with different attribute sets
$attributeCollection->addAttributeGrouping();
$attributeCollection->setCurPage($searchCriteria->getCurrentPage());
$attributeCollection->setPageSize($searchCriteria->getPageSize());
$attributes = [];
/** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
foreach ($attributeCollection as $attribute) {
$attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode());
}
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
$searchResults->setItems($attributes);
$searchResults->setTotalCount($totalCount);
return $searchResults;
}
示例3: save
/**
* {@inheritdoc}
*/
public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
{
$qty = $cartItem->getQty();
if (!is_numeric($qty) || $qty <= 0) {
throw InputException::invalidFieldValue('qty', $qty);
}
$cartId = $cartItem->getQuoteId();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$itemId = $cartItem->getItemId();
try {
/** update item qty */
if (isset($itemId)) {
$cartItem = $quote->getItemById($itemId);
if (!$cartItem) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item %2', $cartId, $itemId));
}
$product = $this->productRepository->get($cartItem->getSku());
$cartItem->setData('qty', $qty);
} else {
$product = $this->productRepository->get($cartItem->getSku());
$quote->addProduct($product, $qty);
}
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
if ($e instanceof NoSuchEntityException) {
throw $e;
}
throw new CouldNotSaveException(__('Could not save quote'));
}
return $quote->getItemByProduct($product);
}
示例4: assertInputExceptionMessages
/**
* Assert for presence of Input exception messages
*
* @param InputException $e
*/
private function assertInputExceptionMessages($e)
{
$this->assertEquals(InputException::DEFAULT_MESSAGE, $e->getMessage());
$errors = $e->getErrors();
$this->assertCount(2, $errors);
$this->assertEquals('username is a required field.', $errors[0]->getLogMessage());
$this->assertEquals('password is a required field.', $errors[1]->getLogMessage());
}
示例5: assertInputExceptionMessages
/**
* Assert for presence of Input exception messages
*
* @param InputException $e
*/
private function assertInputExceptionMessages($e)
{
$this->assertEquals('One or more input exceptions have occurred.', $e->getMessage());
$errors = $e->getErrors();
$this->assertCount(2, $errors);
$this->assertEquals('username is a required field.', $errors[0]->getLogMessage());
$this->assertEquals('password is a required field.', $errors[1]->getLogMessage());
}
示例6: inputException
/**
* {@inheritdoc}
*/
public function inputException($wrappedErrorParameters)
{
$exception = new InputException();
if ($wrappedErrorParameters) {
foreach ($wrappedErrorParameters as $error) {
$exception->addError(__('Invalid value of "%value" provided for the %fieldName field.', ['fieldName' => $error->getFieldName(), 'value' => $error->getValue()]));
}
}
throw $exception;
}
示例7: inputException
/**
* {@inheritdoc}
*/
public function inputException($wrappedErrorParameters)
{
$exception = new InputException();
if ($wrappedErrorParameters) {
foreach ($wrappedErrorParameters as $error) {
$exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => $error->getFieldName(), 'value' => $error->getValue()]));
}
}
throw $exception;
}
示例8: save
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
{
$qty = $cartItem->getQty();
if (!is_numeric($qty) || $qty <= 0) {
throw InputException::invalidFieldValue('qty', $qty);
}
$cartId = $cartItem->getQuoteId();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$itemId = $cartItem->getItemId();
try {
/** update item */
if (isset($itemId)) {
$item = $quote->getItemById($itemId);
if (!$item) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item %2', $cartId, $itemId));
}
$productType = $item->getProduct()->getTypeId();
$buyRequestData = $this->getBuyRequest($productType, $cartItem);
if (is_object($buyRequestData)) {
/** update item product options */
/** @var \Magento\Quote\Model\Quote\Item $cartItem */
$cartItem = $quote->updateItem($itemId, $buyRequestData);
} else {
/** update item qty */
$item->setData('qty', $qty);
}
} else {
/** add item to shopping cart */
$product = $this->productRepository->get($cartItem->getSku());
$productType = $product->getTypeId();
/** @var \Magento\Quote\Model\Quote\Item|string $cartItem */
$cartItem = $quote->addProduct($product, $this->getBuyRequest($productType, $cartItem));
if (is_string($cartItem)) {
throw new \Magento\Framework\Exception\LocalizedException(__($cartItem));
}
}
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
if ($e instanceof NoSuchEntityException || $e instanceof LocalizedException) {
throw $e;
}
throw new CouldNotSaveException(__('Could not save quote'));
}
$itemId = $cartItem->getId();
foreach ($quote->getAllItems() as $quoteItem) {
if ($itemId == $quoteItem->getId()) {
$cartItem = $this->addProductOptions($productType, $quoteItem);
return $this->applyCustomOptions($cartItem);
}
}
throw new CouldNotSaveException(__('Could not save quote'));
}
示例9: save
/**
* @param CartInterface $quote
* @param CartItemInterface $item
* @return CartItemInterface
* @throws CouldNotSaveException
* @throws InputException
* @throws LocalizedException
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function save(CartInterface $quote, CartItemInterface $item)
{
/** @var \Magento\Quote\Model\Quote $quote */
$qty = $item->getQty();
if (!is_numeric($qty) || $qty <= 0) {
throw InputException::invalidFieldValue('qty', $qty);
}
$cartId = $item->getQuoteId();
$itemId = $item->getItemId();
try {
/** Update existing item */
if (isset($itemId)) {
$currentItem = $quote->getItemById($itemId);
if (!$currentItem) {
throw new NoSuchEntityException(__('Cart %1 does not contain item %2', $cartId, $itemId));
}
$productType = $currentItem->getProduct()->getTypeId();
$buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item);
if (is_object($buyRequestData)) {
/** Update item product options */
$item = $quote->updateItem($itemId, $buyRequestData);
} else {
if ($item->getQty() !== $currentItem->getQty()) {
$currentItem->setQty($qty);
}
}
} else {
/** add new item to shopping cart */
$product = $this->productRepository->get($item->getSku());
$productType = $product->getTypeId();
$item = $quote->addProduct($product, $this->cartItemOptionProcessor->getBuyRequest($productType, $item));
if (is_string($item)) {
throw new LocalizedException(__($item));
}
}
} catch (NoSuchEntityException $e) {
throw $e;
} catch (LocalizedException $e) {
throw $e;
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not save quote'));
}
$itemId = $item->getId();
foreach ($quote->getAllItems() as $quoteItem) {
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
if ($itemId == $quoteItem->getId()) {
$item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem);
return $this->cartItemOptionProcessor->applyCustomOptions($item);
}
}
throw new CouldNotSaveException(__('Could not save quote'));
}
示例10: getList
/**
* {@inheritdoc}
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
$attributeSetId = $this->retrieveAttributeSetIdFromSearchCriteria($searchCriteria);
if (!$attributeSetId) {
throw InputException::requiredField('attribute_set_id');
}
try {
$this->setRepository->get($attributeSetId);
} catch (\Exception $exception) {
throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
}
$collection = $this->groupListFactory->create();
$collection->setAttributeSetFilter($attributeSetId);
$collection->setSortOrder();
$searchResult = $this->searchResultsFactory->create();
$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setItems($collection->getItems());
$searchResult->setTotalCount($collection->getSize());
return $searchResult;
}
示例11: updateItem
/**
* {@inheritdoc}
*/
public function updateItem($cartId, $itemId, \Magento\Checkout\Service\V1\Data\Cart\Item $data)
{
$qty = $data->getQty();
if (!is_numeric($qty) || $qty <= 0) {
throw InputException::invalidFieldValue('qty', $qty);
}
/** @var \Magento\Sales\Model\Quote $quote */
$quote = $this->quoteRepository->get($cartId);
$quoteItem = $quote->getItemById($itemId);
if (!$quoteItem) {
throw new NoSuchEntityException("Cart {$cartId} doesn't contain item {$itemId}");
}
$quoteItem->setData('qty', $qty);
try {
$quote->collectTotals()->save();
} catch (\Exception $e) {
throw new CouldNotSaveException('Could not update quote item');
}
return true;
}
示例12: create
/**
* {@inheritdoc}
*/
public function create($entityTypeCode, AttributeSetInterface $attributeSet, $skeletonId)
{
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
if ($attributeSet->getId() !== null) {
throw InputException::invalidFieldValue('id', $attributeSet->getId());
}
if ($skeletonId == 0) {
throw InputException::invalidFieldValue('skeletonId', $skeletonId);
}
// Make sure that skeleton attribute set is valid (try to load it)
$this->repository->get($skeletonId);
try {
$attributeSet->setEntityTypeId($this->eavConfig->getEntityType($entityTypeCode)->getId());
$attributeSet->validate();
} catch (\Exception $exception) {
throw new InputException(__($exception->getMessage()));
}
$this->repository->save($attributeSet);
$attributeSet->initFromSkeleton($skeletonId);
return $this->repository->save($attributeSet);
}
示例13: setProductLinks
/**
* {@inheritdoc}
*/
public function setProductLinks($sku, array $items)
{
$linkTypes = $this->linkTypeProvider->getLinkTypes();
// Check if product link type is set and correct
if (!empty($items)) {
foreach ($items as $newLink) {
$type = $newLink->getLinkType();
if ($type == null) {
throw InputException::requiredField("linkType");
}
if (!isset($linkTypes[$type])) {
throw new NoSuchEntityException(__('Provided link type "%1" does not exist', $type));
}
}
}
$product = $this->productRepository->get($sku);
// Replace only links of the specified type
$existingLinks = $product->getProductLinks();
$newLinks = [];
if (!empty($existingLinks)) {
foreach ($existingLinks as $link) {
if ($link->getLinkType() != $type) {
$newLinks[] = $link;
}
}
$newLinks = array_merge($newLinks, $items);
} else {
$newLinks = $items;
}
$product->setProductLinks($newLinks);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('Invalid data provided for linked products'));
}
return true;
}
示例14: save
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
{
if ($saveOptions) {
$productOptions = $product->getProductOptions();
}
$isDeleteOptions = $product->getIsDeleteOptions();
$tierPrices = $product->getData('tier_price');
$productId = $this->resourceModel->getIdBySku($product->getSku());
$ignoreLinksFlag = $product->getData('ignore_links_flag');
$productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
$productLinks = null;
if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
$productLinks = $product->getProductLinks();
}
$product = $this->initializeProductData($productDataArray, empty($productId));
if (isset($productDataArray['options'])) {
if (!empty($productDataArray['options']) || $isDeleteOptions) {
$this->processOptions($product, $productDataArray['options']);
$product->setCanSaveCustomOptions(true);
}
}
$this->processLinks($product, $productLinks);
if (isset($productDataArray['media_gallery_entries'])) {
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
}
$validationResult = $this->resourceModel->validate($product);
if (true !== $validationResult) {
throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
}
try {
if ($saveOptions) {
$product->setProductOptions($productOptions);
$product->setCanSaveCustomOptions(true);
}
if ($tierPrices !== null) {
$product->setData('tier_price', $tierPrices);
}
$this->resourceModel->save($product);
} catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
}
unset($this->instances[$product->getSku()]);
unset($this->instancesById[$product->getId()]);
return $this->get($product->getSku());
}
示例15: validate
/**
* Validate tax rate
*
* @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
* @throws InputException
* @return void
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
{
$exception = new InputException();
$countryCode = $taxRate->getTaxCountryId();
if (!\Zend_Validate::is($countryCode, 'NotEmpty')) {
$exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id']));
} elseif (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) {
$exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode]));
}
$regionCode = $taxRate->getTaxRegionId();
// if regionCode eq 0 (all regions *), do not validate with existing region list
if (\Zend_Validate::is($regionCode, 'NotEmpty') && $regionCode != "0" && !\Zend_Validate::is($this->regionFactory->create()->load($regionCode)->getId(), 'NotEmpty')) {
$exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'region_id', 'value' => $regionCode]));
}
if (!\Zend_Validate::is($taxRate->getRate(), 'NotEmpty')) {
$exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'percentage_rate']));
}
if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) {
$exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'code']));
}
if ($taxRate->getZipIsRange()) {
$zipRangeFromTo = ['zip_from' => $taxRate->getZipFrom(), 'zip_to' => $taxRate->getZipTo()];
foreach ($zipRangeFromTo as $key => $value) {
if (!is_numeric($value) || $value < 0) {
$exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => $key, 'value' => $value]));
}
}
if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) {
$exception->addError(__('Range To should be equal or greater than Range From.'));
}
} else {
if (!\Zend_Validate::is(trim($taxRate->getTaxPostcode()), 'NotEmpty')) {
$exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode']));
}
}
if ($exception->wasErrorAdded()) {
throw $exception;
}
}