gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 280 forks source link

Fix documentation for filtering for bean export #946

Closed krisives closed 2 months ago

krisives commented 2 months ago

Fixed documentation for export() for OODBBean to accurately reflect current implementation of whitelisting beans. The current documentation for export() reads as:

    /**
     * Exports the bean as an array.
     * This function exports the contents of a bean to an array and returns
     * the resulting array. Depending on the parameters you can also
     * export an entire graph of beans, apply filters or exclude meta data.
     *
     * Usage:
     *
     * <code>
     * $bookData = $book->export( TRUE, TRUE, FALSE, [ 'author' ] );
     * </code>
     *
     * The example above exports all bean properties to an array
     * called $bookData including its meta data, parent objects but without
     * any beans of type 'author'.
     *
     * @param boolean $meta    set to TRUE if you want to export meta data as well
     * @param boolean $parents set to TRUE if you want to export parents as well
     * @param boolean $onlyMe  set to TRUE if you want to export only this bean
     * @param array   $filters optional whitelist for export
     *
     * @return array
     */

With the relevant portion being (emphasis added):

The example above exports all bean properties to an array called $bookData including its meta data, parent objects but without any beans of type 'author'.

However, the implementation and the language of a "whitelist filter" doesn't seem to match that.

The current implementation does indeed implement a whitelist filter:

if ( $hasFilters ) {
    if ( !in_array( strtolower( $value->getMeta( 'type' ) ), $filters ) ) continue;
}

And indeed a simple test:

use RedBeanPHP\R as R;

R::setup("sqlite:./test.db");

$author = R::dispense('author');
$author->name = 'Some Guy';

$book = R::dispense('book');
$book->title = 'Every Book Makes Someone Mad';
$book->author = $author;

var_dump($book->export(false, false, true, ['author']));

Does indeed produce the expected results where the whitelist is respected:

array(3) {
  ["id"]=>
  int(0)
  ["title"]=>
  string(28) "Every Book Makes Someone Mad"
  ["author"]=>
  array(2) {
    ["id"]=>
    int(0)
    ["name"]=>
    string(8) "Some Guy"
  }
}
gabordemooij commented 2 months ago

Thank you for updating the documentation!