Ejemplo n.º 1
0
 /**
  * Accessor method for adding format handlers to instances and subclasses of `Collection`.
  * 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 lithium\net\http\Media::to()
  * @see lithium\net\http\Media::formats()
  * @see lithium\util\Collection::to()
  * @param string $format 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.
  * @param mixed $handler 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 (is_null($handler) && class_exists($format)) {
         return static::$_formats[] = $format;
     }
     return static::$_formats[$format] = $handler;
 }
Ejemplo n.º 2
0
 /**
  * Resets the `Media` class.
  */
 public static function reset()
 {
     static::$_formats = [];
     static::set('html', ['text/html', 'application/xhtml+xml']);
     static::set('text', ['text/plain']);
     static::set('json', ['application/json'], ['encode' => function ($data, $options = []) {
         $defaults = ['depth' => 512, 'flag' => 0];
         $options += $defaults;
         $result = json_encode($data, $options['flag'], $options['depth']);
         if (json_last_error() !== JSON_ERROR_NONE) {
             throw new InvalidArgumentException(json_last_error_msg());
         }
         return $result;
     }, 'decode' => function ($data, $options = []) {
         $defaults = ['array' => true, 'depth' => 512, 'flag' => 0];
         $options += $defaults;
         $result = json_decode($data, $options['array'], $options['depth'], $options['flag']);
         if (json_last_error() !== JSON_ERROR_NONE) {
             throw new InvalidArgumentException(json_last_error_msg());
         }
         return $result;
     }]);
     static::set('form', ['application/x-www-form-urlencoded', 'multipart/form-data'], ['encode' => 'http_build_query', 'decode' => function ($data) {
         $decoded = array();
         parse_str($data, $decoded);
         return $decoded;
     }]);
 }
Ejemplo n.º 3
0
 /**
  * Accessor method for adding format handlers to instances and subclasses of `Collection`.
  *
  * @param string $format
  * @param mixed $handler
  * @return mixed
  */
 public static function formats($format, $handler = null)
 {
     if ($format === false) {
         return static::$_formats = array();
     }
     if (is_null($handler) && class_exists($format)) {
         return static::$_formats[] = $format;
     }
     return static::$_formats[$format] = $handler;
 }
Ejemplo n.º 4
0
 /**
  * Accessor method for adding format handlers to `Collection` instances.
  *
  * 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:
  *
  * ```php
  * 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');
  * ```
  *
  * @see    Lead\Collection\Collection::to()
  * @param  string $format  A string representing the name of the format that a `Collection`
  *                         can be converted to. If `false`, reset the `$_formats` attribute.
  *                         If `null` return the content of the `$_formats` attribute.
  * @param  mixed  $handler The function that handles the conversion, either an anonymous function,
  *                         a fully namespaced class method or `false` to remove the `$format` handler.
  * @return mixed
  */
 public static function formats($format = null, $handler = null)
 {
     if ($format === null) {
         return static::$_formats;
     }
     if ($format === false) {
         return static::$_formats = ['array' => 'Lead\\Collection\\Collection::toArray'];
     }
     if ($handler === false) {
         unset(static::$_formats[$format]);
         return;
     }
     return static::$_formats[$format] = $handler;
 }
Ejemplo n.º 5
0
 /**
  * Accessor method for adding format handlers to `Collection` instances.
  *
  * 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:
  *
  * ```php
  * 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');
  * ```
  *
  * @param  string $format  A string representing the name of the format that a `Collection`
  *                         can be converted to. If `false`, reset the `$_formats` attribute.
  *                         If `null` return the content of the `$_formats` attribute.
  * @param  mixed  $handler The function that handles the conversion, either an anonymous function,
  *                         a fully namespaced class method or `false` to remove the `$format` handler.
  * @return mixed
  */
 public static function formats($format = null, $handler = null)
 {
     if (func_num_args() === 0) {
         return static::$_formats;
     }
     if (func_num_args() === 1) {
         return isset(static::$_formats[$format]) ? static::$_formats[$format] : null;
     }
     if ($format === false) {
         return static::$_formats = [];
     }
     if ($handler === false) {
         unset(static::$_formats[$format]);
         return;
     }
     return static::$_formats[$format] = $handler;
 }
Ejemplo n.º 6
0
 protected static function initializeFormats()
 {
     static::$_formats = array('HTML' => array('text/html', 'application/xhtml+xml'), 'JSON' => array('application/json', 'application/x-json', 'text/json'), 'XML' => array('text/xml', 'application/xml', 'application/x-xml'), 'JS' => array('application/javascript', 'application/x-javascript', 'text/javascript'), 'TXT' => array('text/plain'), 'FORM' => array('application/x-www-form-urlencoded'));
 }