diff --git a/README.md b/README.md index 2655b8a..699210b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## Requirements -`Skrz\Meta` requires PHP `>= 5.3.0` and Symfony `>= 2.7.0`. +`Skrz\Meta` requires PHP `>= 5.4.0` and Symfony `>= 2.7.0`. ## Installation @@ -166,6 +166,40 @@ var_export($someCategory->name === "Some category"); // TRUE ``` +### Fields + +- Fields represent set of symbolic field paths. +- They are composite (fields can have sub-fields). +- Fields can be supplied as `$filter` parameters in `to*()` methods. + +```php +use Skrz\API\Category; +use Skrz\API\Meta\CategoryMeta; +use Skrz\Meta\Fields\Fields; + +$parentCategory = new Category(); +$parentCategory->name = "The parent category"; +$parentCategory->slug = "parent-category"; + +$childCategory = new Category(); +$childCategory->name = "The child category"; +$childCategory->slug = "child-category"; +$childCategory->parentCategory = $parentCategory; + + +var_export(CategoryMeta::toArray($childCategory, null, Fields::fromString("name,parentCategory{name}"))); +// array( +// "name" => "The child category", +// "parentCategory" => array( +// "name" => "The parent category", +// ), +// ) +``` + +Fields are inspired by: + +- [Facebook Graph API's `?fields=...` query parameter](https://developers.facebook.com/docs/graph-api/using-graph-api#fields) +- [Google Protocol Buffers' `FieldMask`](https://github.com/google/protobuf/blob/master/src/google/protobuf/field_mask.proto) (and its [JSON serialization](https://developers.google.com/protocol-buffers/docs/proto3#json)) ### Annotations diff --git a/bin/generate-reflection.php b/bin/generate-reflection.php index cbf2776..efd6a52 100755 --- a/bin/generate-reflection.php +++ b/bin/generate-reflection.php @@ -185,6 +185,8 @@ public function aMethod($aParameter = AClass::A_CONSTANT, AnInterface $anotherPa $method->getName() !== "getStaticProperties" && $method->getName() !== "isCloneable" && $method->getName() !== "isVariadic" && + $method->getName() !== "getType" && // TODO: PHP 7 + $method->getName() !== "isAnonymous" && // TODO: PHP 7 // $method->getName() !== "getDefaultProperties" && (strncmp($method->getName(), "get", 3) === 0 || ($is = (strncmp($method->getName(), "is", 2) === 0))) && ($method->getNumberOfParameters() === 0 || $method->getName() === "getProperties" || $method->getName() === "getMethods") diff --git a/composer.json b/composer.json index 712becd..8113336 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ } ], "require": { - "php": ">=5.3", + "php": ">=5.4", "doctrine/annotations": "~1.2", "nette/php-generator": "~2.3", - "symfony/finder": "~2.7", - "symfony/console": "~2.7" + "symfony/finder": "~2.7|~3.0", + "symfony/console": "~2.7|~3.0" }, "require-dev": { "phpunit/phpunit": "~4.6" diff --git a/gen-src/Google/Protobuf/Compiler/CodeGeneratorResponse/Meta/FileMeta.php b/gen-src/Google/Protobuf/Compiler/CodeGeneratorResponse/Meta/FileMeta.php index 4f7dfb2..c2d214c 100644 --- a/gen-src/Google/Protobuf/Compiler/CodeGeneratorResponse/Meta/FileMeta.php +++ b/gen-src/Google/Protobuf/Compiler/CodeGeneratorResponse/Meta/FileMeta.php @@ -250,7 +250,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorRequestMeta.php b/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorRequestMeta.php index 355104c..f72e09a 100644 --- a/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorRequestMeta.php +++ b/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorRequestMeta.php @@ -260,7 +260,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorResponseMeta.php b/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorResponseMeta.php index c7ce418..26f840e 100644 --- a/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorResponseMeta.php +++ b/gen-src/Google/Protobuf/Compiler/Meta/CodeGeneratorResponseMeta.php @@ -233,7 +233,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/DescriptorProto/Meta/ExtensionRangeMeta.php b/gen-src/Google/Protobuf/DescriptorProto/Meta/ExtensionRangeMeta.php index 9586a18..db49c57 100644 --- a/gen-src/Google/Protobuf/DescriptorProto/Meta/ExtensionRangeMeta.php +++ b/gen-src/Google/Protobuf/DescriptorProto/Meta/ExtensionRangeMeta.php @@ -210,7 +210,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/DescriptorProto/Meta/ReservedRangeMeta.php b/gen-src/Google/Protobuf/DescriptorProto/Meta/ReservedRangeMeta.php index ff35ef9..d6abb85 100644 --- a/gen-src/Google/Protobuf/DescriptorProto/Meta/ReservedRangeMeta.php +++ b/gen-src/Google/Protobuf/DescriptorProto/Meta/ReservedRangeMeta.php @@ -210,7 +210,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/DescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/DescriptorProtoMeta.php index 092390b..a0cb4ef 100644 --- a/gen-src/Google/Protobuf/Meta/DescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/DescriptorProtoMeta.php @@ -438,7 +438,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/EnumDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/EnumDescriptorProtoMeta.php index 3836f9e..431b136 100644 --- a/gen-src/Google/Protobuf/Meta/EnumDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/EnumDescriptorProtoMeta.php @@ -253,7 +253,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/EnumOptionsMeta.php b/gen-src/Google/Protobuf/Meta/EnumOptionsMeta.php index cbc4954..acbaf66 100644 --- a/gen-src/Google/Protobuf/Meta/EnumOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/EnumOptionsMeta.php @@ -236,7 +236,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/EnumValueDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/EnumValueDescriptorProtoMeta.php index 6f0eb24..3d54358 100644 --- a/gen-src/Google/Protobuf/Meta/EnumValueDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/EnumValueDescriptorProtoMeta.php @@ -240,7 +240,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/EnumValueOptionsMeta.php b/gen-src/Google/Protobuf/Meta/EnumValueOptionsMeta.php index 9d75398..efd08e7 100644 --- a/gen-src/Google/Protobuf/Meta/EnumValueOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/EnumValueOptionsMeta.php @@ -223,7 +223,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/FieldDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/FieldDescriptorProtoMeta.php index d0e8e59..fcc14cd 100644 --- a/gen-src/Google/Protobuf/Meta/FieldDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/FieldDescriptorProtoMeta.php @@ -367,7 +367,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/FieldOptionsMeta.php b/gen-src/Google/Protobuf/Meta/FieldOptionsMeta.php index f21e9f7..102c74f 100644 --- a/gen-src/Google/Protobuf/Meta/FieldOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/FieldOptionsMeta.php @@ -288,7 +288,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/FileDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/FileDescriptorProtoMeta.php index 22084c3..15f9e86 100644 --- a/gen-src/Google/Protobuf/Meta/FileDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/FileDescriptorProtoMeta.php @@ -459,7 +459,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/FileDescriptorSetMeta.php b/gen-src/Google/Protobuf/Meta/FileDescriptorSetMeta.php index 51e780e..ee55630 100644 --- a/gen-src/Google/Protobuf/Meta/FileDescriptorSetMeta.php +++ b/gen-src/Google/Protobuf/Meta/FileDescriptorSetMeta.php @@ -210,7 +210,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/FileOptionsMeta.php b/gen-src/Google/Protobuf/Meta/FileOptionsMeta.php index 410f0f1..5b722d6 100644 --- a/gen-src/Google/Protobuf/Meta/FileOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/FileOptionsMeta.php @@ -450,7 +450,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/MessageOptionsMeta.php b/gen-src/Google/Protobuf/Meta/MessageOptionsMeta.php index 96d335d..af46f3c 100644 --- a/gen-src/Google/Protobuf/Meta/MessageOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/MessageOptionsMeta.php @@ -262,7 +262,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/MethodDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/MethodDescriptorProtoMeta.php index 7cca9cd..5d0abb5 100644 --- a/gen-src/Google/Protobuf/Meta/MethodDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/MethodDescriptorProtoMeta.php @@ -297,7 +297,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/MethodOptionsMeta.php b/gen-src/Google/Protobuf/Meta/MethodOptionsMeta.php index fc6eb27..3644fd0 100644 --- a/gen-src/Google/Protobuf/Meta/MethodOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/MethodOptionsMeta.php @@ -223,7 +223,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/OneofDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/OneofDescriptorProtoMeta.php index 34f6271..3f0bd9d 100644 --- a/gen-src/Google/Protobuf/Meta/OneofDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/OneofDescriptorProtoMeta.php @@ -206,7 +206,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/ServiceDescriptorProtoMeta.php b/gen-src/Google/Protobuf/Meta/ServiceDescriptorProtoMeta.php index 48ce848..9bafca7 100644 --- a/gen-src/Google/Protobuf/Meta/ServiceDescriptorProtoMeta.php +++ b/gen-src/Google/Protobuf/Meta/ServiceDescriptorProtoMeta.php @@ -253,7 +253,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/ServiceOptionsMeta.php b/gen-src/Google/Protobuf/Meta/ServiceOptionsMeta.php index 4f43533..952f1d2 100644 --- a/gen-src/Google/Protobuf/Meta/ServiceOptionsMeta.php +++ b/gen-src/Google/Protobuf/Meta/ServiceOptionsMeta.php @@ -223,7 +223,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/SourceCodeInfoMeta.php b/gen-src/Google/Protobuf/Meta/SourceCodeInfoMeta.php index 42c3505..afdd44c 100644 --- a/gen-src/Google/Protobuf/Meta/SourceCodeInfoMeta.php +++ b/gen-src/Google/Protobuf/Meta/SourceCodeInfoMeta.php @@ -211,7 +211,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/Meta/UninterpretedOptionMeta.php b/gen-src/Google/Protobuf/Meta/UninterpretedOptionMeta.php index c9bd0ae..78abd4d 100644 --- a/gen-src/Google/Protobuf/Meta/UninterpretedOptionMeta.php +++ b/gen-src/Google/Protobuf/Meta/UninterpretedOptionMeta.php @@ -323,7 +323,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/SourceCodeInfo/Meta/LocationMeta.php b/gen-src/Google/Protobuf/SourceCodeInfo/Meta/LocationMeta.php index e12bcdd..a5142d7 100644 --- a/gen-src/Google/Protobuf/SourceCodeInfo/Meta/LocationMeta.php +++ b/gen-src/Google/Protobuf/SourceCodeInfo/Meta/LocationMeta.php @@ -311,7 +311,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Google/Protobuf/UninterpretedOption/Meta/NamePartMeta.php b/gen-src/Google/Protobuf/UninterpretedOption/Meta/NamePartMeta.php index 9aa49d7..59e8a0c 100644 --- a/gen-src/Google/Protobuf/UninterpretedOption/Meta/NamePartMeta.php +++ b/gen-src/Google/Protobuf/UninterpretedOption/Meta/NamePartMeta.php @@ -219,7 +219,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/ClassWithEmbeddedMessageProperty/Meta/EmbeddedMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/ClassWithEmbeddedMessageProperty/Meta/EmbeddedMeta.php index 7976220..f1bf108 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/ClassWithEmbeddedMessageProperty/Meta/EmbeddedMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/ClassWithEmbeddedMessageProperty/Meta/EmbeddedMeta.php @@ -185,7 +185,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -275,7 +275,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -393,7 +393,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithEmbeddedMessagePropertyMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithEmbeddedMessagePropertyMeta.php index e8d8440..48f3d9c 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithEmbeddedMessagePropertyMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithEmbeddedMessagePropertyMeta.php @@ -186,7 +186,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -276,7 +276,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -393,7 +393,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithFixed64PropertyMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithFixed64PropertyMeta.php index 5efc167..4731028 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithFixed64PropertyMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithFixed64PropertyMeta.php @@ -185,7 +185,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -275,7 +275,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -391,7 +391,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithNoPropertyMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithNoPropertyMeta.php index 7588661..0847a43 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithNoPropertyMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithNoPropertyMeta.php @@ -171,7 +171,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -251,7 +251,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -350,7 +350,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithStringPropertyMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithStringPropertyMeta.php index e05bed2..bb32717 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithStringPropertyMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithStringPropertyMeta.php @@ -185,7 +185,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -275,7 +275,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -393,7 +393,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithVarintPropertyMeta.php b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithVarintPropertyMeta.php index bee3344..60eb76f 100644 --- a/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithVarintPropertyMeta.php +++ b/gen-src/Skrz/Meta/Fixtures/Protobuf/Meta/ClassWithVarintPropertyMeta.php @@ -185,7 +185,7 @@ public static function fromArray($input, $group = NULL, $object = NULL) * * @return array */ - public static function toArray($object, $group = NULL, array $filter = NULL) + public static function toArray($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -275,7 +275,7 @@ public static function fromObject($input, $group = NULL, $object = NULL) * * @return object */ - public static function toObject($object, $group = NULL, array $filter = NULL) + public static function toObject($object, $group = NULL, $filter = NULL) { if ($object === null) { return null; @@ -384,7 +384,7 @@ public static function fromProtobuf($input, $object = NULL, &$start = 0, $end = * * @return string */ - public static function toProtobuf($object, array $filter = NULL) + public static function toProtobuf($object, $filter = NULL) { $output = ''; diff --git a/src/Skrz/Meta/Fields/Fields.php b/src/Skrz/Meta/Fields/Fields.php new file mode 100644 index 0000000..bcb80e3 --- /dev/null +++ b/src/Skrz/Meta/Fields/Fields.php @@ -0,0 +1,173 @@ + + */ +class Fields +{ + + /** + * Create empty {@link FieldsBuilderInterface} instance. + * + * @return FieldsBuilderInterface + */ + public static function builder() + { + return ScalarFields::getInstance(); + } + + /** + * Convert given array into fields. + * + * @param array $input + * @return FieldsBuilderInterface + */ + public static function fromArray($input = []) + { + $fields = ScalarFields::getInstance(); + + foreach ($input as $k => $v) { + if (is_array($v)) { + $fields = $fields->appendFields($k, self::fromArray($v)); + } elseif (!!$v) { + $fields = $fields->appendField($k); + } + } + + return $fields; + } + + /** + * Parse string into fields. + * + * @param $input + * @param int $start + * @param null $end + * @return FieldsBuilderInterface + */ + public static function fromString($input, $start = 0, $end = null) + { + return self::appendFromString(self::builder(), $input, $start, $end); + } + + /** + * Add fields from given string to existing field set. + * + * @param FieldsBuilderInterface $builder + * @param string $input + * @param int $start + * @param int $end + * @return FieldsBuilderInterface + */ + public static function appendFromString(FieldsBuilderInterface $builder, $input, $start = 0, $end = null) + { + if ($end === null) { + $end = strlen($input); + } + + while ($start < $end) { + $start = self::ltrim($input, $start, $end); + + if ($start >= $end) { + return $builder; + } + + $p = self::pos($input, $start, $end, ".{,"); + + if ($p === $end) { + $p = self::rtrim($input, $start, $p); + $builder = $builder->appendField(substr($input, $start, $p - $start)); + + $start = $end; + + } else { + $fieldName = substr($input, $start, self::rtrim($input, $start, $p) - $start); + + switch ($input[$p]) { + case ".": + for ($q = self::pos($input, $p, $end, "{,"); $q < $end && $input[$q] === "{"; ++$q) { + $q = self::findBalancedRightBrace($input, $q, $end); + } + $builder = $builder->appendFields( + $fieldName, + self::appendFromString($builder->builder($fieldName), $input, $p + 1, $q) + ); + + $start = $q + 1; + break; + + case "{": + $balancedRightBrace = self::findBalancedRightBrace($input, $p, $end); + if ($balancedRightBrace + 1 < $end && $input[$balancedRightBrace + 1] !== ",") { + throw new \InvalidArgumentException( + "Invalid fields string '{$input}' - expected ',' at position " . ($balancedRightBrace + 1) . "." + ); + } + $builder = $builder->appendFields( + $fieldName, + self::appendFromString($builder->builder($fieldName), $input, $p + 1, $balancedRightBrace) + ); + $start = $balancedRightBrace + 2; + break; + + case ",": + $builder = $builder->appendField($fieldName); + $start = $p + 1; + break; + + default: + throw new \LogicException("Unhandled char '{$input[$p]}'."); + } + } + } + + return $builder; + } + + private static function pos($input, $start, $end, $charsList) + { + while ($start < $end && strpos($charsList, $input[$start]) === false) { + ++$start; + } + return $start; + } + + private static function ltrim($input, $start, $end, $charsList = " \t\n\r\0\x0B") + { + while ($start < $end && strpos($charsList, $input[$start]) !== false) { + ++$start; + } + return $start; + } + + private static function rtrim($input, $start, $end, $charsList = " \t\n\r\0\x0B") + { + while ($end > $start && strpos($charsList, $input[$end - 1]) !== false) { + --$end; + } + return $end; + } + + private static function findBalancedRightBrace($input, $start, $end) + { + if ($input[$start] !== "{") { + throw new \InvalidArgumentException("Invalid fields string '{$input}' - no left brace at position {$start}."); + } + + for ($start = $start + 1, $n = 1; $start < $end; ++$start) { + if ($input[$start] === "{") { + ++$n; + } else if ($input[$start] === "}") { + if (--$n < 1) { + return $start; + } + } + } + + throw new \InvalidArgumentException("Invalid fields string '{$input}' - unbalanced braces at position {$start}."); + } + +} diff --git a/src/Skrz/Meta/Fields/FieldsBuilderInterface.php b/src/Skrz/Meta/Fields/FieldsBuilderInterface.php new file mode 100644 index 0000000..956d512 --- /dev/null +++ b/src/Skrz/Meta/Fields/FieldsBuilderInterface.php @@ -0,0 +1,36 @@ + + */ +interface FieldsBuilderInterface extends FieldsInterface +{ + + /** + * Set field to be 'scalar' - ie. it is not composite, has no child fields. + * + * @param string $fieldName + * @return FieldsBuilderInterface + */ + public function appendField($fieldName); + + /** + * Update field to given `$fields` instance. + * + * @param string $fieldName + * @param FieldsBuilderInterface $fields + * @return FieldsBuilderInterface + */ + public function appendFields($fieldName, FieldsBuilderInterface $fields); + + /** + * Return {@link FieldsInterface} from this builder - can optimize inner structure for further faster access. + * + * @return FieldsInterface + */ + public function build(); + +} diff --git a/src/Skrz/Meta/Fields/FieldsInterface.php b/src/Skrz/Meta/Fields/FieldsInterface.php new file mode 100644 index 0000000..e1c0c4a --- /dev/null +++ b/src/Skrz/Meta/Fields/FieldsInterface.php @@ -0,0 +1,44 @@ + + */ +interface FieldsInterface extends \Countable, \Iterator, \ArrayAccess +{ + + /** + * Returns true if given field name is in set. + * + * @param string $fieldName + * @return bool + */ + public function field($fieldName); + + /** + * Returns nested field set for given field name, if field name is not in set, it will return empty set. + * + * @param string $fieldName + * @return FieldsInterface + */ + public function fields($fieldName); + + /** + * If parameter `$fieldName` is not null, it wil return {@link FieldsBuilderInterface} for given field name, + * if the parameters is null, it returns {@link FieldsBuilderInterface} for whole set. + * + * @param string $fieldName + * @return FieldsBuilderInterface + */ + public function builder($fieldName = null); + + /** + * Serialize this field set into string. + * + * @return string + */ + public function __toString(); + +} diff --git a/src/Skrz/Meta/Fields/MapFields.php b/src/Skrz/Meta/Fields/MapFields.php new file mode 100644 index 0000000..2821d51 --- /dev/null +++ b/src/Skrz/Meta/Fields/MapFields.php @@ -0,0 +1,123 @@ + + */ +class MapFields implements FieldsBuilderInterface +{ + + /** @var FieldsBuilderInterface[] */ + private $map = []; + + public function appendField($fieldName) + { + $this->map[$fieldName] = ScalarFields::getInstance(); + return $this; + } + + public function appendFields($fieldName, FieldsBuilderInterface $fields) + { + $this->map[$fieldName] = $fields; + return $this; + } + + public function build() + { + return $this; + } + + public function field($fieldName) + { + return isset($this->map[$fieldName]); + } + + public function fields($fieldName) + { + if (isset($this->map[$fieldName])) { + return $this->map[$fieldName]; + } + + return ScalarFields::getInstance(); + } + + public function count() + { + return count($this->map); + } + + public function builder($fieldName = null) + { + if ($fieldName === null) { + return $this; + } else if (isset($this->map[$fieldName])) { + return $this->map[$fieldName]; + } + + return ScalarFields::getInstance(); + } + + public function current() + { + return current($this->map); + } + + public function next() + { + next($this->map); + } + + public function key() + { + return key($this->map); + } + + public function valid() + { + return !!$this->current(); + } + + public function rewind() + { + reset($this->map); + } + + public function offsetExists($offset) + { + return $this->field($offset); + } + + public function offsetGet($offset) + { + return $this->fields($offset); + } + + public function offsetSet($offset, $value) + { + throw new \LogicException("Cannot set field '{$offset}'."); + } + + public function offsetUnset($offset) + { + throw new \LogicException("Cannot unset field '{$offset}'."); + } + + public function __toString() + { + $s = ""; + + foreach ($this->map as $k => $v) { + if (!empty($s)) { + $s .= ","; + } + + $s .= $k; + if (count($v)) { + $s .= "{" . $v->__toString() . "}"; + } + } + + return $s; + } + +} diff --git a/src/Skrz/Meta/Fields/ScalarFields.php b/src/Skrz/Meta/Fields/ScalarFields.php new file mode 100644 index 0000000..db24693 --- /dev/null +++ b/src/Skrz/Meta/Fields/ScalarFields.php @@ -0,0 +1,113 @@ + + */ +class ScalarFields implements FieldsBuilderInterface +{ + + /** + * @var ScalarFields + */ + private static $instance; + + private function __construct() + { + } + + public static function getInstance() + { + if (self::$instance === null) { + self::$instance = new ScalarFields(); + } + + return self::$instance; + } + + public function appendField($fieldName) + { + $map = new MapFields(); + return $map->appendFields($fieldName, $this); + } + + public function appendFields($fieldName, FieldsBuilderInterface $fields) + { + $map = new MapFields(); + return $map->appendFields($fieldName, $fields); + } + + public function build() + { + return $this; + } + + public function field($fieldName) + { + return false; + } + + public function fields($fieldName) + { + return $this; + } + + public function count() + { + return 0; + } + + public function builder($fieldName = null) + { + return $this; + } + + public function current() + { + return null; + } + + public function next() + { + } + + public function key() + { + return null; + } + + public function valid() + { + return false; + } + + public function rewind() + { + } + + public function offsetExists($offset) + { + return $this->field($offset); + } + + public function offsetGet($offset) + { + return $this->fields($offset); + } + + public function offsetSet($offset, $value) + { + throw new \LogicException("Cannot set field '{$offset}'."); + } + + public function offsetUnset($offset) + { + throw new \LogicException("Cannot unset field '{$offset}'."); + } + + public function __toString() + { + return ""; + } + +} diff --git a/src/Skrz/Meta/JSON/JsonMetaInterface.php b/src/Skrz/Meta/JSON/JsonMetaInterface.php index 2e14134..90bfa47 100644 --- a/src/Skrz/Meta/JSON/JsonMetaInterface.php +++ b/src/Skrz/Meta/JSON/JsonMetaInterface.php @@ -1,10 +1,13 @@ */ -interface JsonMetaInterface +interface JsonMetaInterface extends MetaInterface { /** @@ -23,7 +26,7 @@ public static function fromJson($json, $group = JsonProperty::DEFAULT_GROUP, $ob * * @param object $object * @param string $group - * @param array|int $filterOrOptions + * @param array|FieldsInterface|int $filterOrOptions * @param int $options * * @return array @@ -46,7 +49,7 @@ public static function fromArrayOfJson($input, $group = JsonProperty::DEFAULT_GR * * @param object $object * @param string $group - * @param array|int $filterOrOptions + * @param array|FieldsInterface|int $filterOrOptions * @param int $options * * @return array diff --git a/src/Skrz/Meta/PHP/PhpMetaInterface.php b/src/Skrz/Meta/PHP/PhpMetaInterface.php index 8a70907..ce3223e 100644 --- a/src/Skrz/Meta/PHP/PhpMetaInterface.php +++ b/src/Skrz/Meta/PHP/PhpMetaInterface.php @@ -1,6 +1,7 @@ setStatic(true); $to->addParameter("object"); $to->addParameter("group")->setOptional(true); - $to->addParameter("filter")->setTypeHint("array")->setOptional(true); + $to->addParameter("filter")->setOptional(true); $to ->addDocument("Serializes \\{$type->getName()} to " . strtolower($what)) diff --git a/src/Skrz/Meta/Protobuf/ProtobufMetaInterface.php b/src/Skrz/Meta/Protobuf/ProtobufMetaInterface.php index 089417f..11bf39b 100644 --- a/src/Skrz/Meta/Protobuf/ProtobufMetaInterface.php +++ b/src/Skrz/Meta/Protobuf/ProtobufMetaInterface.php @@ -1,10 +1,13 @@ */ -interface ProtobufMetaInterface +interface ProtobufMetaInterface extends MetaInterface { /** @@ -23,10 +26,10 @@ public static function fromProtobuf($input, $object = null, &$start = 0, $end = * Serializes object state to Protocol Buffers message. * * @param object $object - * @param array $filter + * @param array|FieldsInterface $filter * * @return array */ - public static function toProtobuf($object, array $filter = null); + public static function toProtobuf($object, $filter = null); } diff --git a/src/Skrz/Meta/Protobuf/ProtobufModule.php b/src/Skrz/Meta/Protobuf/ProtobufModule.php index 519069b..0a02e85 100644 --- a/src/Skrz/Meta/Protobuf/ProtobufModule.php +++ b/src/Skrz/Meta/Protobuf/ProtobufModule.php @@ -356,7 +356,7 @@ public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Typ $toProtobuf = $class->addMethod("toProtobuf"); $toProtobuf->setStatic(true); $toProtobuf->addParameter("object"); - $toProtobuf->addParameter("filter", null)->setTypeHint("array"); + $toProtobuf->addParameter("filter", null); $toProtobuf ->addDocument("Serialized \\{$type->getName()} to Protocol Buffers message.") ->addDocument("") diff --git a/src/Skrz/Meta/XML/XmlMetaInterface.php b/src/Skrz/Meta/XML/XmlMetaInterface.php index bbd64fd..b604a82 100644 --- a/src/Skrz/Meta/XML/XmlMetaInterface.php +++ b/src/Skrz/Meta/XML/XmlMetaInterface.php @@ -1,7 +1,10 @@ assertCount(0, $fields); + } + + public function testFromArrayEmptyArray() + { + $fields = Fields::fromArray([ + + ]); + + $this->assertCount(0, $fields); + } + + public function testFromArraySingleField() + { + $fields = Fields::fromArray(["foo" => true]); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertFalse($fields->field("bar")); + $this->assertCount(0, $fields->fields("foo")); + } + + public function testFromArrayMultipleFields() + { + $fields = Fields::fromArray(["foo" => true, "bar" => 1]); + + $this->assertCount(2, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertTrue($fields->field("bar")); + $this->assertFalse($fields->field("baz")); + $this->assertCount(0, $fields->fields("foo")); + $this->assertCount(0, $fields->fields("bar")); + } + + public function testFromArraySingleNestedField() + { + $fields = Fields::fromArray(["foo" => ["bar" => 1]]); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertFalse($fields->field("bar")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + } + + public function testFromArrayMultipleNestedFields() + { + $fields = Fields::fromArray(["foo" => ["bar" => 1], "baz" => ["qux" => true]]); + + $this->assertCount(2, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertFalse($fields->field("bar")); + $this->assertTrue($fields->field("baz")); + $this->assertFalse($fields->field("qux")); + + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + + $this->assertCount(1, $fields->fields("baz")); + $this->assertTrue($fields->fields("baz")->field("qux")); + $this->assertCount(0, $fields->fields("baz")->fields("qux")); + } + + public function testFromStringEmptyString() + { + $fields = Fields::fromString(""); + + $this->assertCount(0, $fields); + $this->assertFalse($fields->field("foo")); + } + + public function testFromStringSingleField() + { + $fields = Fields::fromString("foo"); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(0, $fields->fields("foo")); + $this->assertFalse($fields->field("bar")); + } + + public function testFromStringMultipleFields() + { + $fields = Fields::fromString("foo,bar"); + + $this->assertCount(2, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(0, $fields->fields("foo")); + $this->assertTrue($fields->field("bar")); + $this->assertCount(0, $fields->fields("bar")); + $this->assertFalse($fields->field("baz")); + } + + public function testFromStringMultipleFieldsWhitespace() + { + $fields = Fields::fromString(" foo\t\t, bar \t"); + + $this->assertCount(2, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(0, $fields->fields("foo")); + $this->assertTrue($fields->field("bar")); + $this->assertCount(0, $fields->fields("bar")); + $this->assertFalse($fields->field("baz")); + } + + public function testFromStringDotNestedSingleField() + { + $fields = Fields::fromString("foo.bar"); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + } + + public function testFromStringDotNestedMultipleFields() + { + $fields = Fields::fromString("foo.bar,baz.qux,baz.kwanza"); + + $this->assertCount(2, $fields); + + $this->assertTrue($fields->field("foo")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + + $this->assertTrue($fields->field("baz")); + $this->assertCount(2, $fields->fields("baz")); + $this->assertTrue($fields->fields("baz")->field("qux")); + $this->assertCount(0, $fields->fields("baz")->fields("qux")); + $this->assertTrue($fields->fields("baz")->field("kwanza")); + $this->assertCount(0, $fields->fields("baz")->fields("kwanza")); + } + + public function testFromStringBraceNestedSingleField() + { + $fields = Fields::fromString("foo{bar}"); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + } + + public function testFromStringBraceNestedMultipleFields() + { + $fields = Fields::fromString("foo{bar},baz{qux,kwanza}"); + + $this->assertCount(2, $fields); + + $this->assertTrue($fields->field("foo")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")); + + $this->assertTrue($fields->field("baz")); + $this->assertCount(2, $fields->fields("baz")); + $this->assertTrue($fields->fields("baz")->field("qux")); + $this->assertCount(0, $fields->fields("baz")->fields("qux")); + $this->assertTrue($fields->fields("baz")->field("kwanza")); + $this->assertCount(0, $fields->fields("baz")->fields("kwanza")); + } + + public function testFromStringBraceDeepNestedField() + { + $fields = Fields::fromString("foo{bar{baz{qux}}}"); + + $this->assertCount(1, $fields); + $this->assertTrue($fields->field("foo")); + $this->assertCount(1, $fields->fields("foo")); + $this->assertTrue($fields->fields("foo")->field("bar")); + $this->assertCount(1, $fields->fields("foo")->fields("bar")); + $this->assertTrue($fields->fields("foo")->fields("bar")->field("baz")); + $this->assertCount(1, $fields->fields("foo")->fields("bar")->fields("baz")); + $this->assertTrue($fields->fields("foo")->fields("bar")->fields("baz")->field("qux")); + $this->assertCount(0, $fields->fields("foo")->fields("bar")->fields("baz")->fields("qux")); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFromStringNoCommaAfterRightBrace() + { + Fields::fromString("foo{bar}baz"); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFromStringUnbalancedBraces() + { + Fields::fromString("foo{bar"); + } + + /** + * @dataProvider fieldsStrings + */ + public function testToString($input, $expected) + { + $this->assertEquals($expected, (string)Fields::fromString($input)); + } + + public function fieldsStrings() + { + return [ + ["", ""], + ["foo,bar", "foo,bar"], + [" foo\t\t, bar \t", "foo,bar"], + ["foo.bar", "foo{bar}"], + ["foo.bar,baz.qux,baz.kwanza", "foo{bar},baz{qux,kwanza}"], + ["foo{bar}", "foo{bar}"], + ["foo{bar},baz{qux,kwanza}", "foo{bar},baz{qux,kwanza}"], + ["foo{bar{baz{qux}}}", "foo{bar{baz{qux}}}"], + ]; + } + +} diff --git a/test/Skrz/Meta/JsonModuleTest.php b/test/Skrz/Meta/JsonModuleTest.php index 28e4ef8..1bdf2ee 100644 --- a/test/Skrz/Meta/JsonModuleTest.php +++ b/test/Skrz/Meta/JsonModuleTest.php @@ -1,6 +1,7 @@ a = "foo1"; + $instance->b = "foo2"; + $instance->c = "foo3"; + $instance->d = "foo4"; + $instance->e = "foo5"; + + $this->assertEquals( + '{"a":"foo1","b":"foo2","c":"foo3","d":"foo4","e":"foo5"}', + ClassWithMorePropertiesMeta::toJson($instance) + ); + + $this->assertEquals( + '{"a":"foo1"}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromArray([ + "a" => true, + ])) + ); + + $this->assertEquals( + '{"b":"foo2","c":"foo3","d":"foo4"}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromArray([ + "b" => true, + "c" => true, + "d" => true, + ])) + ); + + $instance2 = new ClassWithMoreProperties(); + $instance2->a = "foo6"; + $instance->f = $instance2; + + $this->assertEquals( + '{"e":"foo5","f":{"a":"foo6","b":null}}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromArray([ + "e" => true, + "f" => [ + "a" => true, + "b" => true, + ], + ])) + ); + + $instance3 = new ClassWithMoreProperties(); + $instance3->a = "foo7"; + $instance->g = [$instance2, $instance3]; + + $this->assertEquals( + '{"g":[{"a":"foo6"},{"a":"foo7"}]}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromArray([ + "g" => [ + "a" => true, + ], + ])) + ); + } + + public function testClassWithMorePropertiesFilteredByFieldsFromString() + { + $instance = new ClassWithMoreProperties(); + $instance->a = "foo1"; + $instance->b = "foo2"; + $instance->c = "foo3"; + $instance->d = "foo4"; + $instance->e = "foo5"; + + $this->assertEquals( + '{"a":"foo1","b":"foo2","c":"foo3","d":"foo4","e":"foo5"}', + ClassWithMorePropertiesMeta::toJson($instance) + ); + + $this->assertEquals( + '{"a":"foo1"}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromString("a")) + ); + + $this->assertEquals( + '{"b":"foo2","c":"foo3","d":"foo4"}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromString("b,c,d")) + ); + + $instance2 = new ClassWithMoreProperties(); + $instance2->a = "foo6"; + $instance->f = $instance2; + + $this->assertEquals( + '{"e":"foo5","f":{"a":"foo6","b":null}}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromString("e,f{a,b}")) + ); + + $instance3 = new ClassWithMoreProperties(); + $instance3->a = "foo7"; + $instance->g = [$instance2, $instance3]; + + $this->assertEquals( + '{"g":[{"a":"foo6"},{"a":"foo7"}]}', + ClassWithMorePropertiesMeta::toJson($instance, null, Fields::fromString("g{a}")) + ); + } + } diff --git a/test/Skrz/Meta/PhpModuleTest.php b/test/Skrz/Meta/PhpModuleTest.php index 583b473..487f276 100644 --- a/test/Skrz/Meta/PhpModuleTest.php +++ b/test/Skrz/Meta/PhpModuleTest.php @@ -1,6 +1,7 @@ a = "foo1"; + $instance->b = "foo2"; + $instance->c = "foo3"; + $instance->d = "foo4"; + $instance->e = "foo5"; + + $this->assertEquals( + [ + "a" => "foo1", + "b" => "foo2", + "c" => "foo3", + "d" => "foo4", + "e" => "foo5", + "f" => null, + "g" => [], + ], + ClassWithMorePropertiesMeta::toArray($instance) + ); + + $this->assertEquals( + [ + "a" => "foo1", + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromArray([ + "a" => true, + ])) + ); + + $this->assertEquals( + [ + "b" => "foo2", + "c" => "foo3", + "d" => "foo4", + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromArray([ + "b" => true, + "c" => true, + "d" => true, + ])) + ); + + $instance2 = new ClassWithMoreProperties(); + $instance2->a = "foo6"; + $instance->f = $instance2; + + $this->assertEquals( + [ + "e" => "foo5", + "f" => [ + "a" => "foo6", + "b" => null, + ], + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromArray([ + "e" => true, + "f" => [ + "a" => true, + "b" => true, + ], + ])) + ); + + $instance3 = new ClassWithMoreProperties(); + $instance3->a = "foo7"; + $instance->g = [$instance2, $instance3]; + + $this->assertEquals( + [ + "g" => [ + [ + "a" => "foo6", + ], + [ + "a" => "foo7", + ] + ], + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromArray([ + "g" => [ + "a" => true, + ], + ])) + ); + } + + public function testClassWithMorePropertiesFilteredByFieldsFromString() + { + $instance = new ClassWithMoreProperties(); + $instance->a = "foo1"; + $instance->b = "foo2"; + $instance->c = "foo3"; + $instance->d = "foo4"; + $instance->e = "foo5"; + + $this->assertEquals( + [ + "a" => "foo1", + "b" => "foo2", + "c" => "foo3", + "d" => "foo4", + "e" => "foo5", + "f" => null, + "g" => [], + ], + ClassWithMorePropertiesMeta::toArray($instance) + ); + + $this->assertEquals( + [ + "a" => "foo1", + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromString("a")) + ); + + $this->assertEquals( + [ + "b" => "foo2", + "c" => "foo3", + "d" => "foo4", + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromString("b,c,d")) + ); + + $instance2 = new ClassWithMoreProperties(); + $instance2->a = "foo6"; + $instance->f = $instance2; + + $this->assertEquals( + [ + "e" => "foo5", + "f" => [ + "a" => "foo6", + "b" => null, + ], + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromString("e,f{a,b}")) + ); + + $instance3 = new ClassWithMoreProperties(); + $instance3->a = "foo7"; + $instance->g = [$instance2, $instance3]; + + $this->assertEquals( + [ + "g" => [ + [ + "a" => "foo6", + ], + [ + "a" => "foo7", + ] + ], + ], + ClassWithMorePropertiesMeta::toArray($instance, null, Fields::fromString("g{a}")) + ); + } + }