root/fORMJSON.php
| Revision 242, 4.6 kB (checked in by wbond, 2 years ago) |
|---|
| Line | Hide Line Numbers |
|---|---|
| 1 | |
| 2 | /** |
| 3 | * Adds JSON functionality to fActiveRecord and fRecordSet |
| 4 | * |
| 5 | * @copyright Copyright (c) 2008 William Bond |
| 6 | * @author William Bond [wb] <will@flourishlib.com> |
| 7 | * @license http://flourishlib.com/license |
| 8 | * |
| 9 | * @package Flourish |
| 10 | * @link http://flourishlib.com/fORMJSON |
| 11 | * |
| 12 | * @version 1.0.0b |
| 13 | * @changes 1.0.0b The initial implementation [wb, 2008-06-25] |
| 14 | */ |
| 15 | class fORMJSON |
| 16 | { |
| 17 | /** |
| 18 | * Adds the method toJSON() to fActiveRecord and fRecordSet instances |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | static public function extend() |
| 23 | { |
| 24 | fORM::registerReflectCallback( |
| 25 | '*', |
| 26 | array('fORMJSON', 'reflect') |
| 27 | ); |
| 28 | |
| 29 | fORM::registerHookCallback( |
| 30 | '*', |
| 31 | 'replace::toJSON()', |
| 32 | array('fORMJSON', 'toJSON') |
| 33 | ); |
| 34 | |
| 35 | fRecordSet::registerMethodCallback( |
| 36 | 'toJSON', |
| 37 | array('fORMJSON', 'toJSONRecordSet') |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Adjusts the {@link fActiveRecord::reflect()} signatures of columns that have been added by this class |
| 44 | * |
| 45 | * @internal |
| 46 | * |
| 47 | * @param string $class The class to reflect |
| 48 | * @param array &$signatures The associative array of {method name} => {signature} |
| 49 | * @param boolean $include_doc_comments If doc comments should be included with the signature |
| 50 | * @return void |
| 51 | */ |
| 52 | static public function reflect($class, &$signatures, $include_doc_comments) |
| 53 | { |
| 54 | $signature = ''; |
| 55 | if ($include_doc_comments) { |
| 56 | $signature .= "/**\n"; |
| 57 | $signature .= " * Converts the values from the record into a JSON object\n"; |
| 58 | $signature .= " * \n"; |
| 59 | $signature .= " * @return string The JSON object representation of this record\n"; |
| 60 | $signature .= " */\n"; |
| 61 | } |
| 62 | $signature .= 'public function toJSON()'; |
| 63 | |
| 64 | $signatures['toJSON'] = $signature; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Returns a JSON object representation of the record |
| 70 | * |
| 71 | * @internal |
| 72 | * |
| 73 | * @param fActiveRecord $object The fActiveRecord instance |
| 74 | * @param array &$values The current values |
| 75 | * @param array &$old_values The old values |
| 76 | * @param array &$related_records Any records related to this record |
| 77 | * @param string &$method_name The method that was called |
| 78 | * @param array &$parameters The parameters passed to the method |
| 79 | * @return string The JSON object that represents the values of this record |
| 80 | */ |
| 81 | static public function toJSON($object, &$values, &$old_values, &$related_records, &$method_name, &$parameters) |
| 82 | { |
| 83 | $output = array(); |
| 84 | foreach ($values as $column => $value) { |
| 85 | if (is_object($value) && is_callable(array($value, '__toString'))) { |
| 86 | $value = $value->__toString(); |
| 87 | } elseif (is_object($value)) { |
| 88 | $value = (string) $value; |
| 89 | } |
| 90 | $output[$column] = $value; |
| 91 | } |
| 92 | |
| 93 | return fJSON::encode($output); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Returns a JSON object representation of a record set |
| 99 | * |
| 100 | * @internal |
| 101 | * |
| 102 | * @param fRecordSet $record_set The fRecordSet instance |
| 103 | * @param string $class The class of the records |
| 104 | * @param array &$records The fActiveRecord objects |
| 105 | * @param integer &$pointer The current iteration pointer |
| 106 | * @param boolean &$associate If the record set should be associated with any containing fActiveRecord |
| 107 | * @return string The JSON object that represents an array of all of the fActiveRecord objects |
| 108 | */ |
| 109 | static public function toJSONRecordSet($record_set, $class, &$records, &$pointer, &$associate) |
| 110 | { |
| 111 | return '[' . join(',', $record_set->call('toJSON')) . ']'; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Forces use as a static class |
| 117 | * |
| 118 | * @return fORMJSON |
| 119 | */ |
| 120 | private function __construct() { } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Copyright (c) 2008 William Bond <will@flourishlib.com> |
| 127 | * |
| 128 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 129 | * of this software and associated documentation files (the "Software"), to deal |
| 130 | * in the Software without restriction, including without limitation the rights |
| 131 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 132 | * copies of the Software, and to permit persons to whom the Software is |
| 133 | * furnished to do so, subject to the following conditions: |
| 134 | * |
| 135 | * The above copyright notice and this permission notice shall be included in |
| 136 | * all copies or substantial portions of the Software. |
| 137 | * |
| 138 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 139 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 140 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 141 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 142 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 143 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 144 | * THE SOFTWARE. |
| 145 | */ |
