|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/php-filter for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/php-filter/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/php-filter/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\Filter; |
| 12 | + |
| 13 | +use Laminas\Filter; |
| 14 | +use Laminas\Filter\FilterChain; |
| 15 | +use Laminas\Filter\Word\SeparatorToSeparator; |
| 16 | +use OpenCodeModeling\Filter\Filter\LowerCaseFirst; |
| 17 | +use OpenCodeModeling\Filter\Filter\NormalizeLabel; |
| 18 | +use OpenCodeModeling\Filter\Filter\UpperCaseFirst; |
| 19 | + |
| 20 | +final class FilterFactory |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Returns a normalize filter for labels, names ... |
| 24 | + * |
| 25 | + * @param callable ...$callables Attached to the end of the filter chain |
| 26 | + * @return callable |
| 27 | + */ |
| 28 | + public static function normalizeFilter(callable ...$callables): callable |
| 29 | + { |
| 30 | + $filter = new Filter\FilterChain(); |
| 31 | + $filter->attach(new NormalizeLabel()); |
| 32 | + $filter->attach(new Filter\Word\SeparatorToSeparator(' ', '-')); |
| 33 | + $filter->attach(new Filter\Word\UnderscoreToCamelCase()); |
| 34 | + $filter->attach(new Filter\Word\DashToCamelCase()); |
| 35 | + |
| 36 | + foreach ($callables as $callable) { |
| 37 | + $filter->attach($callable); |
| 38 | + } |
| 39 | + |
| 40 | + return $filter; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns a filter for valid class names e.g. AddBuilding |
| 45 | + * |
| 46 | + * @return callable |
| 47 | + */ |
| 48 | + public static function classNameFilter(): callable |
| 49 | + { |
| 50 | + return new UpperCaseFirst(self::normalizeFilter()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a filter for valid constant names e.g. ADD_BUILDING |
| 55 | + * |
| 56 | + * @return callable |
| 57 | + */ |
| 58 | + public static function constantNameFilter(): callable |
| 59 | + { |
| 60 | + return self::normalizeFilter( |
| 61 | + new Filter\Word\CamelCaseToUnderscore(), |
| 62 | + new Filter\StringToUpper() |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns a filter for constant values e.g. add_building |
| 68 | + * |
| 69 | + * @return callable |
| 70 | + */ |
| 71 | + public static function constantValueFilter(): callable |
| 72 | + { |
| 73 | + return self::normalizeFilter( |
| 74 | + new Filter\Word\CamelCaseToUnderscore(), |
| 75 | + new Filter\StringToLower() |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a filter for valid property names e.g. addBuilding |
| 81 | + * |
| 82 | + * @return callable |
| 83 | + */ |
| 84 | + public static function propertyNameFilter(): callable |
| 85 | + { |
| 86 | + return new LowerCaseFirst(self::normalizeFilter()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns a filter for valid property names e.g. addBuilding |
| 91 | + * |
| 92 | + * @return callable |
| 93 | + */ |
| 94 | + public static function methodNameFilter(): callable |
| 95 | + { |
| 96 | + return new LowerCaseFirst(self::normalizeFilter()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns a filter for converting a directory to a namespace |
| 101 | + * |
| 102 | + * @return callable |
| 103 | + */ |
| 104 | + public static function directoryToNamespaceFilter(): callable |
| 105 | + { |
| 106 | + $filter = new Filter\FilterChain(); |
| 107 | + $filter->attach(new Filter\Word\SeparatorToSeparator(DIRECTORY_SEPARATOR, '|')); |
| 108 | + $filter->attach(new Filter\Word\SeparatorToSeparator('|', '\\\\')); |
| 109 | + |
| 110 | + return $filter; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns a filter for converting a namespace to a directory |
| 115 | + * |
| 116 | + * @return callable |
| 117 | + */ |
| 118 | + public static function namespaceToDirectoryFilter(): callable |
| 119 | + { |
| 120 | + $filter = new FilterChain(); |
| 121 | + $filter->attach(new SeparatorToSeparator('\\', '|')); |
| 122 | + $filter->attach(new SeparatorToSeparator('|', DIRECTORY_SEPARATOR)); |
| 123 | + |
| 124 | + return $filter; |
| 125 | + } |
| 126 | +} |
0 commit comments