Skip to content
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 mapping properties in XML and YAML #814

Conversation

meyerbaptiste
Copy link
Member

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #739, api-platform/api-platform#140
License MIT
Doc PR N/A

This PR adds support of mapping for properties in XML and YAML.
I followed the recommendations from #421.

@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch from 71a3006 to c528c92 Compare October 21, 2016 13:11
Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!!

$attributes = [];
foreach ($element->attribute as $attribute) {
$value = isset($attribute->attribute[0]) ? $this->getAttributes($attribute) : (string) $attribute;
isset($attribute['name']) ? $attributes[(string) $attribute['name']] = $value : $attributes[] = $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this hard to read, I'm not found of ternary value attributions, maybe it's just me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this hard to read too. I think an if-else is just fine here.

$parseException->setParsedFile($path);

throw $parseException;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one, maybe we should add this to the other Yaml factories!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch from c528c92 to 5426fe1 Compare October 21, 2016 14:20
*/
private function update(PropertyMetadata $propertyMetadata, array $metadata) : PropertyMetadata
{
foreach (['description' => 'get', 'readable' => 'is', 'writable' => 'is', 'writableLink' => 'is', 'readableLink' => 'is', 'required' => 'is', 'identifier' => 'is', 'iri' => 'get', 'attributes' => 'get'] as $metadataKey => $accessorPrefix) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably split the array into multiple lines.


return [
'description' => (string) $property['description'] ?: null,
'readable' => 'true' === (string) $property['readable'] || 'false' === (string) $property['readable'] ? constant((string) $property['readable']) : null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using XmlUtils::phpize instead of this trick?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, (string) $property['readable'] can be '0' or '1' too according to the XSD spec.

(string) $resource['shortName'] ?? null,
(string) $resource['description'] ?? null,
(string) $resource['iri'] ?? null,
(string) $resource['shortName'] ?: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? What if the shortName key doesn't exist?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, anyway I thought this was tested but I've the feeling it is not and should be

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I am wrong but if an attribute does not exist in a \SimpleXMLElement class, its value is null so the result of this expression is never null (i.e. (string) null => '').

Example with $resource['foo'] which does not exist:

var_dump($resource); // class SimpleXMLElement
var_dump($resource['foo']); // NULL
var_dump((string) $resource['foo'] ?? null); // string(0) ""
var_dump((string) $resource['foo'] ?: null); // NULL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware of this behavior, nice catch!

@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch 9 times, most recently from 65c5cfd to 865cb42 Compare October 26, 2016 13:04
@meyerbaptiste
Copy link
Member Author

meyerbaptiste commented Oct 26, 2016

Updated with your changes @soyuka @dunglas @teohhanhui.

Travis failed due to a depreciation notice from Twig (see twigphp/Twig#2208). This will be fixed by the next release of symfony/twig-bundle (i.e. 3.1.6).

@dunglas
Copy link
Member

dunglas commented Oct 27, 2016

@meyerbaptiste can you use this trick twigphp/Twig#2208 (comment) to make Travis happy please?

@meyerbaptiste
Copy link
Member Author

I tested it yesterday, Travis failed again with --prefer-lowest.

@dunglas
Copy link
Member

dunglas commented Oct 27, 2016

You probably need to bump the minimal Twig version in composer.json to make Travis happy with --prefer-lowest.

@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch from 865cb42 to 1fede1a Compare October 27, 2016 10:08
@meyerbaptiste
Copy link
Member Author

It's okay @dunglas, 3.1.6 is now released.

return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
}

if ($parentPropertyMetadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null !== $parentPropertyMetadata

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but #770 (comment).

*/
private function handleNotFound(PropertyMetadata $parentPropertyMetadata = null, string $resourceClass, string $property) : PropertyMetadata
{
if ($parentPropertyMetadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null !== $parentPropertyMetadata

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


$properties = (new \DOMXPath($domDocument))->query(sprintf('//resources/resource[@class="%s"]/property[@name="%s"]', $resourceClass, $propertyName));

if (false === $properties || 0 >= $properties->length || null === $properties->item(0) || false === $property = simplexml_import_dom($properties->item(0))) {
Copy link
Contributor

@Simperfit Simperfit Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this for the sake of lisibility ?

if ( blabla
   || 
     blabla)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done @Simperfit.

@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch 4 times, most recently from 37c48e1 to 1a6afd1 Compare October 31, 2016 10:18
@meyerbaptiste meyerbaptiste force-pushed the add_xml_yml_property_configuration branch from 1a6afd1 to ef5d037 Compare October 31, 2016 10:35
@dunglas dunglas merged commit 2a4bca8 into api-platform:master Nov 2, 2016
@dunglas
Copy link
Member

dunglas commented Nov 2, 2016

Thanks @meyerbaptiste. Don't forget to update the docs please.
Btw, it would be nice to add snippet of JS on the website hiding alternatives to annotations by default like on the Symfony's website.

@meyerbaptiste meyerbaptiste deleted the add_xml_yml_property_configuration branch November 2, 2016 15:36
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
…operty_configuration

Support mapping properties in XML and YAML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants