Skip to content

Conversation

@rdohms
Copy link
Contributor

@rdohms rdohms commented Jul 2, 2015

Mixed value, as per phpdoc standard, returns any value without altering it.

  • added mixedValue function
  • added tests with multiple value types

Mixed value, as per phpdoc standard, returns any value without altering it.
codeliner pushed a commit that referenced this pull request Jul 2, 2015
Support for Mixed Value Retrieval

Fixes #2
@codeliner codeliner merged commit c1be1b8 into codeliner:master Jul 2, 2015
@codeliner
Copy link
Owner

@rdohms Thx for your PR 👍 It fixed #2 and I merged it but one question is left open: What should happen when the path exists but the value is NULL and the user has provided a default other than NULL? I tend to return the default in this case (like the other Value methods do) but Mixed include ``NULLas a valid return type so maybe we need to perform anarray_key_exists` check here. What do you think?

@mikemix
Copy link

mikemix commented Jul 2, 2015

Just a reminder

$array = [
    'key' => null,
];

var_dump(isset($array['key']));

What will be the output? :) Anyway, in my opinion if the requested key does not exist, exception should be thrown. Null should be returned only if the key exists and its value is null.

@codeliner
Copy link
Owner

@mikemix Thx for the isset hint, array_key_exists seems to be the better choice :-) I ❤️ PHP ... sometimes. I've updated my question accordingly.

if the requested key does not exist, exception should be thrown

Hhm, yeah I think you're right. I've created the array reader to have a tool to inspect unknown arrays without running into errors. Throwing an exception doesn't match that criteria in the first place but when I want to avoid exceptions - well then I have to provide a default. I'll create a new issue to add exceptions.

@rdohms
Copy link
Contributor Author

rdohms commented Jul 2, 2015

Yeah i would agree, throw exceptions, catch and return default if default is set, bubble exception otherwise.

However, i can think of an edge case here also. What if i define a default value of null, how do you know if i have set default or not?

@codeliner
Copy link
Owner

What if i define a default value of null, how do you know if i have set default or not?

Good catch! This mixedValue method becomes complex 😱

function test_default($default = null) {
    return func_num_args();
}

echo 'No args: ' . test_default() . PHP_EOL;
echo 'With non null default: ' . test_default('foo')  . PHP_EOL;
echo 'With null default: ' . test_default(null)  . PHP_EOL;

//No args: 0
//With non null default: 1
//With null default: 1

... will do the trick

@mikemix
Copy link

mikemix commented Jul 2, 2015

One can have two readers then: ThrowingExceptionsReader and SilentReader (decorator for the former) 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants