mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
701 stars 207 forks source link

array() -> ArrayObject ? #22

Open IngwiePhoenix opened 11 years ago

IngwiePhoenix commented 11 years ago

Hey.

Since I am coding on a portable PHP application, I can not relay on the PECL extension and have come along to find this very nice and functional bit of PHP.

My goal is to use objects instead of arrays in most places - especially ArrayObjects since these are a "hybrid" of both.

Which places in the class would I have to re-write a bit to turn the whole thing into an ArrayObject instead of regular array? Thanks!

Regards, Ingwie.

ryanuber commented 11 years ago

Hey @IngwiePhoenix,

ArrayObjects can be had easily. You can probably accomplish this by changing line 475, in loadWithSource():

index 7650c7c..fe73dcf 100644
--- a/Spyc.php
+++ b/Spyc.php
@@ -472,7 +472,7 @@ class Spyc {
       $this->delayedPath = array();

     }
-    return $this->result;
+    return new ArrayObject($this->result);
   }

   private function loadFromSource ($input) {

Then you can do this:

php > require 'Spyc.php';
php > print get_class(Spyc::YAMLLoad("- blah\n"));
ArrayObject
mustangostang commented 10 years ago

Of course, I won't be able to merge this changeset it — the default behaviour for parsing YAML lists/hashes is definitely a simple PHP array. However, I feel there should be a good way to support SPL objects in Spyc. How about we make some support for tags, and prepending the list declaration in YAML with !php/ArrayObject will do the trick. @ryanuber, I would very much like to hear your thoughts.

ryanuber commented 10 years ago

Hey Vlad,

The tags idea could work, but one problem I could see with that is the return type can be modified by the YAML document itself. This would force the programmer to check the return type before trying to use the returned data.

What do you think about doing something similar to json_decode ? The second (optional) argument to the loader could be $assoc and have a bool value. When false, the function could return an SplObjectStorage type, and when true, the typical Array primitive.

I think you are correct, an Array primitive is the expected default return value, even though the default in json_decode is an object (it is probably misused commonly because of this). I really like that this library makes YAML as easy as JSON in PHP and even uses a similar naming convention for top-level functions. It's good for adoption.

IngwiePhoenix commented 8 years ago

Oi, this issue be old. But time to revisit the past! (not like we had the BTTF day this year, right? :) )

So, I just revisited this issue because I want to move from INI to Yaml, since it is naturally laid out to be more structured. However, PHP arrays are ok, but if almost the entire code uses Objects now and a library passes by that only returns an Array, that leads me to the question: how to convert? And ontop of that, Symfony's own Yaml parser is broken, as the "enable object support" flag is simply ignored.

Here is what I would suggest: Add a flag.

if($objectSupportEnabled) {
    $output = new ArrayObject;
} else {
    $output = array();
}

I am not a hundred percent sure of Spyc's source, since I get confused easily by parsers and the like. But it would allow for Spyc to keep it's default, but support the flexibility to return an object as well.

IngwiePhoenix commented 8 years ago

And there I just realized, @ryanuber had suggested a similar idea. Oops! (: Sorry. But, you get what I was wanting to say.