1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Core; |
4: | |
5: | use Salient\Contract\Core\Entity\Extensible; |
6: | use Salient\Contract\Core\Entity\Normalisable; |
7: | use Salient\Contract\Core\Entity\Readable; |
8: | use Salient\Contract\Core\Entity\Relatable; |
9: | use Salient\Contract\Core\Entity\Temporal; |
10: | use Salient\Contract\Core\Entity\Treeable; |
11: | use Salient\Contract\Core\Entity\Writable; |
12: | use Salient\Contract\Core\Provider\Providable; |
13: | use Salient\Contract\Core\NormaliserFlag; |
14: | use Salient\Utility\Arr; |
15: | use Salient\Utility\Reflect; |
16: | use Salient\Utility\Regex; |
17: | use Salient\Utility\Str; |
18: | use Closure; |
19: | use DateTimeInterface; |
20: | use ReflectionClass; |
21: | use ReflectionMethod; |
22: | use ReflectionNamedType; |
23: | use ReflectionProperty; |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | class IntrospectionClass |
31: | { |
32: | public const ACTION_GET = 'get'; |
33: | public const ACTION_ISSET = 'isset'; |
34: | public const ACTION_SET = 'set'; |
35: | public const ACTION_UNSET = 'unset'; |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | public $Class; |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | public $IsReadable; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public $IsWritable; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | public $IsExtensible; |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | public $IsProvidable; |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | public $IsRelatable; |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | public $IsTreeable; |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | public $HasDates; |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public $Properties = []; |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | public $PublicProperties = []; |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public $ReadableProperties = []; |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | public $WritableProperties = []; |
138: | |
139: | |
140: | |
141: | |
142: | public string $DynamicPropertiesProperty; |
143: | |
144: | |
145: | |
146: | |
147: | public string $DynamicPropertyNamesProperty; |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | public $Actions = []; |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | public $Parameters = []; |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | public $RequiredParameters = []; |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | public $NotNullableParameters = []; |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | public $ServiceParameters = []; |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | public $PassByRefParameters = []; |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | public $DateParameters = []; |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | public $DefaultArguments = []; |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | public $RequiredArguments = 0; |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | public $ParameterIndex = []; |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | public $SerializableProperties = []; |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | public $NormalisedKeys = []; |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | public $ParentProperty; |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | |
256: | |
257: | public $ChildrenProperty; |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | |
265: | public $OneToOneRelationships = []; |
266: | |
267: | |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | public $OneToManyRelationships = []; |
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | public $DateKeys = []; |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | public ?Closure $Normaliser = null; |
288: | |
289: | |
290: | |
291: | |
292: | |
293: | |
294: | public Closure $GentleNormaliser; |
295: | |
296: | |
297: | |
298: | |
299: | |
300: | |
301: | public Closure $CarefulNormaliser; |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | public $CreateFromSignatureClosures = []; |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | public $CreateProviderlessFromSignatureClosures = []; |
316: | |
317: | |
318: | |
319: | |
320: | |
321: | |
322: | public $CreateProvidableFromSignatureClosures = []; |
323: | |
324: | |
325: | |
326: | |
327: | |
328: | |
329: | public $CreateProviderlessFromClosures = []; |
330: | |
331: | |
332: | |
333: | |
334: | |
335: | |
336: | public $CreateProvidableFromClosures = []; |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | public $PropertyActionClosures = []; |
344: | |
345: | |
346: | public $GetNameClosure; |
347: | |
348: | |
349: | |
350: | |
351: | |
352: | |
353: | public $SerializeClosures = []; |
354: | |
355: | |
356: | protected $Reflector; |
357: | |
358: | |
359: | |
360: | |
361: | public function __construct(string $class) |
362: | { |
363: | $class = new ReflectionClass($class); |
364: | $className = $class->getName(); |
365: | $this->Reflector = $class; |
366: | $this->Class = $className; |
367: | $this->IsReadable = $class->implementsInterface(Readable::class); |
368: | $this->IsWritable = $class->implementsInterface(Writable::class); |
369: | $this->IsExtensible = $class->implementsInterface(Extensible::class); |
370: | $this->IsProvidable = $class->implementsInterface(Providable::class); |
371: | $this->IsTreeable = $class->implementsInterface(Treeable::class); |
372: | $this->IsRelatable = $this->IsTreeable || $class->implementsInterface(Relatable::class); |
373: | $this->HasDates = $class->implementsInterface(Temporal::class); |
374: | |
375: | if ($class->implementsInterface(Normalisable::class)) { |
376: | |
377: | $this->Normaliser = static fn(string $name, bool $greedy = true, string ...$hints) => |
378: | $className::normaliseProperty($name, $greedy, ...$hints); |
379: | $this->GentleNormaliser = fn(string $name): string => ($this->Normaliser)($name, false); |
380: | $this->CarefulNormaliser = fn(string $name): string => ($this->Normaliser)($name, true, ...$this->NormalisedKeys); |
381: | } |
382: | |
383: | $propertyFilter = ReflectionProperty::IS_PUBLIC; |
384: | $methodFilter = 0; |
385: | $reserved = []; |
386: | |
387: | |
388: | |
389: | if ($this->IsReadable || $this->IsWritable) { |
390: | $propertyFilter |= ReflectionProperty::IS_PROTECTED; |
391: | $methodFilter |= ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED; |
392: | } |
393: | |
394: | if ($this->IsExtensible) { |
395: | |
396: | $reserved[] = $this->DynamicPropertiesProperty = $className::getDynamicPropertiesProperty(); |
397: | $reserved[] = $this->DynamicPropertyNamesProperty = $className::getDynamicPropertyNamesProperty(); |
398: | } |
399: | |
400: | $parent = $class; |
401: | do { |
402: | $parents[] = $parent->getName(); |
403: | } while ($parent = $parent->getParentClass()); |
404: | $parents = array_flip($parents); |
405: | |
406: | |
407: | $properties = array_filter( |
408: | $class->getProperties($propertyFilter), |
409: | fn(ReflectionProperty $prop) => !$prop->isStatic() |
410: | ); |
411: | |
412: | uksort( |
413: | $properties, |
414: | function (int $a, int $b) use ($parents, $properties) { |
415: | $depthA = $parents[$properties[$a]->getDeclaringClass()->getName()]; |
416: | $depthB = $parents[$properties[$b]->getDeclaringClass()->getName()]; |
417: | |
418: | return $depthB <=> $depthA ?: $a <=> $b; |
419: | } |
420: | ); |
421: | $names = Reflect::getNames($properties); |
422: | $this->Properties = array_diff_key( |
423: | Arr::combine( |
424: | $this->maybeNormalise($names, NormaliserFlag::LAZY), |
425: | $names |
426: | ), |
427: | array_flip($this->maybeNormalise($reserved, NormaliserFlag::LAZY)), |
428: | ); |
429: | $this->PublicProperties = |
430: | $propertyFilter & ReflectionProperty::IS_PROTECTED |
431: | ? array_intersect( |
432: | $this->Properties, |
433: | Reflect::getNames(array_filter( |
434: | $properties, |
435: | fn(ReflectionProperty $prop) => $prop->isPublic() |
436: | )) |
437: | ) |
438: | : $this->Properties; |
439: | |
440: | if ($this->IsReadable) { |
441: | |
442: | $readable = $className::getReadableProperties(); |
443: | $readable = array_merge( |
444: | ['*'] === $readable |
445: | ? $this->Properties |
446: | : $readable, |
447: | $this->PublicProperties |
448: | ); |
449: | $this->ReadableProperties = array_intersect($this->Properties, $readable); |
450: | } |
451: | |
452: | if ($this->IsWritable) { |
453: | |
454: | $writable = $className::getWritableProperties(); |
455: | $writable = array_merge( |
456: | ['*'] === $writable |
457: | ? $this->Properties |
458: | : $writable, |
459: | $this->PublicProperties |
460: | ); |
461: | $this->WritableProperties = array_intersect($this->Properties, $writable); |
462: | } |
463: | |
464: | |
465: | if ($methodFilter) { |
466: | |
467: | $methods = array_filter( |
468: | $class->getMethods($methodFilter), |
469: | fn(ReflectionMethod $method) => !$method->isStatic() |
470: | ); |
471: | $regex = implode('|', [ |
472: | ...($this->IsReadable ? [self::ACTION_GET, self::ACTION_ISSET] : []), |
473: | ...($this->IsWritable ? [self::ACTION_SET, self::ACTION_UNSET] : []), |
474: | ]); |
475: | $regex = "/^_(?<action>{$regex})(?<property>.+)\$/i"; |
476: | foreach ($methods as $method) { |
477: | if (!Regex::match($regex, $name = $method->getName(), $matches)) { |
478: | continue; |
479: | } |
480: | $action = Str::lower($matches['action']); |
481: | $property = $this->maybeNormalise($matches['property'], NormaliserFlag::LAZY); |
482: | $this->Actions[$action][$property] = $name; |
483: | } |
484: | } |
485: | |
486: | |
487: | |
488: | |
489: | |
490: | |
491: | |
492: | if (($constructor = $class->getConstructor()) && $constructor->isPublic()) { |
493: | $lastRequired = -1; |
494: | $index = -1; |
495: | foreach ($constructor->getParameters() as $param) { |
496: | $type = $param->getType(); |
497: | $type = $type instanceof ReflectionNamedType && !$type->isBuiltin() |
498: | ? $type->getName() |
499: | : null; |
500: | $normalised = $this->maybeNormalise($name = $param->getName(), NormaliserFlag::LAZY); |
501: | $defaultValue = null; |
502: | $isOptional = false; |
503: | if ($param->isOptional()) { |
504: | if ($param->isDefaultValueAvailable()) { |
505: | $defaultValue = $param->getDefaultValue(); |
506: | } |
507: | $isOptional = true; |
508: | if (!$param->allowsNull()) { |
509: | $this->NotNullableParameters[$normalised] = $name; |
510: | } |
511: | } elseif (!$param->allowsNull()) { |
512: | $this->RequiredParameters[$normalised] = $name; |
513: | if ($type) { |
514: | $this->ServiceParameters[$normalised] = $type; |
515: | } |
516: | } |
517: | $index++; |
518: | if (!$isOptional) { |
519: | $lastRequired = $index; |
520: | } |
521: | if ($param->isPassedByReference()) { |
522: | $this->PassByRefParameters[$normalised] = $name; |
523: | } |
524: | if ($this->HasDates && is_a($type, DateTimeInterface::class, true)) { |
525: | $this->DateParameters[$normalised] = $name; |
526: | } |
527: | $this->Parameters[$normalised] = $name; |
528: | $this->DefaultArguments[] = $defaultValue; |
529: | } |
530: | $this->RequiredArguments = $lastRequired + 1; |
531: | $this->ParameterIndex = array_flip(array_values($this->Parameters)); |
532: | } |
533: | |
534: | |
535: | $this->NormalisedKeys = array_keys( |
536: | $this->Properties |
537: | + ($this->Actions[self::ACTION_GET] ?? []) |
538: | + ($this->Actions[self::ACTION_ISSET] ?? []) |
539: | + ($this->Actions[self::ACTION_SET] ?? []) |
540: | + ($this->Actions[self::ACTION_UNSET] ?? []) |
541: | ); |
542: | |
543: | |
544: | |
545: | $this->SerializableProperties = |
546: | array_merge( |
547: | array_values( |
548: | array_intersect( |
549: | ($this->ReadableProperties ?: $this->PublicProperties), |
550: | ($this->WritableProperties ?: $this->PublicProperties), |
551: | ) |
552: | ), |
553: | array_keys( |
554: | array_intersect_key( |
555: | ($this->Actions[self::ACTION_GET] ?? []), |
556: | ($this->Actions[self::ACTION_SET] ?? []), |
557: | ) |
558: | ) |
559: | ); |
560: | |
561: | if ($this->IsRelatable) { |
562: | |
563: | $relationships = $className::getRelationships(); |
564: | $relationships = Arr::combine( |
565: | $this->maybeNormalise(array_keys($relationships), NormaliserFlag::LAZY), |
566: | $relationships |
567: | ); |
568: | |
569: | |
570: | |
571: | |
572: | |
573: | |
574: | if ($this->IsTreeable) { |
575: | $parentMethod = $class->getMethod('getParentProperty'); |
576: | $parentClass = $parentMethod->getDeclaringClass(); |
577: | $childrenMethod = $class->getMethod('getChildrenProperty'); |
578: | $childrenClass = $childrenMethod->getDeclaringClass(); |
579: | |
580: | |
581: | |
582: | $service = $childrenClass->isSubclassOf($parentClass) |
583: | ? $childrenClass->getName() |
584: | : $parentClass->getName(); |
585: | |
586: | |
587: | $treeable = [ |
588: | $className::getParentProperty(), |
589: | $className::getChildrenProperty(), |
590: | ]; |
591: | |
592: | $treeable = array_unique($this->maybeNormalise( |
593: | $treeable, NormaliserFlag::LAZY |
594: | )); |
595: | |
596: | |
597: | |
598: | |
599: | if (count(array_intersect($this->NormalisedKeys, $treeable)) === 2) { |
600: | $this->ParentProperty = $treeable[0]; |
601: | $this->ChildrenProperty = $treeable[1]; |
602: | $this->OneToOneRelationships[$this->ParentProperty] = $service; |
603: | $this->OneToManyRelationships[$this->ChildrenProperty] = $service; |
604: | } else { |
605: | $this->IsTreeable = false; |
606: | } |
607: | } |
608: | |
609: | foreach ($relationships as $property => $reference) { |
610: | $type = array_key_first($reference); |
611: | $target = $reference[$type]; |
612: | if (!in_array($property, $this->NormalisedKeys, true)) { |
613: | continue; |
614: | } |
615: | if (!is_a($target, Relatable::class, true)) { |
616: | continue; |
617: | } |
618: | switch ($type) { |
619: | case Relatable::ONE_TO_ONE: |
620: | $this->OneToOneRelationships[$property] = $target; |
621: | break; |
622: | |
623: | case Relatable::ONE_TO_MANY: |
624: | $this->OneToManyRelationships[$property] = $target; |
625: | break; |
626: | } |
627: | } |
628: | } |
629: | |
630: | if ($this->HasDates) { |
631: | |
632: | $dates = $className::getDateProperties(); |
633: | |
634: | $nativeDates = []; |
635: | foreach ($properties as $property) { |
636: | if (!$property->hasType()) { |
637: | continue; |
638: | } |
639: | foreach (Reflect::getTypeNames($property->getType()) as $type) { |
640: | if (is_a($type, DateTimeInterface::class, true)) { |
641: | $nativeDates[] = $property->getName(); |
642: | continue 2; |
643: | } |
644: | } |
645: | } |
646: | |
647: | $this->DateKeys = |
648: | ['*'] === $dates |
649: | ? ($nativeDates |
650: | ? $this->maybeNormalise($nativeDates, NormaliserFlag::LAZY) |
651: | : $this->NormalisedKeys) |
652: | : array_intersect( |
653: | $this->NormalisedKeys, |
654: | $this->maybeNormalise(array_merge($dates, $nativeDates), NormaliserFlag::LAZY), |
655: | ); |
656: | } |
657: | } |
658: | |
659: | |
660: | |
661: | |
662: | |
663: | |
664: | |
665: | |
666: | |
667: | |
668: | |
669: | |
670: | |
671: | final public function maybeNormalise($value, int $flags = NormaliserFlag::GREEDY) |
672: | { |
673: | if (!$this->Normaliser) { |
674: | return $value; |
675: | } |
676: | switch (true) { |
677: | case $flags & NormaliserFlag::LAZY: |
678: | $normaliser = $this->GentleNormaliser; |
679: | break; |
680: | case $flags & NormaliserFlag::CAREFUL: |
681: | $normaliser = $this->CarefulNormaliser; |
682: | break; |
683: | default: |
684: | $normaliser = $this->Normaliser; |
685: | } |
686: | if (is_array($value)) { |
687: | return array_map($normaliser, $value); |
688: | } |
689: | |
690: | return ($normaliser)($value); |
691: | } |
692: | |
693: | |
694: | |
695: | |
696: | |
697: | |
698: | final public function getReadableProperties(): array |
699: | { |
700: | return array_keys(( |
701: | $this->ReadableProperties |
702: | ?: $this->PublicProperties |
703: | ) + ($this->Actions[self::ACTION_GET] ?? [])); |
704: | } |
705: | |
706: | |
707: | |
708: | |
709: | |
710: | |
711: | final public function getWritableProperties(): array |
712: | { |
713: | return array_keys(( |
714: | $this->WritableProperties |
715: | ?: $this->PublicProperties |
716: | ) + ($this->Actions[self::ACTION_SET] ?? [])); |
717: | } |
718: | |
719: | |
720: | |
721: | |
722: | |
723: | |
724: | |
725: | final public function propertyActionIsAllowed(string $property, string $action): bool |
726: | { |
727: | switch ($action) { |
728: | case self::ACTION_GET: |
729: | case self::ACTION_ISSET: |
730: | return in_array( |
731: | $property, |
732: | $this->getReadableProperties() |
733: | ); |
734: | |
735: | case self::ACTION_SET: |
736: | case self::ACTION_UNSET: |
737: | return in_array( |
738: | $property, |
739: | $this->getWritableProperties() |
740: | ); |
741: | } |
742: | |
743: | return false; |
744: | } |
745: | } |
746: | |