-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support different property paths for read and write #1
Conversation
9bbaa2a
to
8c54ab0
Compare
private $dataMapper; | ||
private $propertyAccessor; | ||
|
||
public function __construct(DataMapperInterface $dataMapper, ?PropertyAccessorInterface $propertyAccessor = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is nullable type-hint and null as default needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot the handling of null
to have this part inline with Symfony's built-in PropertyPathMapper
:
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
This is fixed now.
|
||
foreach ($forms as $form) { | ||
if (null !== $readPropertyPath = $form->getConfig()->getOption('read_property_path')) { | ||
$data = $this->propertyAccessor->getValue($data, $readPropertyPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this could be getValue on null
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
if (null !== $dataMapper = $builder->getDataMapper()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
early exit instead? we already know this is going to be more in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check
ef417ce
to
d7ac03c
Compare
$empty = null === $data || [] === $data; | ||
|
||
if (!$empty && !is_array($data) && !is_object($data)) { | ||
throw new UnexpectedTypeException($data, 'object, array or empty'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use null
instead of empty as someone might assume it also allows an empty string.
} | ||
} | ||
|
||
class Foo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move this dummy data to a separate file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can think about that when we need to reuse the class in other tests IMO. Right now I don't want to expose this dummy data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #7
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
||
class RichModelFormsTypeExtensionSpec extends ObjectBehavior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reason for having both phpspec and phpunit? I think mixing them might raise the barrier for future contributors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #2
a38e9bb
to
6f1c8dc
Compare
right now the extension expects that read and write path are used together, but do we really need to enforce that? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possible follow ups:
- change name of data mapper
- decouple read and write path
Right now I would stick with it. We can reconsider this decision when we have a clear use case where only a read or write path seems reasonable. We could even go one step further and throw an exception when the default |
6f1c8dc
to
cf0198d
Compare
This will resolve #3.