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.
Beispiel #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, '/');
Beispiel #2
0
 public function testTo()
 {
     Collection::formats('lithium\\net\\http\\Media');
     $result = new MockResult(array('data' => array(array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'))));
     $doc = new DocumentSet(array('model' => $this->_model, 'result' => $result));
     $expected = array('4c8f86167675abfabdbf0300' => array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), '5c8f86167675abfabdbf0301' => array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), '6c8f86167675abfabdbf0302' => array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array'));
     $expected = array(array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array', array('indexed' => false)));
 }
 public function testRecordWithCombinedPkAndLazyLoading()
 {
     $records = array(array('client_id' => 1, 'invoice_id' => 4, 'title' => 'Payment1'), array('client_id' => 2, 'invoice_id' => 5, 'title' => 'Payment2'), array('client_id' => 2, 'invoice_id' => 6, 'title' => 'Payment3'), array('client_id' => 4, 'invoice_id' => 7, 'title' => 'Payment3'));
     $result = new MockResult(array('records' => $records));
     $payments = new MockMultiKeyRecordSet(array('result' => $result, 'model' => $this->_model2));
     $this->assertCount(0, $payments->get('_data'));
     $result = $payments[array('client_id' => 1, 'invoice_id' => 4)]->to('array');
     $this->assertEqual($records[0], $result);
     $result = $payments[array('client_id' => 2, 'invoice_id' => 6)]->to('array');
     $this->assertEqual($records[2], $result);
     $this->assertCount(3, $payments->get('_data'));
     $result = $payments[array('client_id' => 2, 'invoice_id' => 5)]->to('array');
     $this->assertEqual($records[1], $result);
     $this->assertCount(3, $payments->get('_data'));
     $this->assertNull($payments[array('client_id' => 3, 'invoice_id' => 3)]);
     $this->assertNull($payments[array('client_id' => 2)]);
     $this->assertNull($payments[array('invoice_id' => 6)]);
     $this->assertCount(4, $payments->get('_data'));
     $this->assertEqual($records, $payments->to('array'));
     $expected = '[{"client_id":1,"invoice_id":4,"title":"Payment1"},';
     $expected .= '{"client_id":2,"invoice_id":5,"title":"Payment2"},';
     $expected .= '{"client_id":2,"invoice_id":6,"title":"Payment3"},';
     $expected .= '{"client_id":4,"invoice_id":7,"title":"Payment3"}]';
     Collection::formats('lithium\\net\\http\\Media');
     $this->assertEqual($expected, $payments->to('json'));
 }
Beispiel #4
0
 /**
  * Tests that various types of handlers can be registered with `Collection::formats()`, and
  * that collection instances are converted correctly.
  *
  * @return void
  */
 public function testCollectionFormatConversion()
 {
     Collection::formats('lithium\\net\\http\\Media');
     $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
     $collection = new Collection(compact('data'));
     $expected = json_encode($data);
     $result = $collection->to('json');
     $this->assertEqual($expected, $result);
     $this->assertNull($collection->to('badness'));
     Collection::formats(false);
     $this->assertNull($collection->to('json'));
     Collection::formats('json', function ($collection, $options) {
         return json_encode($collection->to('array'));
     });
     $result = $collection->to('json');
     $this->assertEqual($expected, $result);
     $result = $collection->to(function ($collection) {
         $value = array_map(function ($i) {
             return is_array($i) ? join(',', $i) : $i;
         }, $collection->to('array'));
         return join(',', $value);
     });
     $expected = 'hello,goodbye,bar,dib';
     $this->assertEqual($expected, $result);
 }
Beispiel #5
0
 public function testToInternal()
 {
     Collection::formats('lithium\\net\\http\\Media');
     $expected = array(array('id' => 1, 'data' => 'data1'), array('id' => 2, 'data' => 'data2'), array('id' => 3, 'data' => 'data3'), array('id' => 4, 'data' => 'data4'));
     $this->assertEqual($expected, $this->_recordSet->to('array', array('indexed' => false)));
     $expected = '{"1":{"id":1,"data":"data1"},"2":{"id":2,"data":"data2"},';
     $expected .= '"3":{"id":3,"data":"data3"},"4":{"id":4,"data":"data4"}}';
     $this->assertEqual($expected, $this->_recordSet->to('json'));
     $expected = '[{"id":1,"data":"data1"},{"id":2,"data":"data2"},';
     $expected .= '{"id":3,"data":"data3"},{"id":4,"data":"data4"}]';
     $result = $this->_recordSet->to('json', array('indexed' => false));
     $this->assertEqual($expected, $result);
 }
Beispiel #6
0
	public function testTo() {
		Collection::formats('\lithium\net\http\Media');

		$this->assertFalse(isset($this->_recordSet[0]));
		$expected = array(
			1 => array('id' => 1, 'data' => 'data1'),
			2 => array('id' => 2, 'data' => 'data2'),
			3 => array('id' => 3, 'data' => 'data3'),
			4 => array('id' => 4, 'data' => 'data4')
		);
		$this->assertEqual($expected, $this->_recordSet->to('array'));

		$expected = '{"1":{"id":1,"data":"data1"},"2":{"id":2,"data":"data2"},'
			. '"3":{"id":3,"data":"data3"},"4":{"id":4,"data":"data4"}}';
		$this->assertEqual($expected, $this->_recordSet->to('json'));
	}
 public function testCollectionFormatConversion()
 {
     Collection::formats('\\lithium\\http\\Media');
     $items = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
     $collection = new Collection(compact('items'));
     $expected = json_encode($items);
     $result = $collection->to('json');
     $this->assertEqual($result, $expected);
     $this->assertNull($collection->to('badness'));
 }
Beispiel #8
0
 * (`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\core\Libraries;
use \lithium\net\http\Media;

Dispatcher::applyFilter('_callable', function($self, $params, $chain) {