Maslosoft Zamm API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/zamm
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/zamm/
*/
namespace Maslosoft\Zamm;
use DirectoryIterator;
use Maslosoft\Addendum\Utilities\ClassChecker;
use ReflectionClass;
use ReflectionProperty;
/**
* Iterator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Iterator
{
/**
* Iterate over classes in same folder.
* @param string $class
* @return string[]
*/
public static function ns($class)
{
$info = new ReflectionClass($class);
$path = dirname($info->getFileName());
$ns = $info->getNamespaceName();
$classNames = [];
foreach (new DirectoryIterator($path) as $fileInfo)
{
$name = $fileInfo->getFilename();
// Only files
if (!$fileInfo->isFile())
{
continue;
}
// Only php
if (!preg_match('~\.php$~', $name))
{
continue;
}
$fqn = sprintf('%s\%s', $ns, basename($name, '.php'));
if (!ClassChecker::exists($fqn))
{
continue;
}
$classNames[] = $fqn;
}
sort($classNames);
return $classNames;
}
/**
* Iterate over *public* class methods
* @param string $class
* @return string[]
*/
public static function methods($class)
{
$info = new ReflectionClass($class);
$methods = [];
foreach ($info->getMethods(ReflectionProperty::IS_PUBLIC) as $method)
{
$methods[] = $method->name;
}
/**
* Sort methods that:
* * Getters and setters are first
* * Getters and setters are side-by-side to each other counterpart
*/
$sort = function($a, $b)
{
// Whether is getter or setter
$isAGS = false;
$isBGS = false;
// Whether is getter
$isAG = false;
$isBG = false;
// Whether is setter
$isAS = false;
$isBS = false;
// Check if it's getter or setter
if (preg_match('~^[gs]et[A-Z0-9]~', $a))
{
if(preg_match('~^get[A-Z0-9]~', $a))
{
$isAG = true;
}
else
{
$isAS = true;
}
$a = substr($a, 3);
$isAGS = true;
}
// Check if it's getter or setter
if (preg_match('~^[gs]et[A-Z0-9]~', $b))
{
if(preg_match('~^get[A-Z0-9]~', $b))
{
$isBG = true;
}
else
{
$isBS = true;
}
$b = substr($b, 3);
$isBGS = true;
}
if($isAGS && !$isBGS)
{
return -1;
}
if(!$isAGS && $isBGS)
{
return 1;
}
if ($a == $b)
{
if($isAGS && $isBGS)
{
if($isAG && $isBS)
{
return -1;
}
if($isBG && $isAS)
{
return 1;
}
}
return 0;
}
return ($a < $b) ? -1 : 1;
};
usort($methods, $sort);
return $methods;
}
/**
* Iterate over *public* class properties
* @param string $class
* @return string[]
*/
public static function properties($class)
{
$info = new ReflectionClass($class);
$properties = [];
foreach ($info->getProperties(ReflectionProperty::IS_PUBLIC) as $property)
{
$properties[] = $property->name;
}
sort($properties);
return $properties;
}
}
API documentation generated by ApiGen