0
0
Fork 0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2025-03-13 13:03:21 +00:00
salesagility_SuiteCRM/Api/V8/Param/Options/Fields.php

54 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2018-05-25 16:50:33 +00:00
<?php
namespace Api\V8\Param\Options;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
class Fields extends BaseOption
{
const REGEX_FIELD_PATTERN = '/[^\w\-,]/';
2018-05-25 16:50:33 +00:00
/**
* @inheritdoc
*
* @throws \InvalidArgumentException In case fields are invalid.
*/
public function add(OptionsResolver $resolver)
{
$resolver
->setDefined('fields')
->setAllowedTypes('fields', 'array')
2018-05-28 18:09:39 +00:00
->setAllowedValues('fields', $this->validatorFactory->createClosureForIterator([
2018-05-25 16:50:33 +00:00
new Assert\NotBlank(),
new Assert\Regex([
'pattern' => self::REGEX_FIELD_PATTERN,
'match' => false,
]),
], true))
2018-05-28 18:09:39 +00:00
->setNormalizer('fields', function (Options $options, $values) {
$bean = $this->beanManager->newBeanSafe(key($values));
2018-05-25 16:50:33 +00:00
$attributes = $bean->toArray();
$fields = explode(',', array_shift($values));
$invalidFields = array_filter($fields, function ($field) use ($attributes) {
return !array_key_exists($field, $attributes);
});
if ($invalidFields) {
throw new \InvalidArgumentException(
sprintf(
'The following field%s in %s module %s not found: %s',
count($invalidFields) > 1 ? 's' : '',
$bean->getObjectName(),
count($invalidFields) > 1 ? 'are' : 'is',
implode(', ', $invalidFields)
)
);
}
return $fields;
});
}
}