imevul/scopefilter

Lightweight array filtering library that uses 'scopes' to define filters.

1.0.2 2013-07-16 17:22 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

ScopeFilter

A lightweight PHP data filtering library. Easily filters object properties depending on specified scopes. Also has support for JsonSerializable.

Flattr

License

MIT (http://opensource.org/licenses/MIT)

To do

Nothing! We're done, for now. If you find a bug or have an awesome idea, be sure to open an issue!

Requirements and limitations

  • PHP v5.3+
  • Classes must implement the Scoped interface and use annotations to define the scopes.

Installation

You can either download the code and include the ScopeFilter/ScopeFilter.php file, or if you're using Composer then simply add the following requirement to your composer.json file:

{
	"require": {
		"imevul/scopefilter": "dev-default"
	}
}

This will add the latest source as a dependency to your project and autoload it via the Composer autoload feature. If you want to make sure you have a stable release, you can specify the version number instead. For example:

{
	"require": {
		"imevul/scopefilter": ">=1.1.1"
	}
}

Check the Tags page for the latest version.

Usage

Using the filter is extremely simple. Just implement the Scoped interface, add the @Scope({}) annotation to your properties. Then run the following code to filter:

$filter = new ScopeFilter();
$filter->filter($myObject, array("some.scope1", "some.scope2"));

This will return an associative array of data that is filtered according to the scopes you passed to the method. Any children will also be recursively converted, using either ScopeFilter->filter() or JsonSerializable->jsonSerialize(). You can also filter any other data structure, but any non-Scoped objects will be returned unmodified. Arrays will also be iterated over and each item run through the filter.

For an example of annotation usage, see the file /tests/ScopeFilterTest.php.

Finally

If you find any bugs or have an idea that you think fits in this project, feel free to open an issue and/or send a pull request.

Changelog

2013-07-16 - v1.0.2

  • Added support for filtering any kind of input. If it's an array of Scoped objects, then all of them will be filtered.
  • Filtering non-Scoped data will now return the unmodified data. This means you can have arrays with mixed Scoped/Non-Scoped entities.

2013-06-25 - v1.0.1

  • Converted all files to Unix line endings.
  • Fixed vendor/autoload problem.
  • Added custom PHPCS ruleset.
  • Replaced phpunit.xml with phpunit.xml.dist.

2013-06-24 - v1.0.0

  • Initial release. Fully working library.