From 0a6d065daac72daa612e81ca50ff32b47897e4db Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Tue, 27 Mar 2012 16:01:01 +0400 Subject: [PATCH 1/6] #81 Add Static Enumeration feature --- core/Base/Enum.class.php | 113 +++ core/Form/Primitive.class.php | 24 + core/Form/Primitives/PrimitiveEnum.class.php | 85 ++ .../Primitives/PrimitiveEnumByValue.class.php | 48 + .../Primitives/PrimitiveEnumList.class.php | 113 +++ main/Base/LightMetaProperty.class.php | 14 +- main/Base/MimeType.class.php | 868 ++++++++++++++++++ main/Criteria/Criteria.class.php | 4 + meta/builders/EnumClassBuilder.class.php | 38 + meta/classes/MetaClassProperty.class.php | 2 + meta/classes/MetaConfiguration.class.php | 84 +- meta/dtd/meta.dtd | 2 + meta/patterns/EnumClassPattern.class.php | 46 + meta/patterns/SpookedEnumPattern.class.php | 30 + meta/types/ObjectType.class.php | 6 +- test/core/PrimitiveEnumTest.class.php | 42 + test/main/EnumerationTest.class.php | 10 + test/meta/config.meta.xml | 8 + test/misc/DAOTest.class.php | 3 + 19 files changed, 1530 insertions(+), 10 deletions(-) create mode 100644 core/Base/Enum.class.php create mode 100644 core/Form/Primitives/PrimitiveEnum.class.php create mode 100644 core/Form/Primitives/PrimitiveEnumByValue.class.php create mode 100644 core/Form/Primitives/PrimitiveEnumList.class.php create mode 100644 main/Base/MimeType.class.php create mode 100644 meta/builders/EnumClassBuilder.class.php create mode 100644 meta/patterns/EnumClassPattern.class.php create mode 100644 meta/patterns/SpookedEnumPattern.class.php create mode 100644 test/core/PrimitiveEnumTest.class.php diff --git a/core/Base/Enum.class.php b/core/Base/Enum.class.php new file mode 100644 index 0000000000..b6bf412439 --- /dev/null +++ b/core/Base/Enum.class.php @@ -0,0 +1,113 @@ +setId($id); + } + + /// prevent's serialization of names' array + //@{ + public function serialize() + { + return (string) $this->id; + } + + public function unserialize($serialized) + { + $this->setId($serialized); + } + //@} + + public static function getList() + { + return static::$names; + } + + /** + * must return any existent ID + * 1 should be ok for most enumerations + **/ + public static function getAnyId() + { + return 1; + } + + /// parent's getId() is too complex in our case + public function getId() + { + return $this->id; + } + + public static function getObjectList() + { + $list = array(); + $names = static::$names; + + foreach (array_keys($names) as $id) + $list[] = static::create($id); + + return $list; + } + + public function toString() + { + return $this->name; + } + + public static function getNameList() + { + return static::$names; + } + + /** + * @return Enum + **/ + public function setId($id) + { + $names = static::$names; + + if (isset($names[$id])) { + $this->id = $id; + $this->name = $names[$id]; + } else + throw new MissingElementException( + 'knows nothing about such id == '.$id + ); + + return $this; + } + } +?> \ No newline at end of file diff --git a/core/Form/Primitive.class.php b/core/Form/Primitive.class.php index beb3d364ad..0d94b18a1d 100644 --- a/core/Form/Primitive.class.php +++ b/core/Form/Primitive.class.php @@ -362,5 +362,29 @@ public static function ipRange($name) { return new PrimitiveIpRange($name); } + + /** + * @return PrimitiveEnum + **/ + public static function enum($name) + { + return new PrimitiveEnum($name); + } + + /** + * @return PrimitiveEnumByValue + **/ + public static function enumByValue($name) + { + return new PrimitiveEnumByValue($name); + } + + /** + * @return PrimitiveEnumList + **/ + public static function enumList($name) + { + return new PrimitiveEnumList($name); + } } ?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveEnum.class.php b/core/Form/Primitives/PrimitiveEnum.class.php new file mode 100644 index 0000000000..33deface4e --- /dev/null +++ b/core/Form/Primitives/PrimitiveEnum.class.php @@ -0,0 +1,85 @@ +value) + return ClassUtils::callStaticMethod(get_class($this->value).'::getObjectList'); + elseif ($this->default) + return ClassUtils::callStaticMethod(get_class($this->default).'::getObjectList'); + else { + $object = new $this->className( + ClassUtils::callStaticMethod($this->className.'::getAnyId') + ); + + return $object->getObjectList(); + } + + Assert::isUnreachable(); + } + + /** + * @throws WrongArgumentException + * @return PrimitiveEnum + **/ + public function of($class) + { + $className = $this->guessClassName($class); + + Assert::classExists($className); + + Assert::isInstance($className, 'Enum'); + + $this->className = $className; + + return $this; + } + + public function importValue(/* Identifiable */ $value) + { + if ($value) + Assert::isEqual(get_class($value), $this->className); + else + return parent::importValue(null); + + return $this->import(array($this->getName() => $value->getId())); + } + + public function import($scope) + { + if (!$this->className) + throw new WrongStateException( + "no class defined for PrimitiveEnum '{$this->name}'" + ); + + $result = parent::import($scope); + + if ($result === true) { + try { + $this->value = new $this->className($this->value); + } catch (MissingElementException $e) { + $this->value = null; + + return false; + } + + return true; + } + + return $result; + } + } +?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveEnumByValue.class.php b/core/Form/Primitives/PrimitiveEnumByValue.class.php new file mode 100644 index 0000000000..ac85b6537d --- /dev/null +++ b/core/Form/Primitives/PrimitiveEnumByValue.class.php @@ -0,0 +1,48 @@ +className) + throw new WrongStateException( + "no class defined for PrimitiveEnum '{$this->name}'" + ); + + if (isset($scope[$this->name])) { + $scopedValue = urldecode($scope[$this->name]); + + $names = ClassUtils::callStaticMethod($this->className.'::getNameList'); + + foreach ($names as $key => $value) { + if ($value == $scopedValue) { + try { + $this->value = new $this->className($key); + } catch (MissingElementException $e) { + $this->value = null; + return false; + } + + return true; + } + } + + return false; + } + + return null; + } + } +?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveEnumList.class.php b/core/Form/Primitives/PrimitiveEnumList.class.php new file mode 100644 index 0000000000..26180b5727 --- /dev/null +++ b/core/Form/Primitives/PrimitiveEnumList.class.php @@ -0,0 +1,113 @@ +value = array(); + + return $this; + } + + /** + * @return PrimitiveEnumList + **/ + public function setValue(/* Enum */ $value) + { + if ($value) { + Assert::isArray($value); + Assert::isInstance(current($value), 'Enum'); + } + + $this->value = $value; + + return $this; + } + + public function importValue($value) + { + if (is_array($value)) { + try { + Assert::isInteger(current($value)); + + return $this->import( + array($this->name => $value) + ); + } catch (WrongArgumentException $e) { + return $this->import( + array($this->name => ArrayUtils::getIdsArray($value)) + ); + } + } + + return parent::importValue($value); + } + + public function import($scope) + { + if (!$this->className) + throw new WrongStateException( + "no class defined for PrimitiveIdentifierList '{$this->name}'" + ); + + if (!BasePrimitive::import($scope)) + return null; + + if (!is_array($scope[$this->name])) + return false; + + $list = array_unique($scope[$this->name]); + + $values = array(); + + foreach ($list as $id) { + if (!Assert::checkInteger($id)) + return false; + + $values[] = $id; + } + + $objectList = array(); + + foreach ($values as $value) { + $className = $this->className; + $objectList[] = new $className($value); + } + + if (count($objectList) == count($values)) { + $this->value = $objectList; + return true; + } + + return false; + } + + public function exportValue() + { + if (!$this->value) + return null; + + return ArrayUtils::getIdsArray($this->value); + } + } +?> \ No newline at end of file diff --git a/main/Base/LightMetaProperty.class.php b/main/Base/LightMetaProperty.class.php index e3a9225c54..548e33963a 100644 --- a/main/Base/LightMetaProperty.class.php +++ b/main/Base/LightMetaProperty.class.php @@ -113,6 +113,7 @@ public static function fill( || ($type == 'identifier') // obsoleted || ($type == 'integerIdentifier') || ($type == 'enumeration') + || ($type == 'enum') ) { $property->min = self::$limits[$size][0]; $property->max = self::$limits[$size][1]; @@ -405,7 +406,10 @@ public function toValue(ProtoDAO $dao = null, $array, $prefix = null) // BOVM: prevents segfault on >=php-5.2.5 Assert::classExists($this->className); - if (!is_subclass_of($this->className, 'Enumeration')) { + if ( + !is_subclass_of($this->className, 'Enumeration') || + !is_subclass_of($this->className, 'Enum') + ) { $remoteDao = call_user_func(array($this->className, 'dao')); $joinPrefix = $remoteDao->getJoinPrefix( @@ -507,7 +511,13 @@ final public function toString() public function isFormless() { // NOTE: enum here formless types - return ($this->type == 'enumeration'); + return in_array( + $this->type, + array( + 'enumeration', + 'enum', + ) + ); } } ?> \ No newline at end of file diff --git a/main/Base/MimeType.class.php b/main/Base/MimeType.class.php new file mode 100644 index 0000000000..d6b4d1fd39 --- /dev/null +++ b/main/Base/MimeType.class.php @@ -0,0 +1,868 @@ + 'application/andrew-inset', + 2 => 'application/annodex', + 3 => 'application/atom+xml', + 4 => 'application/atomcat+xml', + 5 => 'application/atomserv+xml', + 6 => 'application/bbolin', + 7 => 'application/cap', + 8 => 'application/cu-seeme', + 9 => 'application/davmount+xml', + 10 => 'application/dsptype', + 11 => 'application/ecmascript', + 12 => 'application/futuresplash', + 13 => 'application/hta', + 14 => 'application/java-archive', + 15 => 'application/java-serialized-object', + 16 => 'application/java-vm', + 17 => 'application/javascript', + 18 => 'application/m3g', + 19 => 'application/mac-binhex40', + 20 => 'application/mac-compactpro', + 21 => 'application/mathematica', + 22 => 'application/msaccess', + 23 => 'application/msword', + 24 => 'application/mxf', + 25 => 'application/octet-stream', + 26 => 'application/oda', + 27 => 'application/ogg', + 28 => 'application/pdf', + 29 => 'application/pgp-keys', + 30 => 'application/pgp-signature', + 31 => 'application/pics-rules', + 32 => 'application/postscript', + 33 => 'application/rar', + 34 => 'application/rdf+xml', + 35 => 'application/rss+xml', + 36 => 'application/rtf', + 37 => 'application/smil', + 38 => 'application/xhtml+xml', + 39 => 'application/xml', + 40 => 'application/xspf+xml', + 41 => 'application/zip', + 42 => 'application/vnd.android.package-archive', + 43 => 'application/vnd.cinderella', + 44 => 'application/vnd.google-earth.kml+xml', + 45 => 'application/vnd.google-earth.kmz', + 46 => 'application/vnd.mozilla.xul+xml', + 47 => 'application/vnd.ms-excel', + 48 => 'application/vnd.ms-pki.seccat', + 49 => 'application/vnd.ms-pki.stl', + 50 => 'application/vnd.ms-powerpoint', + 51 => 'application/vnd.oasis.opendocument.chart', + 52 => 'application/vnd.oasis.opendocument.database', + 53 => 'application/vnd.oasis.opendocument.formula', + 54 => 'application/vnd.oasis.opendocument.graphics', + 55 => 'application/vnd.oasis.opendocument.graphics-template', + 56 => 'application/vnd.oasis.opendocument.image', + 57 => 'application/vnd.oasis.opendocument.presentation', + 58 => 'application/vnd.oasis.opendocument.presentation-template', + 59 => 'application/vnd.oasis.opendocument.spreadsheet', + 60 => 'application/vnd.oasis.opendocument.spreadsheet-template', + 61 => 'application/vnd.oasis.opendocument.text', + 62 => 'application/vnd.oasis.opendocument.text-master', + 63 => 'application/vnd.oasis.opendocument.text-template', + 64 => 'application/vnd.oasis.opendocument.text-web', + 65 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 66 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', + 67 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 68 => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', + 69 => 'application/vnd.openxmlformats-officedocument.presentationml.template', + 70 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 71 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', + 72 => 'application/vnd.rim.cod', + 73 => 'application/vnd.smaf', + 74 => 'application/vnd.stardivision.calc', + 75 => 'application/vnd.stardivision.chart', + 76 => 'application/vnd.stardivision.draw', + 77 => 'application/vnd.stardivision.impress', + 78 => 'application/vnd.stardivision.math', + 79 => 'application/vnd.stardivision.writer', + 80 => 'application/vnd.stardivision.writer-global', + 81 => 'application/vnd.sun.xml.calc', + 82 => 'application/vnd.sun.xml.calc.template', + 83 => 'application/vnd.sun.xml.draw', + 84 => 'application/vnd.sun.xml.draw.template', + 85 => 'application/vnd.sun.xml.impress', + 86 => 'application/vnd.sun.xml.impress.template', + 87 => 'application/vnd.sun.xml.math', + 88 => 'application/vnd.sun.xml.writer', + 89 => 'application/vnd.sun.xml.writer.global', + 90 => 'application/vnd.sun.xml.writer.template', + 91 => 'application/vnd.symbian.install', + 92 => 'application/vnd.visio', + 93 => 'application/vnd.wap.wbxml', + 94 => 'application/vnd.wap.wmlc', + 95 => 'application/vnd.wap.wmlscriptc', + 96 => 'application/vnd.wordperfect', + 97 => 'application/vnd.wordperfect5.1', + 98 => 'application/x-123', + 99 => 'application/x-7z-compressed', + 100 => 'application/x-abiword', + 101 => 'application/x-apple-diskimage', + 102 => 'application/x-bcpio', + 103 => 'application/x-bittorrent', + 104 => 'application/x-cab', + 105 => 'application/x-cbr', + 106 => 'application/x-cbz', + 107 => 'application/x-cdf', + 108 => 'application/x-cdlink', + 109 => 'application/x-chess-pgn', + 110 => 'application/x-cpio', + 111 => 'application/x-csh', + 112 => 'application/x-debian-package', + 113 => 'application/x-director', + 114 => 'application/x-dms', + 115 => 'application/x-doom', + 116 => 'application/x-dvi', + 117 => 'application/x-httpd-eruby', + 118 => 'application/x-font', + 119 => 'application/x-freemind', + 120 => 'application/x-futuresplash', + 121 => 'application/x-gnumeric', + 122 => 'application/x-go-sgf', + 123 => 'application/x-graphing-calculator', + 124 => 'application/x-gtar', + 125 => 'application/x-hdf', + 126 => 'application/x-httpd-php', + 127 => 'application/x-httpd-php-source', + 128 => 'application/x-httpd-php3', + 129 => 'application/x-httpd-php3-preprocessed', + 130 => 'application/x-httpd-php4', + 131 => 'application/x-httpd-php5', + 132 => 'application/x-ica', + 133 => 'application/x-info', + 134 => 'application/x-internet-signup', + 135 => 'application/x-iphone', + 136 => 'application/x-iso9660-image', + 137 => 'application/x-jam', + 138 => 'application/x-java-jnlp-file', + 139 => 'application/x-jmol', + 140 => 'application/x-kchart', + 141 => 'application/x-killustrator', + 142 => 'application/x-koan', + 143 => 'application/x-kpresenter', + 144 => 'application/x-kspread', + 145 => 'application/x-kword', + 146 => 'application/x-latex', + 147 => 'application/x-lha', + 148 => 'application/x-lyx', + 149 => 'application/x-lzh', + 150 => 'application/x-lzx', + 151 => 'application/x-maker', + 152 => 'application/x-mif', + 153 => 'application/x-ms-wmd', + 154 => 'application/x-ms-wmz', + 155 => 'application/x-msdos-program', + 156 => 'application/x-msi', + 157 => 'application/x-netcdf', + 158 => 'application/x-ns-proxy-autoconfig', + 159 => 'application/x-nwc', + 160 => 'application/x-object', + 161 => 'application/x-oz-application', + 162 => 'application/x-pkcs7-certreqresp', + 163 => 'application/x-pkcs7-crl', + 164 => 'application/x-python-code', + 165 => 'application/x-qgis', + 166 => 'application/x-quicktimeplayer', + 167 => 'application/x-redhat-package-manager', + 168 => 'application/x-ruby', + 169 => 'application/x-sh', + 170 => 'application/x-shar', + 171 => 'application/x-shockwave-flash', + 172 => 'application/x-silverlight', + 173 => 'application/x-stuffit', + 174 => 'application/x-sv4cpio', + 175 => 'application/x-sv4crc', + 176 => 'application/x-tar', + 177 => 'application/x-tcl', + 178 => 'application/x-tex-gf', + 179 => 'application/x-tex-pk', + 180 => 'application/x-texinfo', + 181 => 'application/x-troff', + 182 => 'application/x-troff-man', + 183 => 'application/x-troff-me', + 184 => 'application/x-troff-ms', + 185 => 'application/x-ustar', + 186 => 'application/x-wais-source', + 187 => 'application/x-wingz', + 188 => 'application/x-x509-ca-cert', + 189 => 'application/x-xcf', + 190 => 'application/x-xfig', + 191 => 'application/x-xpinstall', + 192 => 'audio/amr', + 193 => 'audio/amr-wb', + 194 => 'audio/amr', + 195 => 'audio/amr-wb', + 196 => 'audio/annodex', + 197 => 'audio/basic', + 198 => 'audio/flac', + 199 => 'audio/midi', + 200 => 'audio/mpeg', + 201 => 'audio/mpegurl', + 202 => 'audio/ogg', + 203 => 'audio/prs.sid', + 204 => 'audio/x-aiff', + 205 => 'audio/x-gsm', + 206 => 'audio/x-mpegurl', + 207 => 'audio/x-ms-wma', + 208 => 'audio/x-ms-wax', + 209 => 'audio/x-pn-realaudio', + 210 => 'audio/x-realaudio', + 211 => 'audio/x-scpls', + 212 => 'audio/x-sd2', + 213 => 'audio/x-wav', + 214 => 'chemical/x-alchemy', + 215 => 'chemical/x-cache', + 216 => 'chemical/x-cache-csf', + 217 => 'chemical/x-cactvs-binary', + 218 => 'chemical/x-cdx', + 219 => 'chemical/x-cerius', + 220 => 'chemical/x-chem3d', + 221 => 'chemical/x-chemdraw', + 222 => 'chemical/x-cif', + 223 => 'chemical/x-cmdf', + 224 => 'chemical/x-cml', + 225 => 'chemical/x-compass', + 226 => 'chemical/x-crossfire', + 227 => 'chemical/x-csml', + 228 => 'chemical/x-ctx', + 229 => 'chemical/x-cxf', + 230 => 'chemical/x-daylight-smiles', + 231 => 'chemical/x-embl-dl-nucleotide', + 232 => 'chemical/x-galactic-spc', + 233 => 'chemical/x-gamess-input', + 234 => 'chemical/x-gaussian-checkpoint', + 235 => 'chemical/x-gaussian-cube', + 236 => 'chemical/x-gaussian-input', + 237 => 'chemical/x-gaussian-log', + 238 => 'chemical/x-gcg8-sequence', + 239 => 'chemical/x-genbank', + 240 => 'chemical/x-hin', + 241 => 'chemical/x-isostar', + 242 => 'chemical/x-jcamp-dx', + 243 => 'chemical/x-kinemage', + 244 => 'chemical/x-macmolecule', + 245 => 'chemical/x-macromodel-input', + 246 => 'chemical/x-mdl-molfile', + 247 => 'chemical/x-mdl-rdfile', + 248 => 'chemical/x-mdl-rxnfile', + 249 => 'chemical/x-mdl-sdfile', + 250 => 'chemical/x-mdl-tgf', + 251 => 'chemical/x-mif', + 252 => 'chemical/x-mmcif', + 253 => 'chemical/x-mol2', + 254 => 'chemical/x-molconn-Z', + 255 => 'chemical/x-mopac-graph', + 256 => 'chemical/x-mopac-input', + 257 => 'chemical/x-mopac-out', + 258 => 'chemical/x-mopac-vib', + 259 => 'chemical/x-ncbi-asn1', + 260 => 'chemical/x-ncbi-asn1-ascii', + 261 => 'chemical/x-ncbi-asn1-binary', + 262 => 'chemical/x-ncbi-asn1-spec', + 263 => 'chemical/x-pdb', + 264 => 'chemical/x-rosdal', + 265 => 'chemical/x-swissprot', + 266 => 'chemical/x-vamas-iso14976', + 267 => 'chemical/x-vmd', + 268 => 'chemical/x-xtel', + 269 => 'chemical/x-xyz', + 270 => 'image/gif', + 271 => 'image/ief', + 272 => 'image/jpeg', + 273 => 'image/pcx', + 274 => 'image/png', + 275 => 'image/svg+xml', + 276 => 'image/tiff', + 277 => 'image/vnd.djvu', + 278 => 'image/vnd.wap.wbmp', + 279 => 'image/x-canon-cr2', + 280 => 'image/x-canon-crw', + 281 => 'image/x-cmu-raster', + 282 => 'image/x-coreldraw', + 283 => 'image/x-coreldrawpattern', + 284 => 'image/x-coreldrawtemplate', + 285 => 'image/x-corelphotopaint', + 286 => 'image/x-epson-erf', + 287 => 'image/x-icon', + 288 => 'image/x-jg', + 289 => 'image/x-jng', + 290 => 'image/x-ms-bmp', + 291 => 'image/x-nikon-nef', + 292 => 'image/x-olympus-orf', + 293 => 'image/x-photoshop', + 294 => 'image/x-portable-anymap', + 295 => 'image/x-portable-bitmap', + 296 => 'image/x-portable-graymap', + 297 => 'image/x-portable-pixmap', + 298 => 'image/x-rgb', + 299 => 'image/x-xbitmap', + 300 => 'image/x-xpixmap', + 301 => 'image/x-xwindowdump', + 302 => 'message/rfc822', + 303 => 'model/iges', + 304 => 'model/mesh', + 305 => 'model/vrml', + 306 => 'model/x3d+vrml', + 307 => 'model/x3d+xml', + 308 => 'model/x3d+binary', + 309 => 'text/cache-manifest', + 310 => 'text/calendar', + 311 => 'text/css', + 312 => 'text/csv', + 313 => 'text/h323', + 314 => 'text/html', + 315 => 'text/iuls', + 316 => 'text/mathml', + 317 => 'text/plain', + 318 => 'text/richtext', + 319 => 'text/scriptlet', + 320 => 'text/texmacs', + 321 => 'text/tab-separated-values', + 322 => 'text/vnd.sun.j2me.app-descriptor', + 323 => 'text/vnd.wap.wml', + 324 => 'text/vnd.wap.wmlscript', + 325 => 'text/x-bibtex', + 326 => 'text/x-boo', + 327 => 'text/x-c++hdr', + 328 => 'text/x-c++src', + 329 => 'text/x-chdr', + 330 => 'text/x-component', + 331 => 'text/x-csh', + 332 => 'text/x-csrc', + 333 => 'text/x-dsrc', + 334 => 'text/x-diff', + 335 => 'text/x-haskell', + 336 => 'text/x-java', + 337 => 'text/x-literate-haskell', + 338 => 'text/x-moc', + 339 => 'text/x-pascal', + 340 => 'text/x-pcs-gcd', + 341 => 'text/x-perl', + 342 => 'text/x-python', + 343 => 'text/x-scala', + 344 => 'text/x-setext', + 345 => 'text/x-sh', + 346 => 'text/x-tcl', + 347 => 'text/x-tex', + 348 => 'text/x-vcalendar', + 349 => 'text/x-vcard', + 350 => 'video/3gpp', + 351 => 'video/annodex', + 352 => 'video/dl', + 353 => 'video/dv', + 354 => 'video/fli', + 355 => 'video/gl', + 356 => 'video/mpeg', + 357 => 'video/mp4', + 358 => 'video/quicktime', + 359 => 'video/ogg', + 360 => 'video/vnd.mpegurl', + 361 => 'video/x-flv', + 362 => 'video/x-la-asf', + 363 => 'video/x-mng', + 364 => 'video/x-ms-asf', + 365 => 'video/x-ms-wm', + 366 => 'video/x-ms-wmv', + 367 => 'video/x-ms-wmx', + 368 => 'video/x-ms-wvx', + 369 => 'video/x-msvideo', + 370 => 'video/x-sgi-movie', + 371 => 'video/x-matroska', + 372 => 'x-conference/x-cooltalk', + 373 => 'x-epoc/x-sisx-app', + 374 => 'x-world/x-vrml', + 375 => 'image/jpeg', + ); + + /* + * Extension map + */ + protected static $extensions = array( + 1 => 'ez', + 2 => 'anx', + 3 => 'atom', + 4 => 'atomcat', + 5 => 'atomsrv', + 6 => 'lin', + 7 => 'cap', + 8 => 'cu', + 9 => 'davmount', + 10 => 'tsp', + 11 => 'es', + 12 => 'spl', + 13 => 'hta', + 14 => 'jar', + 15 => 'ser', + 16 => 'class', + 17 => 'js', + 18 => 'm3g', + 19 => 'hqx', + 20 => 'cpt', + 21 => 'nb', + 22 => 'mdb', + 23 => 'doc', + 24 => 'mxf', + 25 => 'bin', + 26 => 'oda', + 27 => 'ogx', + 28 => 'pdf', + 29 => 'key', + 30 => 'pgp', + 31 => 'prf', + 32 => 'ps', + 33 => 'rar', + 34 => 'rdf', + 35 => 'rss', + 36 => 'rtf', + 37 => 'smi', + 38 => 'xhtml', + 39 => 'xml', + 40 => 'xspf', + 41 => 'zip', + 42 => 'apk', + 43 => 'cdy', + 44 => 'kml', + 45 => 'kmz', + 46 => 'xul', + 47 => 'xls', + 48 => 'cat', + 49 => 'stl', + 50 => 'ppt', + 51 => 'odc', + 52 => 'odb', + 53 => 'odf', + 54 => 'odg', + 55 => 'otg', + 56 => 'odi', + 57 => 'odp', + 58 => 'otp', + 59 => 'ods', + 60 => 'ots', + 61 => 'odt', + 62 => 'odm', + 63 => 'ott', + 64 => 'oth', + 65 => 'xlsx', + 66 => 'xltx', + 67 => 'pptx', + 68 => 'ppsx', + 69 => 'potx', + 70 => 'docx', + 71 => 'dotx', + 72 => 'cod', + 73 => 'mmf', + 74 => 'sdc', + 75 => 'sds', + 76 => 'sda', + 77 => 'sdd', + 78 => 'sdf', + 79 => 'sdw', + 80 => 'sgl', + 81 => 'sxc', + 82 => 'stc', + 83 => 'sxd', + 84 => 'std', + 85 => 'sxi', + 86 => 'sti', + 87 => 'sxm', + 88 => 'sxw', + 89 => 'sxg', + 90 => 'stw', + 91 => 'sis', + 92 => 'vsd', + 93 => 'wbxml', + 94 => 'wmlc', + 95 => 'wmlsc', + 96 => 'wpd', + 97 => 'wp5', + 98 => 'wk', + 99 => '7z', + 100 => 'abw', + 101 => 'dmg', + 102 => 'bcpio', + 103 => 'torrent', + 104 => 'cab', + 105 => 'cbr', + 106 => 'cbz', + 107 => 'cdf', + 108 => 'vcd', + 109 => 'pgn', + 110 => 'cpio', + 111 => 'csh', + 112 => 'deb', + 113 => 'dcr', + 114 => 'dms', + 115 => 'wad', + 116 => 'dvi', + 117 => 'rhtml', + 118 => 'pfa', + 119 => 'mm', + 120 => 'spl', + 121 => 'gnumeric', + 122 => 'sgf', + 123 => 'gcf', + 124 => 'gtar', + 125 => 'hdf', + 126 => 'phtml', + 127 => 'phps', + 128 => 'php3', + 129 => 'php3p', + 130 => 'php4', + 131 => 'php5', + 132 => 'ica', + 133 => 'info', + 134 => 'ins', + 135 => 'iii', + 136 => 'iso', + 137 => 'jam', + 138 => 'jnlp', + 139 => 'jmz', + 140 => 'chrt', + 141 => 'kil', + 142 => 'skp', + 143 => 'kpr', + 144 => 'ksp', + 145 => 'kwd', + 146 => 'latex', + 147 => 'lha', + 148 => 'lyx', + 149 => 'lzh', + 150 => 'lzx', + 151 => 'frm', + 152 => 'mif', + 153 => 'wmd', + 154 => 'wmz', + 155 => 'com', + 156 => 'msi', + 157 => 'nc', + 158 => 'pac', + 159 => 'nwc', + 160 => 'o', + 161 => 'oza', + 162 => 'p7r', + 163 => 'crl', + 164 => 'pyc', + 165 => 'qgs', + 166 => 'qtl', + 167 => 'rpm', + 168 => 'rb', + 169 => 'sh', + 170 => 'shar', + 171 => 'swf', + 172 => 'scr', + 173 => 'sit', + 174 => 'sv4cpio', + 175 => 'sv4crc', + 176 => 'tar', + 177 => 'tcl', + 178 => 'gf', + 179 => 'pk', + 180 => 'texinfo', + 181 => 't', + 182 => 'man', + 183 => 'me', + 184 => 'ms', + 185 => 'ustar', + 186 => 'src', + 187 => 'wz', + 188 => 'crt', + 189 => 'xcf', + 190 => 'fig', + 191 => 'xpi', + 192 => 'amr', + 193 => 'awb', + 194 => 'amr', + 195 => 'awb', + 196 => 'axa', + 197 => 'au', + 198 => 'flac', + 199 => 'mid', + 200 => 'mpga', + 201 => 'm3u', + 202 => 'oga', + 203 => 'sid', + 204 => 'aif', + 205 => 'gsm', + 206 => 'm3u', + 207 => 'wma', + 208 => 'wax', + 209 => 'ra', + 210 => 'ra', + 211 => 'pls', + 212 => 'sd2', + 213 => 'wav', + 214 => 'alc', + 215 => 'cac', + 216 => 'csf', + 217 => 'cbin', + 218 => 'cdx', + 219 => 'cer', + 220 => 'c3d', + 221 => 'chm', + 222 => 'cif', + 223 => 'cmdf', + 224 => 'cml', + 225 => 'cpa', + 226 => 'bsd', + 227 => 'csml', + 228 => 'ctx', + 229 => 'cxf', + 230 => 'smi', + 231 => 'emb', + 232 => 'spc', + 233 => 'inp', + 234 => 'fch', + 235 => 'cub', + 236 => 'gau', + 237 => 'gal', + 238 => 'gcg', + 239 => 'gen', + 240 => 'hin', + 241 => 'istr', + 242 => 'jdx', + 243 => 'kin', + 244 => 'mcm', + 245 => 'mmd', + 246 => 'mol', + 247 => 'rd', + 248 => 'rxn', + 249 => 'sd', + 250 => 'tgf', + 251 => 'mif', + 252 => 'mcif', + 253 => 'mol2', + 254 => 'b', + 255 => 'gpt', + 256 => 'mop', + 257 => 'moo', + 258 => 'mvb', + 259 => 'asn', + 260 => 'prt', + 261 => 'val', + 262 => 'asn', + 263 => 'pdb', + 264 => 'ros', + 265 => 'sw', + 266 => 'vms', + 267 => 'vmd', + 268 => 'xtel', + 269 => 'xyz', + 270 => 'gif', + 271 => 'ief', + 272 => 'jpeg', + 273 => 'pcx', + 274 => 'png', + 275 => 'svg', + 276 => 'tiff', + 277 => 'djvu', + 278 => 'wbmp', + 279 => 'cr2', + 280 => 'crw', + 281 => 'ras', + 282 => 'cdr', + 283 => 'pat', + 284 => 'cdt', + 285 => 'cpt', + 286 => 'erf', + 287 => 'ico', + 288 => 'art', + 289 => 'jng', + 290 => 'bmp', + 291 => 'nef', + 292 => 'orf', + 293 => 'psd', + 294 => 'pnm', + 295 => 'pbm', + 296 => 'pgm', + 297 => 'ppm', + 298 => 'rgb', + 299 => 'xbm', + 300 => 'xpm', + 301 => 'xwd', + 302 => 'eml', + 303 => 'igs', + 304 => 'msh', + 305 => 'wrl', + 306 => 'x3dv', + 307 => 'x3d', + 308 => 'x3db', + 309 => 'manifest', + 310 => 'ics', + 311 => 'css', + 312 => 'csv', + 313 => '323', + 314 => 'html', + 315 => 'uls', + 316 => 'mml', + 317 => 'asc', + 318 => 'rtx', + 319 => 'sct', + 320 => 'tm', + 321 => 'tsv', + 322 => 'jad', + 323 => 'wml', + 324 => 'wmls', + 325 => 'bib', + 326 => 'boo', + 327 => 'h', + 328 => 'c', + 329 => 'h', + 330 => 'htc', + 331 => 'csh', + 332 => 'c', + 333 => 'd', + 334 => 'diff', + 335 => 'hs', + 336 => 'java', + 337 => 'lhs', + 338 => 'moc', + 339 => 'p', + 340 => 'gcd', + 341 => 'pl', + 342 => 'py', + 343 => 'scala', + 344 => 'etx', + 345 => 'sh', + 346 => 'tcl', + 347 => 'tex', + 348 => 'vcs', + 349 => 'vcf', + 350 => '3gp', + 351 => 'axv', + 352 => 'dl', + 353 => 'dif', + 354 => 'fli', + 355 => 'gl', + 356 => 'mpeg', + 357 => 'mp4', + 358 => 'qt', + 359 => 'ogv', + 360 => 'mxu', + 361 => 'flv', + 362 => 'lsf', + 363 => 'mng', + 364 => 'asf', + 365 => 'wm', + 366 => 'wmv', + 367 => 'wmx', + 368 => 'wvx', + 369 => 'avi', + 370 => 'movie', + 371 => 'mpv', + 372 => 'ice', + 373 => 'sisx', + 374 => 'vrm', + 375 => 'jpg', + ); + + /** + * @return MimeType + */ + public static function wrap($id) + { + return new self($id); + } + + + /** + * Return MimeType object by mime-type string + * @param string $value + * @throws ObjectNotFoundException + * @return MimeType + */ + public static function getByMimeType($value) + { + $list = static::getNameList(); + foreach ( $list as $key => $mimeType ) + { + if( mb_strtolower($mimeType) === mb_strtolower($value) ) + return new self($key); + + } + + throw new ObjectNotFoundException( + 'Can not find similar mime type "'.$value.'" !' + ); + } + + /** + * Return MimeType object by extension without [dot] prefix + * @param string $value + * @throws ObjectNotFoundException + * @return MimeType + */ + public static function getByExtension($value) + { + $list = static::getExtensionList(); + foreach ( $list as $key => $extension ) + { + if( mb_strtolower( $extension ) === mb_strtolower($value) ) + return new self($key); + + } + + throw new ObjectNotFoundException( + 'Can not find similar extension "'.$value.'" !' + ); + } + + + /** + * Extension list + * @return array + */ + public static function getExtensionList() + { + return static::$extensions; + } + + /** + * Return extension without [dot] prefix. + * @throws ObjectNotFoundException + * @return string + */ + public function getExtension() + { + if( + isset( $this->extensions[$this->id] ) + ) + return $this->extensions[$this->id]; + + throw new ObjectNotFoundException( + 'Can not find "'.$this->id.'" in extensions map!' + ); + } + + /** + * Return Mime type string + * @return string + */ + public function getMimeType() + { + return $this->getName(); + } + } +?> \ No newline at end of file diff --git a/main/Criteria/Criteria.class.php b/main/Criteria/Criteria.class.php index 08e97142ab..faf16837fb 100644 --- a/main/Criteria/Criteria.class.php +++ b/main/Criteria/Criteria.class.php @@ -558,6 +558,10 @@ private function joinProperties( is_subclass_of( $property->getClassName(), 'Enumeration' + ) || + is_subclass_of( + $property->getClassName(), + 'Enum' ) ) { // field already added by makeSelectHead diff --git a/meta/builders/EnumClassBuilder.class.php b/meta/builders/EnumClassBuilder.class.php new file mode 100644 index 0000000000..a19d09ebec --- /dev/null +++ b/meta/builders/EnumClassBuilder.class.php @@ -0,0 +1,38 @@ +getType()) + $type = "{$type->getName()} "; + else + $type = null; + + $out .= <<getName()} extends Enum +{ + // implement me! + protected static \$names = array(); +} + +EOT; + + return $out.self::getHeel(); + } + } +?> \ No newline at end of file diff --git a/meta/classes/MetaClassProperty.class.php b/meta/classes/MetaClassProperty.class.php index dbac6fe23b..b8572961ca 100644 --- a/meta/classes/MetaClassProperty.class.php +++ b/meta/classes/MetaClassProperty.class.php @@ -342,6 +342,8 @@ public function toLightProperty(MetaClass $holder) if ($pattern instanceof EnumerationClassPattern) { $primitiveName = 'enumeration'; + } elseif ($pattern instanceof EnumClassPattern) { + $primitiveName = 'enum'; } elseif ( $pattern instanceof DictionaryClassPattern && ($identifier = $this->getType()->getClass()->getIdentifier()) diff --git a/meta/classes/MetaConfiguration.class.php b/meta/classes/MetaConfiguration.class.php index d00289490b..1d48d5e615 100644 --- a/meta/classes/MetaConfiguration.class.php +++ b/meta/classes/MetaConfiguration.class.php @@ -190,6 +190,9 @@ public function load($metafile, $generate = true) ) || ( $property->getType()->getClass()->getPattern() instanceof SpookedEnumerationPattern + ) || ( + $property->getType()->getClass()->getPattern() + instanceof SpookedEnumPattern ) ) && ( $property->getFetchStrategy() @@ -307,6 +310,8 @@ public function buildSchemaChanges() if ( $class->getTypeId() == MetaClassType::CLASS_ABSTRACT || $class->getPattern() instanceof EnumerationClassPattern + || $class->getPattern() instanceof EnumClassPattern + ) continue; @@ -452,6 +457,7 @@ public function checkIntegrity() !( $class->getPattern() instanceof SpookedClassPattern || $class->getPattern() instanceof SpookedEnumerationPattern + || $class->getPattern() instanceof SpookedEnumPattern || $class->getPattern() instanceof InternalClassPattern ) && ( class_exists($class->getName(), true) @@ -485,7 +491,10 @@ class_exists($class->getName(), true) ); // special handling for Enumeration instances - if ($class->getPattern() instanceof EnumerationClassPattern) { + if ( + $class->getPattern() instanceof EnumerationClassPattern + || $class->getPattern() instanceof EnumClassPattern + ) { $object = new $name(call_user_func(array($name, 'getAnyId'))); Assert::isTrue( @@ -495,10 +504,19 @@ class_exists($class->getName(), true) $out->info(', '); if ($this->checkEnumerationRefIntegrity) - $this->checkEnumerationReferentialIntegrity( - $object, - $class->getTableName() - ); + { + if($object instanceof Enumeration) + $this->checkEnumerationReferentialIntegrity( + $object, + $class->getTableName() + ); + elseif($object instanceof Enum) + $this->checkEnumReferentialIntegrity( + $object, + $class->getTableName() + ); + } + continue; } @@ -864,7 +882,8 @@ private function checkSanity(MetaClass $class) Assert::isTrue( ($class->getPattern() instanceof SpookedClassPattern - || $class->getPattern() instanceof SpookedEnumerationPattern), + || $class->getPattern() instanceof SpookedEnumerationPattern + || $class->getPattern() instanceof SpookedEnumPattern), 'spooked classes must use spooked patterns only: ' .$class->getName() ); @@ -1085,6 +1104,8 @@ private function processClasses(SimpleXMLElement $xml, $metafile, $generate) $class->getPattern() instanceof SpookedClassPattern ) || ( $class->getPattern() instanceof SpookedEnumerationPattern + ) || ( + $class->getPattern() instanceof SpookedEnumPattern ) ) { $class->setType( @@ -1362,5 +1383,56 @@ private function checkEnumerationReferentialIntegrity( return $this; } + + private function checkEnumReferentialIntegrity( + Enum $enum, $tableName + ) + { + $updateQueries = null; + + $db = DBPool::me()->getLink(); + + $class = get_class($enum); + + $ids = array(); + + $list = ClassUtils::callStaticMethod($class.'::'.'getObjectList'); + + foreach ($list as $enumerationObject) + $ids[$enumerationObject->getId()] = $enumerationObject->getName(); + + $rows = + $db->querySet( + OSQL::select()->from($tableName)-> + multiGet('id', 'name') + ); + + echo "\n"; + + foreach ($rows as $row) { + if (!isset($ids[$row['id']])) + echo "Class '{$class}', strange id: {$row['id']} found. \n"; + else { + if ($ids[$row['id']] != $row['name']) { + echo "Class '{$class}',id: {$row['id']} sync names. \n"; + + $updateQueries .= + OSQL::update($tableName)-> + set('name', $ids[$row['id']])-> + where(Expression::eq('id', $row['id']))-> + toDialectString($db->getDialect()) . ";\n"; + } + + unset($ids[$row['id']]); + } + } + + foreach ($ids as $id => $name) + echo "Class '{$class}', id: {$id} not present in database. \n"; + + echo $updateQueries; + + return $this; + } } ?> \ No newline at end of file diff --git a/meta/dtd/meta.dtd b/meta/dtd/meta.dtd index 35a0b81a57..0eaa67897f 100644 --- a/meta/dtd/meta.dtd +++ b/meta/dtd/meta.dtd @@ -47,8 +47,10 @@ |AbstractClass |StraightMapping |EnumerationClass + |EnumClass |SpookedClass |SpookedEnumeration + |SpookedEnum |ValueObject |InternalClass ) #REQUIRED diff --git a/meta/patterns/EnumClassPattern.class.php b/meta/patterns/EnumClassPattern.class.php new file mode 100644 index 0000000000..484e1551e2 --- /dev/null +++ b/meta/patterns/EnumClassPattern.class.php @@ -0,0 +1,46 @@ +getName().EXT_CLASS; + + if ( + MetaConfiguration::me()->isForcedGeneration() + || !file_exists($userFile) + ) + $this->dumpFile( + $userFile, + Format::indentize(EnumClassBuilder::build($class)) + ); + + return $this; + } + } +?> \ No newline at end of file diff --git a/meta/patterns/SpookedEnumPattern.class.php b/meta/patterns/SpookedEnumPattern.class.php new file mode 100644 index 0000000000..16c4b57a39 --- /dev/null +++ b/meta/patterns/SpookedEnumPattern.class.php @@ -0,0 +1,30 @@ + \ No newline at end of file diff --git a/meta/types/ObjectType.class.php b/meta/types/ObjectType.class.php index 525c77f407..fa90a9b6ec 100644 --- a/meta/types/ObjectType.class.php +++ b/meta/types/ObjectType.class.php @@ -100,8 +100,10 @@ public function {$methodName}() $className = $property->getType()->getClassName(); $isEnumeration = - $property->getType()->getClass()->getPattern() - instanceof EnumerationClassPattern; + ( + $property->getType()->getClass()->getPattern() instanceof EnumerationClassPattern + || $property->getType()->getClass()->getPattern() instanceof EnumClassPattern + ); $fetchObjectString = $isEnumeration ? "new {$className}(\$this->{$name}Id)" diff --git a/test/core/PrimitiveEnumTest.class.php b/test/core/PrimitiveEnumTest.class.php new file mode 100644 index 0000000000..51ff824f3a --- /dev/null +++ b/test/core/PrimitiveEnumTest.class.php @@ -0,0 +1,42 @@ + + add( + Primitive::enum('enum')->of('MimeType') + ); + + $form->import(array('enum' => '1')); + + $this->assertEquals($form->getValue('enum')->getId(), 1); + $this->assertSame($form->getValue('enum')->getId(), 1); + } + + public function testGetList() + { + $primitive = Primitive::enum('enum')->of('MimeType'); + $enum = MimeType::wrap(1); + + $this->assertEquals($primitive->getList(), MimeType::getObjectList()); + + $primitive->setDefault($enum); + $this->assertEquals($primitive->getList(), MimeType::getObjectList()); + + $primitive->import(array('enum' => MimeType::getAnyId())); + $this->assertEquals($primitive->getList(), MimeType::getObjectList()); + } + } +?> \ No newline at end of file diff --git a/test/main/EnumerationTest.class.php b/test/main/EnumerationTest.class.php index 1eccea51e5..f9441c60cb 100644 --- a/test/main/EnumerationTest.class.php +++ b/test/main/EnumerationTest.class.php @@ -14,6 +14,16 @@ public function testAnyId() ) ); + /* pass */ + } catch (MissingElementException $e) { + $this->fail($className); + } + } elseif(is_subclass_of($className, 'Enum')) { + try { + $enum = new $className( + ClassUtils::callStaticMethod($className.'::getAnyId') + ); + /* pass */ } catch (MissingElementException $e) { $this->fail($className); diff --git a/test/meta/config.meta.xml b/test/meta/config.meta.xml index e4341b477b..ba98dc7840 100644 --- a/test/meta/config.meta.xml +++ b/test/meta/config.meta.xml @@ -153,6 +153,13 @@ + + + + + + + @@ -160,6 +167,7 @@ + diff --git a/test/misc/DAOTest.class.php b/test/misc/DAOTest.class.php index 14f8dacb8a..c173e541d9 100644 --- a/test/misc/DAOTest.class.php +++ b/test/misc/DAOTest.class.php @@ -184,6 +184,7 @@ public function testGetByEmptyId() $this->assertNull($empty->getCity()); $this->assertNull($empty->getCityOptional()); $this->assertNull($empty->getEnum()); + $this->assertNull($empty->getStaticEnum()); $this->drop(); } @@ -943,6 +944,8 @@ private function lazyTest() setCityOptional($city)-> setEnum( new ImageType(ImageType::getAnyId()) + )->setStaticEnum( + new MimeType(MimeType::getAnyId()) ) ); From 270051aa9f5b6307eed48161d16b7ed895c6433f Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Tue, 27 Mar 2012 17:14:30 +0400 Subject: [PATCH 2/6] Code clean --- core/Base/Enum.class.php | 28 ++++++--- main/Base/MimeType.class.php | 36 +++++------ meta/classes/MetaConfiguration.class.php | 78 ++++++------------------ 3 files changed, 51 insertions(+), 91 deletions(-) diff --git a/core/Base/Enum.class.php b/core/Base/Enum.class.php index b6bf412439..be5492f430 100644 --- a/core/Base/Enum.class.php +++ b/core/Base/Enum.class.php @@ -33,7 +33,7 @@ public static function create($id) return new $className($id); } - final public function __construct($id) + public function __construct($id) { $this->setId($id); } @@ -51,9 +51,18 @@ public function unserialize($serialized) } //@} + /** + * Array of object + * @static + * @return array + */ public static function getList() { - return static::$names; + $list = array(); + foreach (array_keys(static::$names) as $id) + $list[] = static::create($id); + + return $list; } /** @@ -71,15 +80,16 @@ public function getId() return $this->id; } + + /** + * Alias for getList() + * @static + * @deprecated + * @return array + */ public static function getObjectList() { - $list = array(); - $names = static::$names; - - foreach (array_keys($names) as $id) - $list[] = static::create($id); - - return $list; + return static::getList(); } public function toString() diff --git a/main/Base/MimeType.class.php b/main/Base/MimeType.class.php index d6b4d1fd39..52cfb20448 100644 --- a/main/Base/MimeType.class.php +++ b/main/Base/MimeType.class.php @@ -790,43 +790,35 @@ public static function wrap($id) /** * Return MimeType object by mime-type string * @param string $value - * @throws ObjectNotFoundException + * @throws MissingElementException * @return MimeType */ public static function getByMimeType($value) { $list = static::getNameList(); - foreach ( $list as $key => $mimeType ) - { - if( mb_strtolower($mimeType) === mb_strtolower($value) ) - return new self($key); - } + $id = array_search(mb_strtolower($value), $list); + if ($id === false) + throw new MissingElementException('Can not find similar mime type "'.$value.'" !'); - throw new ObjectNotFoundException( - 'Can not find similar mime type "'.$value.'" !' - ); + return new self($id); } /** * Return MimeType object by extension without [dot] prefix * @param string $value - * @throws ObjectNotFoundException + * @throws MissingElementException * @return MimeType */ public static function getByExtension($value) { $list = static::getExtensionList(); - foreach ( $list as $key => $extension ) - { - if( mb_strtolower( $extension ) === mb_strtolower($value) ) - return new self($key); - } + $id = array_search(mb_strtolower($value), $list); + if ($id === false) + throw new MissingElementException('Can not find similar extension "'.$value.'" !'); - throw new ObjectNotFoundException( - 'Can not find similar extension "'.$value.'" !' - ); + return new self($id); } @@ -841,17 +833,17 @@ public static function getExtensionList() /** * Return extension without [dot] prefix. - * @throws ObjectNotFoundException + * @throws MissingElementException * @return string */ public function getExtension() { if( - isset( $this->extensions[$this->id] ) + isset( static::$extensions[$this->id] ) ) - return $this->extensions[$this->id]; + return static::$extensions[$this->id]; - throw new ObjectNotFoundException( + throw new MissingElementException( 'Can not find "'.$this->id.'" in extensions map!' ); } diff --git a/meta/classes/MetaConfiguration.class.php b/meta/classes/MetaConfiguration.class.php index 1d48d5e615..0da7c4c786 100644 --- a/meta/classes/MetaConfiguration.class.php +++ b/meta/classes/MetaConfiguration.class.php @@ -505,16 +505,14 @@ class_exists($class->getName(), true) if ($this->checkEnumerationRefIntegrity) { - if($object instanceof Enumeration) + if( + $object instanceof Enumeration + || $object instanceof Enum + ) $this->checkEnumerationReferentialIntegrity( $object, $class->getTableName() ); - elseif($object instanceof Enum) - $this->checkEnumReferentialIntegrity( - $object, - $class->getTableName() - ); } @@ -1334,9 +1332,17 @@ private function checkClassSanity( } private function checkEnumerationReferentialIntegrity( - Enumeration $enumeration, $tableName + $enumeration, $tableName ) { + Assert::isTrue( + ( + $enumeration instanceof Enumeration + || $enumeration instanceof Enum + ), + 'argument enumeation must be instacne of Enumeration or Enum! gived, "'.gettype($enumeration).'"' + ); + $updateQueries = null; $db = DBPool::me()->getLink(); @@ -1344,8 +1350,11 @@ private function checkEnumerationReferentialIntegrity( $class = get_class($enumeration); $ids = array(); - - $list = $enumeration->getObjectList(); + + if ($enumeration instanceof Enumeration) + $list = $enumeration->getList(); + elseif ($enumeration instanceof Enum) + $list = ClassUtils::callStaticMethod($class.'::getList'); foreach ($list as $enumerationObject) $ids[$enumerationObject->getId()] = $enumerationObject->getName(); @@ -1383,56 +1392,5 @@ private function checkEnumerationReferentialIntegrity( return $this; } - - private function checkEnumReferentialIntegrity( - Enum $enum, $tableName - ) - { - $updateQueries = null; - - $db = DBPool::me()->getLink(); - - $class = get_class($enum); - - $ids = array(); - - $list = ClassUtils::callStaticMethod($class.'::'.'getObjectList'); - - foreach ($list as $enumerationObject) - $ids[$enumerationObject->getId()] = $enumerationObject->getName(); - - $rows = - $db->querySet( - OSQL::select()->from($tableName)-> - multiGet('id', 'name') - ); - - echo "\n"; - - foreach ($rows as $row) { - if (!isset($ids[$row['id']])) - echo "Class '{$class}', strange id: {$row['id']} found. \n"; - else { - if ($ids[$row['id']] != $row['name']) { - echo "Class '{$class}',id: {$row['id']} sync names. \n"; - - $updateQueries .= - OSQL::update($tableName)-> - set('name', $ids[$row['id']])-> - where(Expression::eq('id', $row['id']))-> - toDialectString($db->getDialect()) . ";\n"; - } - - unset($ids[$row['id']]); - } - } - - foreach ($ids as $id => $name) - echo "Class '{$class}', id: {$id} not present in database. \n"; - - echo $updateQueries; - - return $this; - } } ?> \ No newline at end of file From 1bdf6a956d2126918487acae7e2302d68ddb1120 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 4 Apr 2012 13:07:09 +0400 Subject: [PATCH 3/6] Enum new politics & code clean --- core/Base/Enum.class.php | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/core/Base/Enum.class.php b/core/Base/Enum.class.php index be5492f430..0e4501c722 100644 --- a/core/Base/Enum.class.php +++ b/core/Base/Enum.class.php @@ -35,21 +35,44 @@ public static function create($id) public function __construct($id) { - $this->setId($id); + $this->setInternalId($id); } - /// prevent's serialization of names' array - //@{ + /** + * @param $id + * @return Enum + * @throws MissingElementException + */ + protected function setInternalId($id) + { + $names = static::$names; + + if (isset($names[$id])) { + $this->id = $id; + $this->name = $names[$id]; + } else + throw new MissingElementException( + 'knows nothing about such id == '.$id + ); + + return $this; + } + + /** + * @return string + */ public function serialize() { return (string) $this->id; } + /** + * @param $serialized + */ public function unserialize($serialized) { - $this->setId($serialized); + $this->setInternalId($serialized); } - //@} /** * Array of object @@ -68,13 +91,16 @@ public static function getList() /** * must return any existent ID * 1 should be ok for most enumerations + * @return integer **/ public static function getAnyId() { return 1; } - /// parent's getId() is too complex in our case + /** + * @return null|integer + */ public function getId() { return $this->id; @@ -92,11 +118,19 @@ public static function getObjectList() return static::getList(); } + /** + * @return string + */ public function toString() { return $this->name; } + /** + * Plain list + * @static + * @return array + */ public static function getNameList() { return static::$names; @@ -107,17 +141,7 @@ public static function getNameList() **/ public function setId($id) { - $names = static::$names; - - if (isset($names[$id])) { - $this->id = $id; - $this->name = $names[$id]; - } else - throw new MissingElementException( - 'knows nothing about such id == '.$id - ); - - return $this; + throw new UnsupportedMethodException('You can not change id here, because it is politics for Enum!'); } } ?> \ No newline at end of file From 98d33b184c80400a345c900208ac56de2633cca4 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 4 Apr 2012 13:08:08 +0400 Subject: [PATCH 4/6] clean --- core/Base/Enum.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Base/Enum.class.php b/core/Base/Enum.class.php index 0e4501c722..d145d33baa 100644 --- a/core/Base/Enum.class.php +++ b/core/Base/Enum.class.php @@ -12,7 +12,7 @@ /** * Parent of all enumeration classes. * - * @see AccessMode for example + * @see MimeType for example * * @ingroup Base * @ingroup Module From 132d7f387d22151be103dcb09221069c258d9abf Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 25 Apr 2012 18:03:26 +0400 Subject: [PATCH 5/6] clean --- core/Base/Enum.class.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/core/Base/Enum.class.php b/core/Base/Enum.class.php index d145d33baa..9c3df626ff 100644 --- a/core/Base/Enum.class.php +++ b/core/Base/Enum.class.php @@ -29,8 +29,7 @@ abstract class Enum extends NamedObject */ public static function create($id) { - $className = get_called_class(); - return new $className($id); + return new static($id); } public function __construct($id) @@ -45,11 +44,9 @@ public function __construct($id) */ protected function setInternalId($id) { - $names = static::$names; - - if (isset($names[$id])) { + if (isset(static::$names[$id])) { $this->id = $id; - $this->name = $names[$id]; + $this->name = static::$names[$id]; } else throw new MissingElementException( 'knows nothing about such id == '.$id From 2b25872e24c2e28d5b3794814153fb9f6c2ff65d Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 25 Apr 2012 23:30:22 +0400 Subject: [PATCH 6/6] refactor & add new features --- core/Form/Primitives/PrimitiveEnum.class.php | 65 +++++++++++++++++--- test/core/PrimitiveEnumTest.class.php | 27 ++++++-- 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/core/Form/Primitives/PrimitiveEnum.class.php b/core/Form/Primitives/PrimitiveEnum.class.php index 33deface4e..3fdecb9118 100644 --- a/core/Form/Primitives/PrimitiveEnum.class.php +++ b/core/Form/Primitives/PrimitiveEnum.class.php @@ -12,14 +12,14 @@ /** * @ingroup Primitives **/ - class PrimitiveEnum extends IdentifiablePrimitive + class PrimitiveEnum extends IdentifiablePrimitive implements ListedPrimitive { public function getList() { if ($this->value) - return ClassUtils::callStaticMethod(get_class($this->value).'::getObjectList'); + return ClassUtils::callStaticMethod(get_class($this->value).'::getList'); elseif ($this->default) - return ClassUtils::callStaticMethod(get_class($this->default).'::getObjectList'); + return ClassUtils::callStaticMethod(get_class($this->default).'::getList'); else { $object = new $this->className( ClassUtils::callStaticMethod($this->className.'::getAnyId') @@ -60,16 +60,11 @@ public function importValue(/* Identifiable */ $value) public function import($scope) { - if (!$this->className) - throw new WrongStateException( - "no class defined for PrimitiveEnum '{$this->name}'" - ); - $result = parent::import($scope); if ($result === true) { try { - $this->value = new $this->className($this->value); + $this->value = $this->makeEnumById($this->value); } catch (MissingElementException $e) { $this->value = null; @@ -81,5 +76,57 @@ public function import($scope) return $result; } + + /** + * @param $list + * @throws UnsupportedMethodException + */ + public function setList($list) + { + throw new UnsupportedMethodException('you cannot set list here, it is impossible, because list getted from enum classes'); + } + + /** + * @return null|string + */ + public function getChoiceValue() + { + if( + ($value = $this->getValue() ) && + $value instanceof Enum + ) + return $value->getName(); + + return null; + } + + + /** + * @return Enum|mixed|null + */ + public function getActualChoiceValue() + { + if( + !$this->getChoiceValue() && + $this->getDefault() + ) + return $this->getDefault()->getName(); + + return null; + } + + /** + * @param $id + * @return Enum|mixed + */ + protected function makeEnumById($id) + { + if (!$this->className) + throw new WrongStateException( + "no class defined for PrimitiveEnum '{$this->name}'" + ); + + return new $this->className($id); + } } ?> \ No newline at end of file diff --git a/test/core/PrimitiveEnumTest.class.php b/test/core/PrimitiveEnumTest.class.php index 51ff824f3a..585e5be89c 100644 --- a/test/core/PrimitiveEnumTest.class.php +++ b/test/core/PrimitiveEnumTest.class.php @@ -13,16 +13,35 @@ final class PrimitiveEnumTest extends TestCase { public function testIntegerValues() { + $defaultId = 2; + $importId = 1; + + $default = MimeType::wrap($defaultId); + $imported = MimeType::wrap($importId); + $form = Form::create()-> add( - Primitive::enum('enum')->of('MimeType') + Primitive::enum('enum') + ->of('MimeType') + ->setDefault($default) ); - $form->import(array('enum' => '1')); + $form->import(array('enum' => $importId)); - $this->assertEquals($form->getValue('enum')->getId(), 1); - $this->assertSame($form->getValue('enum')->getId(), 1); + $this->assertEquals($form->getValue('enum')->getId(), $importId); + $this->assertSame($form->getValue('enum')->getId(), $importId); + + $this->assertEquals($form->getChoiceValue('enum'), $imported->getName()); + $this->assertSame($form->getChoiceValue('enum'), $imported->getName()); + + $form->clean(); + + $this->assertNull($form->getValue('enum')); + $this->assertNull($form->getChoiceValue('enum')); + $this->assertEquals($form->getActualValue('enum')->getId(), $defaultId); + $this->assertEquals($form->getActualChoiceValue('enum'), $default->getName()); + } public function testGetList()