lithium\util\Collection::formats PHP Method

formats() public static method

The values assigned are used by Collection::to() to convert Collection instances into different formats, i.e. JSON. This can be accomplished in two ways. First, format handlers may be registered on a case-by-case basis, as in the following: Collection::formats('json', function($collection, $options) { return json_encode($collection->to('array')); }); You can also implement the above as a static class method, and register it as follows: Collection::formats('json', '\my\custom\Formatter::toJson'); Alternatively, you can implement a class that can handle several formats. This class must implement two static methods: - A formats() method, which returns an array indicating what formats it handles. - A to() method, which handles the actual conversion. Once a class implements these methods, it may be registered per the following: Collection::formats('\lithium\net\http\Media'); For reference on how to implement these methods, see the Media class. Once a handler is registered, any instance of Collection or a subclass can be converted to the format(s) supported by the class or handler, using the to() method.
See also: lithium\net\http\Media::to()
See also: lithium\net\http\Media::formats()
See also: lithium\util\Collection::to()
public static formats ( string $format, mixed $handler = null ) : mixed
$format string A string representing the name of the format that a `Collection` can be converted to. This corresponds to the `$format` parameter in the `to()` method. Alternatively, the fully-namespaced class name of a format-handler class.
$handler mixed If `$format` is the name of a format string, `$handler` should be the function that handles the conversion, either an anonymous function, or a reference to a method name in `"Class::method"` form. If `$format` is a class name, can be `null`.
return mixed Returns the value of the format handler assigned.
    public static function formats($format, $handler = null)
    {
        if ($format === false) {
            return static::$_formats = array('array' => 'lithium\\util\\Collection::toArray');
        }
        if ($handler === null && class_exists($format)) {
            return static::$_formats[] = $format;
        }
        return static::$_formats[$format] = $handler;
    }

Usage Example

Example #1
0
 * The `Collection` class, which serves as the base class for some of Lithium's data objects
 * (`RecordSet` and `Document`) provides a way to manage data collections in a very flexible and
 * intuitive way, using closures and SPL interfaces. The `to()` method allows a `Collection` (or
 * subclass) to be converted to another format, such as an array. The `Collection` class also allows
 * other classes to be connected as handlers to convert `Collection` objects to other formats.
 *
 * The following connects the `Media` class as a format handler, which allows `Collection`s to be
 * exported to any format with a handler provided by `Media`, i.e. JSON. This enables things like
 * the following:
 * {{{
 * $posts = Post::find('all');
 * return $posts->to('json');
 * }}}
 */
use lithium\util\Collection;
Collection::formats('lithium\\net\\http\\Media');
/**
 * This filter is a convenience method which allows you to automatically route requests for static
 * assets stored within active plugins. For example, given a JavaScript file `bar.js` inside the
 * `li3_foo` plugin installed in an application, requests to `http://app/path/li3_foo/js/bar.js`
 * will be routed to `/path/to/app/libraries/plugins/li3_foo/webroot/js/bar.js` on the filesystem.
 * In production, it is recommended that you disable this filter in favor of symlinking each
 * plugin's `webroot` directory into your main application's `webroot` directory, or adding routing
 * rules in your web server's configuration.
 */
// use lithium\action\Dispatcher;
// use lithium\action\Response;
// use lithium\net\http\Media;
//
// Dispatcher::applyFilter('_callable', function($self, $params, $chain) {
// 	$url = ltrim($params['request']->url, '/');
All Usage Examples Of lithium\util\Collection::formats