Download .zip |
Info | View files (27) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2010-09-08 (8 years ago) | Not enough user ratings | Total: 429 | All time: 6,162 This week: 540 |
Version | License | PHP version | Categories | |||
comparator-tools 0.9 | BSD License | 5.1 | PHP 5, Data types, Language |
Description | Author | |||
This package can be used to Sort and compare objects of many types. Innovation Award
|
+-----------------------------------------------------------------------------+ | ComparatorTools | +-----------------------------------------------------------------------------+ - Synopsis - Requirements - Files - Simple Usage - Procedural Interface - Error Handling Synopsis -------- This package provides sorting of objects that have a Comparable interface and other useful functions with comparison of objects. With these tools the "Comparable" and "Comparator" interfaces can be used like known from Java. Requirements ------------ The package requires PHP 5.1 or later. To use the package, just include comparatortools.lib.php. If you do not want the additional procedural interface, remove the inclusion of functions.inc. Files ----- readme.txt - the file you are reading right now license.txt - BSD license comparatortools.lib.php - library loader, include this file to use the package ComparatorTool.php - class file: abstract tool class ObjectSorter.php - class file: tool class for sorting ObjectArrayModifier.php - class file: tool class for other array modifications (diff, intersect, unique) Comparator.php- class file: comparator interface Comparable.php - class file: comparable interface ComparatorException.php - class file: comparator exception functions.inc - functions file: procedural interface (osort, orsort etc.) Iterators/ObjectSortingIterator.php - class file: ObjectSorter functionality for iterators Comparators/ComparableComparator.php - class file: comparator for Comparable interface Comparators/ObjectComparator.php - class file: comparator for object identity Comparators/ReverseComparator.php - class file: decorator class to revert comparator outcome Comparators/SplFileInfoComparator.php - class file: abstract comparator for SplFileInfo objects Comparators/SplFileInfoComparatorATime.php - class file: compare SplFileInfo objects by access time Comparators/SplFileInfoComparatorCTime.php - class file: compare SplFileInfo objects by creation time Comparators/SplFileInfoComparatorMTime.php - class file: compare SplFileInfo objects by modification time Comparators/SplFileInfoComparatorName.php - class file: compare SplFileInfo objects by file name Comparators/SplFileInfoComparatorSize.php - class file: compare SplFileInfo objects by file size Comparators/SplFileInfoComparatorType.php - class file: compare SplFileInfo objects by file type example/ExampleData.php - example: an implementation of the Comparable interface, used by the examples example/ExampleComparator.php - example: an implementation of the Comparator interface, used by the examples example/sort.php - example: some sorting example/sort_procedural.php - example: same example but with the procedural interface example/advanced.php - example: more examples Simple Usage ------------ To give your classes the comparable functionality, just implement the Comparable interface: class Foo implements Comparable { public function compareTo($object) { } } The compareTo method will be called with another instance of Foo as parameter and must return a negative value if ($this < $object) applies and a positive value if ($this > $object) applies, 0 otherwise (the objects are considered equal) To use the tools - i.e. sort objects - instantiate a tool class (currently available: ObjectSorter, ObjectArrayModifier): $tool = new ObjectSorter; $tool->sort($array_of_foo_objects); For detailed description of the functions see phpDoc documentation inside the tool class files. It is also possible to implement separate comparator classes: class FooComparator implements Comparator { public function compare($object1, $object2) { } } The compare method behaves just like the compareTo method but both objects are passed as parameters. To use a comparator, call the setComparator() method of the tool or pass the comparator in the constructor: $fooTool = new ObjectSorter(new FooComparator); $fooTool->sort($array_of_foo_objects); Procedural Interface -------------------- There is also a procedural interface with functions much like sort, array_diff and so on from the PHP core. This way the Comparable interface can be used in a convenient, familiar way. osort($array_of_foo_objects); Currently available: osort orsort oarsort oasort array_omultisort array_ounique array_odiff array_ointersect For detailed description of the functions see phpDoc documentation inside functions.inc Error Handling -------------- You are encouraged to throw a ComparatorException if your compare() or compareTo() methods fail, i.e. in case of wrong parameters. The tools will catch and handle them. By default most functions return false and trigger a E_USER_WARNING if such an exception was thrown. But it is also possible to change this behaviour: ComparatorTool::setThrowExceptions(true); so that the exceptions are carried on to your application. |
Files |
File | Role | Description | ||
---|---|---|---|---|
Iterators (1 file) | ||||
Comparators (10 files) | ||||
example (6 files) | ||||
license.txt | Lic. | BSD License | ||
readme.txt | Doc. | Documentation | ||
comparatortools.lib.php | Aux. | library loader, include this file to use the package | ||
ComparatorTool.php | Class | abstract tool class | ||
ObjectArrayModifier.php | Class | tool class for other array modifications (diff, intersect, unique) | ||
ObjectSorter.php | Class | tool class for sorting | ||
Comparable.php | Class | comparable interface | ||
Comparator.php | Class | comparator interface | ||
ComparatorException.php | Class | comparator exception | ||
functions.inc | Aux. | procedural interface (osort, orsort etc.) |
Files | / | Iterators |
File | Role | Description |
---|---|---|
ObjectSortingIterator.php | Class | ObjectSorter functionality for iterators |
Files | / | Comparators |
File | Role | Description |
---|---|---|
ComparableComparator.php | Class | comparator for Comparable interface |
ObjectComparator.php | Class | comparator for object identity |
ReverseComparator.php | Class | decorator class to revert comparator outcome |
SplFileInfoComparator.php | Class | abstract comparator for SplFileInfo objects |
SplFileInfoComparatorATime.php | Class | compare SplFileInfo objects by access time |
SplFileInfoComparatorCTime.php | Class | compare SplFileInfo objects by creation time |
SplFileInfoComparatorMTime.php | Class | compare SplFileInfo objects by modification time |
SplFileInfoComparatorName.php | Class | compare SplFileInfo objects by file name |
SplFileInfoComparatorSize.php | Class | compare SplFileInfo objects by file size |
SplFileInfoComparatorType.php | Class | compare SplFileInfo objects by file type |
Files | / | example |
File | Role | Description |
---|---|---|
ExampleComparator.php | Example | an implementation of the Comparator interface, used by the examples |
ExampleData.php | Example | an implementation of the Comparable interface, used by the examples |
randomdata.inc | Example | generation of a randomized object array |
sort.php | Example | some sorting |
sort_procedural.php | Example | same example but with the procedural interface |
advanced.php | Example | more examples |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
0% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Related pages |
github.com Latest version at github |