Add function where() by Erikvv · Pull Request #122 · lstrojny/functional-php · GitHub
Skip to content

Add function where()#122

Closed
Erikvv wants to merge 2 commits into
lstrojny:masterfrom
Erikvv:master
Closed

Add function where()#122
Erikvv wants to merge 2 commits into
lstrojny:masterfrom
Erikvv:master

Conversation

@Erikvv

@Erikvv Erikvv commented Feb 22, 2017

Copy link
Copy Markdown
Contributor

Issue #120

The file contains multiple functions, I don't think there is any other file which does this. Should I split it up?

Many extensions are possible. Support for ArrayAccess would be one.

It now throws an exception when a property is not found but I can imagine cases where it is better to return no match instead. Like in the case of dynamic properties or when checking for a nested property and the containing object is null.

Comment thread docs/functional-php.md

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Document weither it’s an "any properties" or "all properties" proposition?

Comment thread src/Functional/Where.php
* @param array $propertyMap
* @return bool
*/
function matches($object, array $propertyMap)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you inline the function so that we are not exposing it in the Functional\ namepace

@Erikvv Erikvv Apr 4, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Directly inlining it makes it too much of a mess.
The only thing I can think of is making it 2 closures inside the main function like so:

<?php

namespace Functional;

use Functional\Exceptions\InvalidArgumentException;

function where($objects, array $propertyMap)
{
    $readProperty = function($object, $property) {
        //...
    };

    $matches = function($object, array $propertyMap) use ($readProperty) {
        //...
    };

    //... implementation of where()
}

Comment thread src/Functional/Where.php
throw new \InvalidArgumentException(sprintf('Expected property or method %s in %s', $property, get_class($object)));
}
} elseif (is_array($object)) {
return $object[$property];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What if the property does not exist? Wouldn’t this issue a strict warning?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes it would. I could throw a OutOfBoundsException and catch it in matches() and return false for no match.

Comment thread src/Functional/Where.php
* @param $property
* @return mixed
*/
function read_property($object, $property)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you inline this function as well?

Comment thread src/Functional/Where.php
*/
function read_property($object, $property)
{
if (is_object($object)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Other scenarios that come to mind are magic methods (__isset and __get). Symfony’s property path component also supports isFoo() and getFoo() but that might be overkill.

@Erikvv Erikvv Apr 4, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think there is a good way to determine if a magic property exist. The object should have implemented an __isset() method but there is no guarantee that it does and it might have different behavior for null which currently works fine.

I suggest to just not support magic properties, maybe document it explicitly.

Comment thread src/Functional/Where.php
if (property_exists($object, $property)) {
return $object->$property;
} elseif (method_exists($object, $property)) {
return call_user_func([$object, $property]);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can be $object->{$property}().

Comment thread docs/functional-php.md


## zip()
## zip() & zip_all()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you split the zip_all() doc change into another PR?

@Erikvv Erikvv mentioned this pull request Apr 4, 2017
@Erikvv

Erikvv commented Apr 4, 2017

Copy link
Copy Markdown
Contributor Author

@Erikvv Erikvv closed this Apr 4, 2017
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.

2 participants