| 1: | <?php |
| 2: | namespace TIC\CoreBundle\Traits; |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | trait ContextProperties |
| 16: | { |
| 17: | protected $ctxClass; |
| 18: | protected $ctxType; |
| 19: | protected $ctxRoot; |
| 20: | protected $ctxName; |
| 21: | protected $ctxPath; |
| 22: | |
| 23: | protected $ctxBundle; |
| 24: | protected $ctxSnake; |
| 25: | protected $ctxRoute; |
| 26: | protected $ctxTrans; |
| 27: | protected $ctxMesg = 'item'; |
| 28: | protected $ctxForm; |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | public function setContextProperties(?string $classType = null): void |
| 38: | { |
| 39: | if (null === $this->ctxClass) $this->ctxClass = static::class; |
| 40: | |
| 41: | |
| 42: | if (null === $classType) $classType = 'Controller|Command|Form|Type|Repository|Model|Service'; |
| 43: | $re = '/^(.+)\\\\('.$classType.')\\\\(.+\\\\)?([^\\\\]+)('.$classType.')$/'; |
| 44: | |
| 45: | |
| 46: | if (\preg_match($re, $this->ctxClass, $match)) { |
| 47: | if (null === $this->ctxType) $this->ctxType = $match[2]; |
| 48: | if (null === $this->ctxRoot) $this->ctxRoot = $match[1]; |
| 49: | if (null === $this->ctxName) $this->ctxName = $match[4]; |
| 50: | if (null === $this->ctxPath) $this->ctxPath = \trim($match[3], "\\"); |
| 51: | } |
| 52: | |
| 53: | if (null === $this->ctxBundle) |
| 54: | $this->ctxBundle = \preg_match('/^(.+)Bundle$/', $this->ctxRoot, $match) ? '@'.\str_replace("\\",'',$match[1]) : null; |
| 55: | |
| 56: | if (null === $this->ctxSnake) |
| 57: | $this->ctxSnake = \strtolower(\trim(\preg_replace("/([A-Z][^A-Z])/", '_\1', $this->ctxName), '_')); |
| 58: | |
| 59: | |
| 60: | if (null === $this->ctxRoute) { |
| 61: | if ($this->ctxBundle) { |
| 62: | $this->ctxRoute = \strtolower(\str_replace('@', '', $this->ctxBundle)); |
| 63: | if (! empty($this->ctxPath)) $this->ctxRoute.= '_' . \strtolower($this->ctxPath); |
| 64: | } else { |
| 65: | $this->ctxRoute = empty($this->ctxPath) ? 'app' : \strtolower($this->ctxPath); |
| 66: | } |
| 67: | $this->ctxRoute.= '_' . \strtolower($this->ctxName) . '_'; |
| 68: | } |
| 69: | |
| 70: | |
| 71: | if (null === $this->ctxTrans) |
| 72: | $this->ctxTrans = \strtr($this->ctxRoute, '_', '.'); |
| 73: | |
| 74: | if (null === $this->ctxMesg) |
| 75: | $this->ctxMesg = \strtolower($this->ctxName); |
| 76: | |
| 77: | |
| 78: | if (null === $this->ctxForm) { |
| 79: | $formClass = array( $this->ctxRoot, "Form" ); |
| 80: | if (! empty($this->ctxPath)) $formClass[] = $this->ctxPath; |
| 81: | $formClass[] = $this->ctxName . "Type"; |
| 82: | $this->ctxForm = \implode("\\", $formClass); |
| 83: | } |
| 84: | |
| 85: | |
| 86: | } |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | protected function getContextProperties(): array |
| 92: | { |
| 93: | return array( |
| 94: | "class" => $this->ctxClass, |
| 95: | "type" => $this->ctxType, |
| 96: | "root" => $this->ctxRoot, |
| 97: | "name" => $this->ctxName, |
| 98: | "path" => $this->ctxPath, |
| 99: | "bundle" => $this->ctxBundle, |
| 100: | "snake" => $this->ctxSnake, |
| 101: | "route" => $this->ctxRoute, |
| 102: | "trans" => $this->ctxTrans, |
| 103: | "mesg" => $this->ctxMesg, |
| 104: | "form" => $this->ctxForm, |
| 105: | ); |
| 106: | } |
| 107: | |
| 108: | } |
| 109: | |