From 7de136815a9375e97083f2938ef56b78b3cc109d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 29 Nov 2015 22:03:06 +0100 Subject: [PATCH] Init --- Account/Account.php | 390 + Account/AccountManager.php | 86 + Account/AccountStatus.php | 37 + Account/AccountType.php | 35 + Account/Group.php | 141 + Account/NullAccount.php | 31 + ApplicationAbstract.php | 127 + Asset/AssetManager.php | 119 + Asset/AssetType.php | 37 + Auth/Auth.php | 165 + Auth/LoginReturnType.php | 45 + Autoloader.php | 75 + Config/OptionsInterface.php | 83 + Config/OptionsTrait.php | 80 + Config/SettingsAbstract.php | 134 + Console/CommandManager.php | 137 + Contract/ArrayableInterface.php | 44 + Contract/JsonableInterface.php | 46 + Contract/Object.php | 31 + Contract/RenderableInterface.php | 45 + DataStorage/Cache/CacheInterface.php | 135 + DataStorage/Cache/CacheManager.php | 248 + DataStorage/Cache/CacheStatus.php | 41 + DataStorage/Cache/CacheType.php | 40 + DataStorage/Cache/FileCache.php | 116 + DataStorage/Cache/MemCache.php | 163 + DataStorage/Cache/NullCache.php | 96 + DataStorage/Cache/RedisCache.php | 98 + DataStorage/Cache/WinCache.php | 98 + DataStorage/Cookie/CookieJar.php | 29 + DataStorage/DataMapperInterface.php | 138 + .../Connection/ConnectionAbstract.php | 153 + .../Database/Connection/ConnectionFactory.php | 67 + .../Connection/ConnectionInterface.php | 88 + .../Database/Connection/MysqlConnection.php | 78 + .../Connection/PostgresConnection.php | 0 .../Database/Connection/SQLiteConnection.php | 78 + .../Connection/SqlServerConnection.php | 0 .../Database/Connector/MysqlConnector.php | 0 .../Database/Connector/PostgresConnector.php | 0 .../Database/Connector/SQLiteConnector.php | 0 .../Database/Connector/SqlServerConnector.php | 0 DataStorage/Database/DataMapperAbstract.php | 600 + DataStorage/Database/DatabaseStatus.php | 41 + DataStorage/Database/DatabaseType.php | 40 + DataStorage/Database/Grammar.php | 39 + DataStorage/Database/Pool.php | 136 + DataStorage/Database/Query/Builder.php | 883 + DataStorage/Database/Query/Column.php | 58 + DataStorage/Database/Query/Expression.php | 0 .../Database/Query/Grammar/Grammar.php | 533 + .../Query/Grammar/GrammarInterface.php | 23 + .../Database/Query/Grammar/MysqlGrammar.php | 29 + .../Query/Grammar/PostgresGrammar.php | 0 .../Database/Query/Grammar/SQLiteGrammar.php | 29 + .../Query/Grammar/SqlServerGrammar.php | 0 DataStorage/Database/Query/QueryType.php | 37 + DataStorage/Database/Schema/Builder.php | 0 .../Schema/Grammar/GrammarInterface.php | 61 + .../Database/Schema/Grammar/MysqlGrammar.php | 0 .../Schema/Grammar/PostgresGrammar.php | 0 .../Database/Schema/Grammar/SQLiteGrammar.php | 0 .../Schema/Grammar/SqlServerGrammar.php | 0 DataStorage/Database/Schema/QueryType.php | 38 + DataStorage/Session/ConsoleSession.php | 99 + DataStorage/Session/HttpSession.php | 128 + DataStorage/Session/README.md | 1 + DataStorage/Session/SessionInterface.php | 102 + DataStorage/Session/SocketSession.php | 99 + Datatypes/Address.php | 168 + Datatypes/AddressType.php | 36 + Datatypes/Enum.php | 98 + Datatypes/EnumArray.php | 109 + Datatypes/ExactFloat.php | 130 + Datatypes/Exception/InvalidEnumName.php | 39 + Datatypes/Exception/InvalidEnumValue.php | 39 + Datatypes/Location.php | 259 + Datatypes/PhoneType.php | 35 + Datatypes/SmartDateTime.php | 102 + Dispatcher/Dispatcher.php | 160 + Event/EventManager.php | 134 + Html/TagType.php | 41 + Install/Backup.php | 0 Install/DummyFactory.php | 55 + Install/DummyInterface.php | 47 + Install/Library.php | 0 Install/README.md | 1 + Install/Uninstall.php | 0 Install/Update.php | 44 + Localization/CityGeoLocationsEnumArray.php | 251554 +++++++++++++++ Localization/ISO3166Enum.php | 522 + Localization/ISO3166EnumArray.php | 280 + Localization/ISO4217Enum.php | 260 + Localization/ISO4217EnumArray.php | 149 + Localization/ISO639Enum.php | 400 + Localization/ISO639EnumArray.php | 219 + Localization/ISO8601EnumArray.php | 42 + Localization/L11nManager.php | 107 + Localization/Localization.php | 287 + Localization/NullLocalization.php | 34 + Localization/PhoneEnum.php | 262 + Localization/PhoneEnumArray.php | 264 + Localization/TimeZoneEnumArray.php | 451 + Localization/lang/en.lang.php | 45 + Log/FileLogger.php | 545 + Log/LogLevel.php | 41 + Log/LoggerInterface.php | 122 + Log/README.md | 1 + Math/Complex.php | 59 + Math/Finance/FinanceFormulas.php | 446 + Math/Finance/Forecasting/AR.php | 1 + Math/Finance/Forecasting/ARCH.php | 1 + Math/Finance/Forecasting/ARFIMA.php | 1 + Math/Finance/Forecasting/ARIMA.php | 1 + Math/Finance/Forecasting/ARMA.php | 1 + .../Forecasting/ExponentialSmoothing.php | 1 + .../Forecasting/ForecastingInterface.php | 1 + Math/Finance/Forecasting/GARCH.php | 1 + Math/Finance/Forecasting/MA.php | 1 + Math/Finance/Forecasting/NAR.php | 1 + Math/Finance/Forecasting/NMA.php | 1 + Math/Finance/Forecasting/Regression.php | 1 + Math/Finance/Forecasting/SARIMA.php | 0 Math/Matrix.php | 0 Math/Matrix/Matrix.php | 19 + Math/Matrix/Vector.php | 67 + Math/Number/Complex.php | 14 + Math/Number/Integer.php | 9 + Math/Number/Irrational.php | 2 + Math/Number/Natural.php | 2 + Math/Number/Number.php | 5 + Math/Number/NumberInterface.php | 8 + Math/Number/NumberType.php | 11 + Math/Number/Rational.php | 3 + Math/Number/Real.php | 2 + Math/NumberType.php | 0 Math/README.md | 1 + Math/Shape/D2/Circle.php | 0 Math/Shape/D2/Ellipse.php | 0 Math/Shape/D2/Polygon.php | 286 + Math/Shape/D2/Quadrilateral.php | 7 + Math/Shape/D2/Rectangle.php | 0 Math/Shape/D2/Shape2DInterface.php | 155 + Math/Shape/D2/Square.php | 0 Math/Shape/D2/Trapezoid.php | 0 Math/Shape/D2/Triangle.php | 0 Math/Shape/D3/Cone.php | 0 Math/Shape/D3/Cube.php | 0 Math/Shape/D3/Cylinder.php | 0 Math/Shape/D3/Prism.php | 0 Math/Shape/D3/Pyramid.php | 0 Math/Shape/D3/Sphere.php | 0 Math/Shape/D3/SquarePyramid.php | 0 Math/Shape/D3/Tetrahedron.php | 0 Math/Shape/ShapeInterface.php | 31 + Math/Stochastic/Mean.php | 1 + Math/Vector.php | 0 Message/Http/BrowserType.php | 44 + Message/Http/OSType.php | 58 + Message/Http/Request.php | 382 + Message/Http/Response.php | 324 + Message/Http/Rest.php | 88 + Message/Mail/Imap.php | 142 + Message/Mail/Mail.php | 62 + Message/Mail/MailType.php | 38 + Message/Mail/OAuth.php | 0 Message/Mail/Pop3.php | 0 Message/Mail/Smtp.php | 0 Message/MessageInterface.php | 141 + Message/Request/Lock/FileLock.php | 0 Message/Request/Lock/MemcacheLock.php | 0 Message/Request/Lock/SemLock.php | 0 Message/Request/Lock/SessionLock.php | 0 Message/Request/README.md | 1 + Message/RequestAbstract.php | 251 + Message/RequestDestination.php | 52 + Message/RequestInterface.php | 73 + Message/RequestMethod.php | 39 + Message/RequestSource.php | 36 + Message/RequestStatus.php | 142 + Message/ResponseAbstract.php | 161 + Message/ResponseInterface.php | 33 + Message/ResponseType.php | 37 + Message/Socket/Request.php | 0 Message/Socket/Response.php | 0 Message/UploadedFileInterface.php | 83 + Model/Html/Head.php | 318 + Model/Html/Meta.php | 227 + Module/ConsoleInterface.php | 41 + Module/InfoManager.php | 82 + Module/InstallerAbstract.php | 48 + Module/ModuleAbstract.php | 181 + Module/ModuleFactory.php | 107 + Module/ModuleManager.php | 476 + Module/SocketInterface.php | 41 + Module/WebInterface.php | 31 + Pattern/Mediator.php | 75 + Pattern/Multition.php | 41 + Pattern/Observer.php | 45 + Pattern/Singleton.php | 51 + Pattern/Subject.php | 67 + Router/Router.php | 137 + Security/Encryption/Encryption.php | 431 + Socket/Client/Client.php | 127 + Socket/CommandManager.php | 126 + Socket/Packets/Header.php | 192 + Socket/Packets/PacketAbstract.php | 103 + Socket/Packets/PacketManager.php | 89 + Socket/Packets/PacketType.php | 44 + Socket/Server/ClientManager.php | 21 + Socket/Server/Server.php | 224 + Socket/SocketAbstract.php | 98 + Socket/SocketInterface.php | 58 + Socket/SocketType.php | 35 + Stdlib/PriorityQueue.php | 207 + System/ArrayParser.php | 160 + System/FilePathException.php | 37 + System/FileSystem.php | 141 + System/LockHandler.php | 0 System/MimeType.php | 1021 + Tests/README.md | 13 + Uri/Http.php | 363 + Uri/InvalidUriException.php | 38 + Uri/UriFactory.php | 116 + Uri/UriInterface.php | 145 + Uri/UriScheme.php | 48 + Utils/ArrayUtils.php | 222 + Utils/Color.php | 84 + Utils/Compiler/Php/ClassCompiler.php | 170 + Utils/Converter/Currency.php | 162 + Utils/Converter/File.php | 85 + Utils/Converter/Measurement.php | 80 + Utils/Converter/Numeric.php | 198 + Utils/Converter/TemperatureType.php | 37 + Utils/Excel/Excel.php | 31 + Utils/IO/Csv/CsvDatabaseMapper.php | 90 + Utils/IO/Csv/CsvInterface.php | 55 + Utils/IO/Csv/CsvSettingsTrait.php | 53 + Utils/IO/Excel/ExcelDatabaseMapper.php | 44 + Utils/IO/Excel/ExcelInterface.php | 55 + Utils/IO/ExchangeInterface.php | 36 + Utils/IO/IODatabaseMapper.php | 23 + Utils/IO/Json/InvalidJsonException.php | 35 + Utils/IO/Json/JsonInterface.php | 55 + Utils/IO/Pdf/PdfInterface.php | 43 + Utils/IO/Zip/Zip.php | 93 + Utils/JsonBuilder.php | 110 + Utils/MultiMap.php | 272 + Utils/PDF/Pdf.php | 31 + Utils/Parser/Markdown/License.txt | 20 + Utils/Parser/Markdown/Markdown.php | 1117 + Utils/RnG/Address.php | 0 Utils/RnG/City.php | 0 Utils/RnG/Country.php | 0 Utils/RnG/Currency.php | 0 Utils/RnG/DateTime.php | 50 + Utils/RnG/DistributionType.php | 36 + Utils/RnG/Email.php | 0 Utils/RnG/File.php | 145 + Utils/RnG/IBAN.php | 0 Utils/RnG/Name.php | 491 + Utils/RnG/Numeric.php | 0 Utils/RnG/Phone.php | 70 + Utils/RnG/PostalZip.php | 0 Utils/RnG/String.php | 56 + Utils/RnG/Text.php | 508 + Utils/StringUtils.php | 209 + Utils/SystemUtils.php | 154 + Validation/Barcode/Barcode.php | 0 Validation/Barcode/Barcode11.php | 0 Validation/Barcode/Barcode128.php | 0 Validation/Barcode/Barcode39.php | 0 Validation/Barcode/Barcode93.php | 0 Validation/Barcode/BarcodeCodebar.php | 0 Validation/Barcode/BarcodeDatamatrix.php | 0 Validation/Barcode/BarcodeEAN.php | 0 Validation/Barcode/BarcodeI25.php | 0 Validation/Barcode/BarcodeMSI.php | 0 Validation/Barcode/QrCode.php | 0 Validation/Base/BIC.php | 51 + Validation/Base/CreditCard.php | 74 + Validation/Base/Email.php | 51 + Validation/Base/Hostname.php | 51 + Validation/Base/IBAN.php | 81 + Validation/Base/IP.php | 51 + Validation/File/Crc32.php | 0 Validation/File/Extension.php | 0 Validation/File/FileSize.php | 0 Validation/File/Hash.php | 0 Validation/File/IsCompressed.php | 0 Validation/File/IsExecutable.php | 0 Validation/File/IsImage.php | 0 Validation/File/IsPDF.php | 0 Validation/File/MimeType.php | 0 Validation/ModelValidationTrait.php | 98 + Validation/Validator.php | 182 + Validation/ValidatorAbstract.php | 55 + Validation/ValidatorInterface.php | 53 + Version/Version.php | 100 + Version/info.json | 10 + Views/View.php | 357 + Views/ViewLayout.php | 45 + 302 files changed, 278923 insertions(+) create mode 100644 Account/Account.php create mode 100644 Account/AccountManager.php create mode 100644 Account/AccountStatus.php create mode 100644 Account/AccountType.php create mode 100644 Account/Group.php create mode 100644 Account/NullAccount.php create mode 100644 ApplicationAbstract.php create mode 100644 Asset/AssetManager.php create mode 100644 Asset/AssetType.php create mode 100644 Auth/Auth.php create mode 100644 Auth/LoginReturnType.php create mode 100644 Autoloader.php create mode 100644 Config/OptionsInterface.php create mode 100644 Config/OptionsTrait.php create mode 100644 Config/SettingsAbstract.php create mode 100644 Console/CommandManager.php create mode 100644 Contract/ArrayableInterface.php create mode 100644 Contract/JsonableInterface.php create mode 100644 Contract/Object.php create mode 100644 Contract/RenderableInterface.php create mode 100644 DataStorage/Cache/CacheInterface.php create mode 100644 DataStorage/Cache/CacheManager.php create mode 100644 DataStorage/Cache/CacheStatus.php create mode 100644 DataStorage/Cache/CacheType.php create mode 100644 DataStorage/Cache/FileCache.php create mode 100644 DataStorage/Cache/MemCache.php create mode 100644 DataStorage/Cache/NullCache.php create mode 100644 DataStorage/Cache/RedisCache.php create mode 100644 DataStorage/Cache/WinCache.php create mode 100644 DataStorage/Cookie/CookieJar.php create mode 100644 DataStorage/DataMapperInterface.php create mode 100644 DataStorage/Database/Connection/ConnectionAbstract.php create mode 100644 DataStorage/Database/Connection/ConnectionFactory.php create mode 100644 DataStorage/Database/Connection/ConnectionInterface.php create mode 100644 DataStorage/Database/Connection/MysqlConnection.php create mode 100644 DataStorage/Database/Connection/PostgresConnection.php create mode 100644 DataStorage/Database/Connection/SQLiteConnection.php create mode 100644 DataStorage/Database/Connection/SqlServerConnection.php create mode 100644 DataStorage/Database/Connector/MysqlConnector.php create mode 100644 DataStorage/Database/Connector/PostgresConnector.php create mode 100644 DataStorage/Database/Connector/SQLiteConnector.php create mode 100644 DataStorage/Database/Connector/SqlServerConnector.php create mode 100644 DataStorage/Database/DataMapperAbstract.php create mode 100644 DataStorage/Database/DatabaseStatus.php create mode 100644 DataStorage/Database/DatabaseType.php create mode 100644 DataStorage/Database/Grammar.php create mode 100644 DataStorage/Database/Pool.php create mode 100644 DataStorage/Database/Query/Builder.php create mode 100644 DataStorage/Database/Query/Column.php create mode 100644 DataStorage/Database/Query/Expression.php create mode 100644 DataStorage/Database/Query/Grammar/Grammar.php create mode 100644 DataStorage/Database/Query/Grammar/GrammarInterface.php create mode 100644 DataStorage/Database/Query/Grammar/MysqlGrammar.php create mode 100644 DataStorage/Database/Query/Grammar/PostgresGrammar.php create mode 100644 DataStorage/Database/Query/Grammar/SQLiteGrammar.php create mode 100644 DataStorage/Database/Query/Grammar/SqlServerGrammar.php create mode 100644 DataStorage/Database/Query/QueryType.php create mode 100644 DataStorage/Database/Schema/Builder.php create mode 100644 DataStorage/Database/Schema/Grammar/GrammarInterface.php create mode 100644 DataStorage/Database/Schema/Grammar/MysqlGrammar.php create mode 100644 DataStorage/Database/Schema/Grammar/PostgresGrammar.php create mode 100644 DataStorage/Database/Schema/Grammar/SQLiteGrammar.php create mode 100644 DataStorage/Database/Schema/Grammar/SqlServerGrammar.php create mode 100644 DataStorage/Database/Schema/QueryType.php create mode 100644 DataStorage/Session/ConsoleSession.php create mode 100644 DataStorage/Session/HttpSession.php create mode 100644 DataStorage/Session/README.md create mode 100644 DataStorage/Session/SessionInterface.php create mode 100644 DataStorage/Session/SocketSession.php create mode 100644 Datatypes/Address.php create mode 100644 Datatypes/AddressType.php create mode 100644 Datatypes/Enum.php create mode 100644 Datatypes/EnumArray.php create mode 100644 Datatypes/ExactFloat.php create mode 100644 Datatypes/Exception/InvalidEnumName.php create mode 100644 Datatypes/Exception/InvalidEnumValue.php create mode 100644 Datatypes/Location.php create mode 100644 Datatypes/PhoneType.php create mode 100644 Datatypes/SmartDateTime.php create mode 100644 Dispatcher/Dispatcher.php create mode 100644 Event/EventManager.php create mode 100644 Html/TagType.php create mode 100644 Install/Backup.php create mode 100644 Install/DummyFactory.php create mode 100644 Install/DummyInterface.php create mode 100644 Install/Library.php create mode 100644 Install/README.md create mode 100644 Install/Uninstall.php create mode 100644 Install/Update.php create mode 100644 Localization/CityGeoLocationsEnumArray.php create mode 100644 Localization/ISO3166Enum.php create mode 100644 Localization/ISO3166EnumArray.php create mode 100644 Localization/ISO4217Enum.php create mode 100644 Localization/ISO4217EnumArray.php create mode 100644 Localization/ISO639Enum.php create mode 100644 Localization/ISO639EnumArray.php create mode 100644 Localization/ISO8601EnumArray.php create mode 100644 Localization/L11nManager.php create mode 100644 Localization/Localization.php create mode 100644 Localization/NullLocalization.php create mode 100644 Localization/PhoneEnum.php create mode 100644 Localization/PhoneEnumArray.php create mode 100644 Localization/TimeZoneEnumArray.php create mode 100644 Localization/lang/en.lang.php create mode 100644 Log/FileLogger.php create mode 100644 Log/LogLevel.php create mode 100644 Log/LoggerInterface.php create mode 100644 Log/README.md create mode 100644 Math/Complex.php create mode 100644 Math/Finance/FinanceFormulas.php create mode 100644 Math/Finance/Forecasting/AR.php create mode 100644 Math/Finance/Forecasting/ARCH.php create mode 100644 Math/Finance/Forecasting/ARFIMA.php create mode 100644 Math/Finance/Forecasting/ARIMA.php create mode 100644 Math/Finance/Forecasting/ARMA.php create mode 100644 Math/Finance/Forecasting/ExponentialSmoothing.php create mode 100644 Math/Finance/Forecasting/ForecastingInterface.php create mode 100644 Math/Finance/Forecasting/GARCH.php create mode 100644 Math/Finance/Forecasting/MA.php create mode 100644 Math/Finance/Forecasting/NAR.php create mode 100644 Math/Finance/Forecasting/NMA.php create mode 100644 Math/Finance/Forecasting/Regression.php create mode 100644 Math/Finance/Forecasting/SARIMA.php create mode 100644 Math/Matrix.php create mode 100644 Math/Matrix/Matrix.php create mode 100644 Math/Matrix/Vector.php create mode 100644 Math/Number/Complex.php create mode 100644 Math/Number/Integer.php create mode 100644 Math/Number/Irrational.php create mode 100644 Math/Number/Natural.php create mode 100644 Math/Number/Number.php create mode 100644 Math/Number/NumberInterface.php create mode 100644 Math/Number/NumberType.php create mode 100644 Math/Number/Rational.php create mode 100644 Math/Number/Real.php create mode 100644 Math/NumberType.php create mode 100644 Math/README.md create mode 100644 Math/Shape/D2/Circle.php create mode 100644 Math/Shape/D2/Ellipse.php create mode 100644 Math/Shape/D2/Polygon.php create mode 100644 Math/Shape/D2/Quadrilateral.php create mode 100644 Math/Shape/D2/Rectangle.php create mode 100644 Math/Shape/D2/Shape2DInterface.php create mode 100644 Math/Shape/D2/Square.php create mode 100644 Math/Shape/D2/Trapezoid.php create mode 100644 Math/Shape/D2/Triangle.php create mode 100644 Math/Shape/D3/Cone.php create mode 100644 Math/Shape/D3/Cube.php create mode 100644 Math/Shape/D3/Cylinder.php create mode 100644 Math/Shape/D3/Prism.php create mode 100644 Math/Shape/D3/Pyramid.php create mode 100644 Math/Shape/D3/Sphere.php create mode 100644 Math/Shape/D3/SquarePyramid.php create mode 100644 Math/Shape/D3/Tetrahedron.php create mode 100644 Math/Shape/ShapeInterface.php create mode 100644 Math/Stochastic/Mean.php create mode 100644 Math/Vector.php create mode 100644 Message/Http/BrowserType.php create mode 100644 Message/Http/OSType.php create mode 100644 Message/Http/Request.php create mode 100644 Message/Http/Response.php create mode 100644 Message/Http/Rest.php create mode 100644 Message/Mail/Imap.php create mode 100644 Message/Mail/Mail.php create mode 100644 Message/Mail/MailType.php create mode 100644 Message/Mail/OAuth.php create mode 100644 Message/Mail/Pop3.php create mode 100644 Message/Mail/Smtp.php create mode 100644 Message/MessageInterface.php create mode 100644 Message/Request/Lock/FileLock.php create mode 100644 Message/Request/Lock/MemcacheLock.php create mode 100644 Message/Request/Lock/SemLock.php create mode 100644 Message/Request/Lock/SessionLock.php create mode 100644 Message/Request/README.md create mode 100644 Message/RequestAbstract.php create mode 100644 Message/RequestDestination.php create mode 100644 Message/RequestInterface.php create mode 100644 Message/RequestMethod.php create mode 100644 Message/RequestSource.php create mode 100644 Message/RequestStatus.php create mode 100644 Message/ResponseAbstract.php create mode 100644 Message/ResponseInterface.php create mode 100644 Message/ResponseType.php create mode 100644 Message/Socket/Request.php create mode 100644 Message/Socket/Response.php create mode 100644 Message/UploadedFileInterface.php create mode 100644 Model/Html/Head.php create mode 100644 Model/Html/Meta.php create mode 100644 Module/ConsoleInterface.php create mode 100644 Module/InfoManager.php create mode 100644 Module/InstallerAbstract.php create mode 100644 Module/ModuleAbstract.php create mode 100644 Module/ModuleFactory.php create mode 100644 Module/ModuleManager.php create mode 100644 Module/SocketInterface.php create mode 100644 Module/WebInterface.php create mode 100644 Pattern/Mediator.php create mode 100644 Pattern/Multition.php create mode 100644 Pattern/Observer.php create mode 100644 Pattern/Singleton.php create mode 100644 Pattern/Subject.php create mode 100644 Router/Router.php create mode 100644 Security/Encryption/Encryption.php create mode 100644 Socket/Client/Client.php create mode 100644 Socket/CommandManager.php create mode 100644 Socket/Packets/Header.php create mode 100644 Socket/Packets/PacketAbstract.php create mode 100644 Socket/Packets/PacketManager.php create mode 100644 Socket/Packets/PacketType.php create mode 100644 Socket/Server/ClientManager.php create mode 100644 Socket/Server/Server.php create mode 100644 Socket/SocketAbstract.php create mode 100644 Socket/SocketInterface.php create mode 100644 Socket/SocketType.php create mode 100644 Stdlib/PriorityQueue.php create mode 100644 System/ArrayParser.php create mode 100644 System/FilePathException.php create mode 100644 System/FileSystem.php create mode 100644 System/LockHandler.php create mode 100644 System/MimeType.php create mode 100644 Tests/README.md create mode 100644 Uri/Http.php create mode 100644 Uri/InvalidUriException.php create mode 100644 Uri/UriFactory.php create mode 100644 Uri/UriInterface.php create mode 100644 Uri/UriScheme.php create mode 100644 Utils/ArrayUtils.php create mode 100644 Utils/Color.php create mode 100644 Utils/Compiler/Php/ClassCompiler.php create mode 100644 Utils/Converter/Currency.php create mode 100644 Utils/Converter/File.php create mode 100644 Utils/Converter/Measurement.php create mode 100644 Utils/Converter/Numeric.php create mode 100644 Utils/Converter/TemperatureType.php create mode 100644 Utils/Excel/Excel.php create mode 100644 Utils/IO/Csv/CsvDatabaseMapper.php create mode 100644 Utils/IO/Csv/CsvInterface.php create mode 100644 Utils/IO/Csv/CsvSettingsTrait.php create mode 100644 Utils/IO/Excel/ExcelDatabaseMapper.php create mode 100644 Utils/IO/Excel/ExcelInterface.php create mode 100644 Utils/IO/ExchangeInterface.php create mode 100644 Utils/IO/IODatabaseMapper.php create mode 100644 Utils/IO/Json/InvalidJsonException.php create mode 100644 Utils/IO/Json/JsonInterface.php create mode 100644 Utils/IO/Pdf/PdfInterface.php create mode 100644 Utils/IO/Zip/Zip.php create mode 100644 Utils/JsonBuilder.php create mode 100644 Utils/MultiMap.php create mode 100644 Utils/PDF/Pdf.php create mode 100644 Utils/Parser/Markdown/License.txt create mode 100644 Utils/Parser/Markdown/Markdown.php create mode 100644 Utils/RnG/Address.php create mode 100644 Utils/RnG/City.php create mode 100644 Utils/RnG/Country.php create mode 100644 Utils/RnG/Currency.php create mode 100644 Utils/RnG/DateTime.php create mode 100644 Utils/RnG/DistributionType.php create mode 100644 Utils/RnG/Email.php create mode 100644 Utils/RnG/File.php create mode 100644 Utils/RnG/IBAN.php create mode 100644 Utils/RnG/Name.php create mode 100644 Utils/RnG/Numeric.php create mode 100644 Utils/RnG/Phone.php create mode 100644 Utils/RnG/PostalZip.php create mode 100644 Utils/RnG/String.php create mode 100644 Utils/RnG/Text.php create mode 100644 Utils/StringUtils.php create mode 100644 Utils/SystemUtils.php create mode 100644 Validation/Barcode/Barcode.php create mode 100644 Validation/Barcode/Barcode11.php create mode 100644 Validation/Barcode/Barcode128.php create mode 100644 Validation/Barcode/Barcode39.php create mode 100644 Validation/Barcode/Barcode93.php create mode 100644 Validation/Barcode/BarcodeCodebar.php create mode 100644 Validation/Barcode/BarcodeDatamatrix.php create mode 100644 Validation/Barcode/BarcodeEAN.php create mode 100644 Validation/Barcode/BarcodeI25.php create mode 100644 Validation/Barcode/BarcodeMSI.php create mode 100644 Validation/Barcode/QrCode.php create mode 100644 Validation/Base/BIC.php create mode 100644 Validation/Base/CreditCard.php create mode 100644 Validation/Base/Email.php create mode 100644 Validation/Base/Hostname.php create mode 100644 Validation/Base/IBAN.php create mode 100644 Validation/Base/IP.php create mode 100644 Validation/File/Crc32.php create mode 100644 Validation/File/Extension.php create mode 100644 Validation/File/FileSize.php create mode 100644 Validation/File/Hash.php create mode 100644 Validation/File/IsCompressed.php create mode 100644 Validation/File/IsExecutable.php create mode 100644 Validation/File/IsImage.php create mode 100644 Validation/File/IsPDF.php create mode 100644 Validation/File/MimeType.php create mode 100644 Validation/ModelValidationTrait.php create mode 100644 Validation/Validator.php create mode 100644 Validation/ValidatorAbstract.php create mode 100644 Validation/ValidatorInterface.php create mode 100644 Version/Version.php create mode 100644 Version/info.json create mode 100644 Views/View.php create mode 100644 Views/ViewLayout.php diff --git a/Account/Account.php b/Account/Account.php new file mode 100644 index 000000000..20f2a0d94 --- /dev/null +++ b/Account/Account.php @@ -0,0 +1,390 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +use phpOMS\Localization\Localization; +use phpOMS\Localization\NullLocalization; + +/** + * Account manager class. + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Account +{ + + /** + * Id. + * + * @var \int + * @since 1.0.0 + */ + protected $id = 0; + + /** + * Names. + * + * @var \string + * @since 1.0.0 + */ + protected $name1 = ''; + + /** + * Names. + * + * @var \string + * @since 1.0.0 + */ + protected $name2 = ''; + + /** + * Names. + * + * @var \string + * @since 1.0.0 + */ + protected $name3 = ''; + + /** + * Email. + * + * @var \string + * @since 1.0.0 + */ + protected $email = ''; + + /** + * Ip. + * + * Used in order to make sure ips don't change + * + * @var \string + * @since 1.0.0 + */ + protected $origin = ''; + + /** + * Login. + * + * @var \string + * @since 1.0.0 + */ + protected $login = ''; + + /** + * Last activity. + * + * @var \DateTime + * @since 1.0.0 + */ + protected $lastActive = null; + + /** + * Permissions. + * + * @var array + * @since 1.0.0 + */ + protected $permissions = []; + + /** + * Groups. + * + * @var \int[] + * @since 1.0.0 + */ + protected $groups = []; + + /** + * Account type. + * + * @var AccountType + * @since 1.0.0 + */ + protected $type = AccountType::USER; + + /** + * Account status. + * + * @var AccountStatus + * @since 1.0.0 + */ + protected $status = AccountStatus::INACTIVE; + + /** + * Localization. + * + * @var Localization + * @since 1.0.0 + */ + protected $l11n = null; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * Get account id. + * + * @return \int Account id + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getId() : \int + { + return $this->id; + } + + /** + * Get localization. + * + * @return Localization + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getL11n() : Localization + { + return $this->l11n ?? new NullLocalization(); + } + + /** + * Set localization. + * + * @param Localization $l11n Localization + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setL11n(Localization $l11n) + { + $this->l11n = $l11n; + } + + /** + * Get name1. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getName1() : \string + { + return $this->name1; + } + + /** + * Get name2. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getName2() : \string + { + return $this->name2; + } + + /** + * Get name3. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getName3() : \string + { + return $this->name3; + } + + /** + * Get email. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEmail() : \string + { + return $this->email; + } + + /** + * Get status. + * + * AccountStatus + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getStatus() : \int + { + return $this->status; + } + + /** + * Get type. + * + * AccountType + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getType() : \int + { + return $this->type; + } + + /** + * Get last activity. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getLastActive() : \DateTime + { + return $this->lastActive ?? new \DateTime('NOW'); + } + + /** + * Set name1. + * + * @param \string $name Name + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setName1(\string $name) + { + $this->name1 = $name; + } + + /** + * Set name2. + * + * @param \string $name Name + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setName2(\string $name) + { + $this->name2 = $name; + } + + /** + * Set name3. + * + * @param \string $name Name + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setName3(\string $name) + { + $this->name3 = $name; + } + + /** + * Set email. + * + * @param \string $email Email + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setEmail(\string $email) + { + $this->email = $email; + } + + /** + * Get status. + * + * @param \int $status Status + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setStatus(\int $status) + { + $this->status = $status; + } + + /** + * Get type. + * + * @param \int $type Type + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setType(\int $type) + { + $this->type = $type; + } + + /** + * Get last activity. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function updateLastActive() + { + $this->lastActive = new \DateTime('NOW'); + } + +} diff --git a/Account/AccountManager.php b/Account/AccountManager.php new file mode 100644 index 000000000..938a866bb --- /dev/null +++ b/Account/AccountManager.php @@ -0,0 +1,86 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +/** + * Account manager class. + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class AccountManager +{ + + /** + * Accounts. + * + * @var Account[] + * @since 1.0.0 + */ + private $accounts = []; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * Get account. + * + * @param \int $id Account id + * + * @return Account + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get(\int $id) : Account + { + return $this->accounts[$id] ?? new NullAccount(); + } + + /** + * Set account. + * + * @param Account $account Account + * + * @return \int Account id + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(Account $account) + { + if (!isset($this->accounts[$account->getId()])) { + $this->accounts[$account->getId()] = $account; + + return $account->getId(); + } + + return null; + } + +} diff --git a/Account/AccountStatus.php b/Account/AccountStatus.php new file mode 100644 index 000000000..2e1c024e4 --- /dev/null +++ b/Account/AccountStatus.php @@ -0,0 +1,37 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +use phpOMS\Datatypes\Enum; + +/** + * Account type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class AccountStatus extends Enum +{ + const ACTIVE = 1; + const INACTIVE = 2; + const TIMEOUT = 3; + const BANNED = 4; +} diff --git a/Account/AccountType.php b/Account/AccountType.php new file mode 100644 index 000000000..bfd342cca --- /dev/null +++ b/Account/AccountType.php @@ -0,0 +1,35 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +use phpOMS\Datatypes\Enum; + +/** + * Account type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class AccountType extends Enum +{ + const USER = 0; + const GROUP = 1; +} diff --git a/Account/Group.php b/Account/Group.php new file mode 100644 index 000000000..bc927f905 --- /dev/null +++ b/Account/Group.php @@ -0,0 +1,141 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +class Group +{ + + /** + * Account id. + * + * @var \int + * @since 1.0.0 + */ + protected $id = 0; + + /** + * Account name. + * + * @var \string + * @since 1.0.0 + */ + protected $name = ''; + + /** + * Account name. + * + * @var \string + * @since 1.0.0 + */ + protected $description = ''; + + /** + * Account name. + * + * @var \int + * @since 1.0.0 + */ + protected $members = []; + + /** + * Parents . + * + * @var \int[] + * @since 1.0.0 + */ + protected $parents = []; + + /** + * Permissions. + * + * @var \int[] + * @since 1.0.0 + */ + protected $permissions = []; + + /** + * Multition cache. + * + * @var \Model\Account[] + * @since 1.0.0 + */ + private static $instances = []; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * Multition constructor. + * + * @param \int $id Account id + * + * @return \phpOMS\Account\Group + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getInstance($id) + { + return self::$instances[$id] = self::$instances[$id] ?? new self(); + } + + /** + * Get account id. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getId() + { + return $this->id; + } + + /** + * Get account name. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getName() : \string + { + return $this->name; + } + + /** + * Get group description. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getDescription() : \string + { + return $this->description; + } + +} diff --git a/Account/NullAccount.php b/Account/NullAccount.php new file mode 100644 index 000000000..d389f42dc --- /dev/null +++ b/Account/NullAccount.php @@ -0,0 +1,31 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Account; + +/** + * Null account class. + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class NullAccount extends Account +{ +} diff --git a/ApplicationAbstract.php b/ApplicationAbstract.php new file mode 100644 index 000000000..5505e8c4d --- /dev/null +++ b/ApplicationAbstract.php @@ -0,0 +1,127 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS; + +/** + * Application class. + * + * @category Framework + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ApplicationAbstract +{ + + /** + * Database object. + * + * @var \phpOMS\DataStorage\Database\Pool + * @since 1.0.0 + */ + public $dbPool = null; + + /** + * Application settings object. + * + * @var \Model\CoreSettings + * @since 1.0.0 + */ + public $appSettings = null; + + /** + * Account manager instance. + * + * @var \phpOMS\Account\AccountManager + * @since 1.0.0 + */ + public $accountManager = null; + + /** + * Cache instance. + * + * @var \phpOMS\DataStorage\Cache\CacheManager + * @since 1.0.0 + */ + public $cacheManager = null; + + /** + * ModuleManager instance. + * + * @var \phpOMS\Module\ModuleManager + * @since 1.0.0 + */ + public $moduleManager = null; + + /** + * Router instance. + * + * @var \phpOMS\Router\Router + * @since 1.0.0 + */ + public $router = null; + + /** + * Dispatcher instance. + * + * @var \phpOMS\Dispatcher\Dispatcher + * @since 1.0.0 + */ + public $dispatcher = null; + + /** + * Session instance. + * + * @var \phpOMS\DataStorage\Session\SessionInterface + * @since 1.0.0 + */ + public $sessionManager = null; + + /** + * Server localization. + * + * @var \phpOMS\Localization\Localization + * @since 1.0.0 + */ + public $l11nServer = null; + + /** + * Server localization. + * + * @var \phpOMS\Log\FileLogger + * @since 1.0.0 + */ + public $logger = null; + + /** + * L11n manager. + * + * @var \phpOMS\Localization\L11nManager + * @since 1.0.0 + */ + public $l11nManager = null; + + /** + * Event manager. + * + * @var \phpOMS\Event\EventManager + * @since 1.0.0 + */ + public $eventManager = null; +} diff --git a/Asset/AssetManager.php b/Asset/AssetManager.php new file mode 100644 index 000000000..c0360830d --- /dev/null +++ b/Asset/AssetManager.php @@ -0,0 +1,119 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Asset; + +use phpOMS\DataStorage\Database\Pool; + +/** + * Asset manager class. + * + * Responsible for authenticating and initializing the connection + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class AssetManager +{ + + /** + * Assets. + * + * @var array + * @since 1.0.0 + */ + private $assets = []; + + /** + * Constructor. + * + * @param Pool $dbPool Database pool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(Pool $dbPool) + { + } + + /** + * Add asset. + * + * @param \string $id Asset id + * @param \string $asset Asset + * @param \bool $overwrite Overwrite + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(\string $id, \string $asset, \bool $overwrite = true) : \bool + { + if ($overwrite || !isset($this->assets[$id])) { + $this->assets[$id] = $asset; + + return true; + } + + return false; + } + + /** + * Remove asset. + * + * @param \string $id Asset id + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function remove(\string $id) : \bool + { + if (isset($this->assets[$id])) { + unset($this->assets[$id]); + + return true; + } + + return false; + } + + /** + * Get asset. + * + * @param \string $id Asset id + * + * @return mixed Asset + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get(\string $id) + { + if (isset($this->assets[$id])) { + return $this->assets[$id]; + } + + return null; + } + +} diff --git a/Asset/AssetType.php b/Asset/AssetType.php new file mode 100644 index 000000000..7574b16e9 --- /dev/null +++ b/Asset/AssetType.php @@ -0,0 +1,37 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Asset; + +use phpOMS\Datatypes\Enum; + +/** + * Login return types enum. + * + * These are possible answers to authentications. + * + * @category Framework + * @package phpOMS\Auth + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class AssetType extends Enum +{ + const CSS = 'css'; + const JS = 'js'; +} diff --git a/Auth/Auth.php b/Auth/Auth.php new file mode 100644 index 000000000..d3a5a737b --- /dev/null +++ b/Auth/Auth.php @@ -0,0 +1,165 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Auth; + +use phpOMS\Config\OptionsInterface; +use phpOMS\Config\OptionsTrait; +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; +use phpOMS\DataStorage\Database\DatabaseType; +use phpOMS\DataStorage\Session\SessionInterface; + +/** + * Auth class. + * + * Responsible for authenticating and initializing the connection + * + * @category Framework + * @package phpOMS\Auth + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Auth implements OptionsInterface +{ + use OptionsTrait; + + /** + * Session instance. + * + * @var SessionInterface + * @since 1.0.0 + */ + private $session = null; + + /** + * Database connection instance. + * + * @var ConnectionAbstract + * @since 1.0.0 + */ + private $connection = null; + + /** + * Constructor. + * + * @param ConnectionAbstract $connection Database connection + * @param SessionInterface $session Session + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(ConnectionAbstract $connection, SessionInterface $session) + { + $this->connection = $connection; + $this->session = $session; + } + + /** + * Authenticates user. + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function authenticate() : \bool + { + $uid = $this->session->get('UID'); + + if ($uid === null) { + $uid = false; + } + + return $uid; + } + + /** + * Login user. + * + * @param \string $login Username + * @param \string $password Password + * + * @return \int Login code + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function login(\string $login, \string $password) : \int + { + try { + $result = null; + + switch ($this->connection->getType()) { + case DatabaseType::MYSQL: + + $sth = $this->connection->con->prepare( + 'SELECT + `' . $this->connection->prefix . 'account`.* + FROM + `' . $this->connection->prefix . 'account` + WHERE + `account_login` = :login' + ); + $sth->bindValue(':login', $login, \PDO::PARAM_STR); + $sth->execute(); + + $result = $sth->fetchAll(); +break; + } + + // TODO: check if user is allowed to login on THIS page (backend|frontend|etc...) + + if (!isset($result[0])) { + return LoginReturnType::WRONG_USERNAME; + } + + $result = $result[0]; + + if ($result['account_tries'] <= 0) { + return LoginReturnType::WRONG_INPUT_EXCEEDED; + } + + if (password_verify($password, $result['account_password'])) { + $this->session->set('UID', $result['account_id']); + $this->session->save(); + + return LoginReturnType::OK; + } + + return LoginReturnType::WRONG_PASSWORD; + } catch (\Exception $e) { + return LoginReturnType::FAILURE; + } + } + + /** + * Logout the given user. + * + * @param \int $uid User ID + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function logout(\int $uid) + { + // TODO: logout other users? If admin wants to kick a user for updates etc. + $this->session->remove('UID'); + } +} diff --git a/Auth/LoginReturnType.php b/Auth/LoginReturnType.php new file mode 100644 index 000000000..5a28855be --- /dev/null +++ b/Auth/LoginReturnType.php @@ -0,0 +1,45 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Auth; + +use phpOMS\Datatypes\Enum; + +/** + * Login return types enum. + * + * These are possible answers to authentications. + * + * @category Framework + * @package phpOMS\Auth + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class LoginReturnType extends Enum +{ + const OK = 0; /* Everything is ok and the user got authed */ + const FAILURE = 1; /* Authentication resulted in a unexpected failure */ + const WRONG_PASSWORD = 2; /* Authentication with wrong password */ + const WRONG_USERNAME = 3; /* Authentication with unknown user */ + const WRONG_PERMISSION = 4; /* User doesn't have permission to authenticate */ + const NOT_ACTIVATED = 5; /* The user is not activated yet */ + const WRONG_INPUT_EXCEEDED = 6; /* Too many wrong logins recently */ + const TIMEOUTED = 7; /* User received a timeout and can not log in until a certain date */ + const BANNED = 8; /* User is banned */ + const INACTIVE = 9; /* User is inactive */ +} diff --git a/Autoloader.php b/Autoloader.php new file mode 100644 index 000000000..ac7cd1088 --- /dev/null +++ b/Autoloader.php @@ -0,0 +1,75 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS; + +spl_autoload_register('\phpOMS\Autoloader::default_autoloader'); + +/** + * Autoloader class. + * + * @category Framework + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Autoloader +{ + + /** + * Loading classes by namespace + class name. + * + * @param \string $class Class path + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function default_autoloader(\string $class) + { + if (($class = self::exists($class)) !== false) { + /** @noinspection PhpIncludeInspection */ + include __DIR__ . '/../' . $class . '.php'; + } + } + + /** + * Check if class exists. + * + * @param \string $class Class path + * + * @return false|string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function exists(\string $class) + { + $class = ltrim($class, '\\'); + $class = str_replace(['_', '\\'], DIRECTORY_SEPARATOR, $class); + + if (file_exists(__DIR__ . '/../' . $class . '.php')) { + return $class; + } + + return false; + } + +} diff --git a/Config/OptionsInterface.php b/Config/OptionsInterface.php new file mode 100644 index 000000000..c2bfdb123 --- /dev/null +++ b/Config/OptionsInterface.php @@ -0,0 +1,83 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Config; + +/** + * Options class. + * + * @category Framework + * @package phpOMS\Config + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface OptionsInterface +{ + + /** + * Is this key set. + * + * @param mixed $key Key to check for existence + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function exists($key); + + /** + * Updating or adding settings. + * + * @param mixed $key Unique option key + * @param mixed $value Option value + * @param \bool $overwrite Overwrite existing value + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setOption($key, $value, \bool $overwrite = true) : \bool; + + /** + * Updating or adding settings. + * + * @param array $pair Key value pair + * @param \bool $overwrite Overwrite existing value + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setOptions(array $pair, \bool $overwrite = true) : \bool; + + /** + * Get option by key. + * + * @param mixed $key Unique option key + * + * @return mixed Option value + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getOption($key); + +} diff --git a/Config/OptionsTrait.php b/Config/OptionsTrait.php new file mode 100644 index 000000000..8f19bbf77 --- /dev/null +++ b/Config/OptionsTrait.php @@ -0,0 +1,80 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Config; + +/** + * Options trait. + * + * @category Framework + * @package phpOMS\Config + * @since 1.0.0 + */ +trait OptionsTrait +{ + + /** + * Options. + * + * @var array + * @since 1.0.0 + */ + private $options = []; + + /** + * {@inheritdoc} + */ + public function exists($key) + { + return array_key_exists($key, $this->options); + } + + /** + * {@inheritdoc} + */ + public function getOption($key) + { + return $this->options[$key] ?? null; + } + + /** + * {@inheritdoc} + */ + public function setOption($key, $value, \bool $overwrite = true) : \bool + { + if ($overwrite || !array_key_exists($key, $this->options)) { + $this->options[$key] = [$value, $overwrite]; + + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function setOptions(array $pair, \bool $overwrite = true) : \bool + { + if ($overwrite) { + $this->options += $pair; + + return true; + } + + return false; + } + +} diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php new file mode 100644 index 000000000..7e4274e22 --- /dev/null +++ b/Config/SettingsAbstract.php @@ -0,0 +1,134 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Config; + + +use phpOMS\DataStorage\Database\DatabaseType; +use phpOMS\DataStorage\Database\Query\Builder; + +/** + * Settings class. + * + * Responsible for providing a database/cache bound settings manger + * + * @category Framework + * @package phpOMS\Config + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class SettingsAbstract implements OptionsInterface +{ + use OptionsTrait; + + /** + * Cache manager (pool). + * + * @var \phpOMS\DataStorage\Cache\CacheManager + * @since 1.0.0 + */ + protected $cache = null; + + /** + * Database connection instance. + * + * @var \phpOMS\DataStorage\Database\Connection\ConnectionAbstract + * @since 1.0.0 + */ + protected $connection = null; + + /** + * Settings table. + * + * @var \string + * @since 1.0.0 + */ + protected static $table = null; + + /** + * Columns to identify the value. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $columns = [ + 'id', + ]; + + /** + * Field where the actual value is stored. + * + * @var \string + * @since 1.0.0 + */ + protected $valueField = 'option'; + + /** + * Get option by key. + * + * @param \string[] $columns Column values for filtering + * + * @return mixed Option value + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get(array $columns) + { + $options = false; + + switch ($this->connection->getType()) { + case DatabaseType::MYSQL: + $query = new Builder($this->connection); + $sql = $query->select(static::$columns[0], 'settings_content') + ->from($this->connection->prefix . static::$table) + ->where(static::$columns[0], 'in', $columns) + ->toSql(); + + $sth = $this->connection->con->prepare($sql); + $sth->execute(); + + $options = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); + $this->setOptions($options); + break; + } + + return $options; + } + + /** + * Set option by key. + * + * @param \string[] $options Column values for filtering + * @param \bool $overwrite Overwrite existing settings + * @param \bool $store Save this Setting immediately to database + * + * @return mixed Option value + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(array $options, \bool $overwrite = true, \bool $store = false) + { + $this->setOptions($options, $overwrite); + + if($store) { + // save to db + } + } +} diff --git a/Console/CommandManager.php b/Console/CommandManager.php new file mode 100644 index 000000000..459bd3e62 --- /dev/null +++ b/Console/CommandManager.php @@ -0,0 +1,137 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Console; + +/** + * CommandManager class. + * + * @category Framework + * @package phpOMS\Socket + * @since 1.0.0 + * + * @todo : Hey, this looks like a copy of an event manager! + */ +class CommandManager implements \Countable +{ + + /** + * Commands. + * + * @var mixed[] + * @since 1.0.0 + */ + private $commands = []; + + /** + * Commands. + * + * @var \int + * @since 1.0.0 + */ + private $count = 0; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * Attach new command. + * + * @param \string $cmd Command ID + * @param mixed $callback Function callback + * @param mixed $source Provider + * @param \bool $overwrite Overwrite existing + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function attach(\string $cmd, $callback, $source, \bool $overwrite = true) : \bool + { + if ($overwrite || !isset($this->commands[$cmd])) { + $this->commands[$cmd] = [$callback, $source]; + $this->count++; + + return true; + } + + return false; + } + + /** + * Detach existing command. + * + * @param \string $cmd Command ID + * @param mixed $source Provider + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function detach(\string $cmd, $source) : \bool + { + if (array_key_exists($cmd, $this->commands)) { + unset($this->commands[$cmd]); + $this->count--; + + return true; + } + + return false; + } + + /** + * Trigger command. + * + * @param \string $cmd Command ID + * @param mixed $para Parameters to pass + * + * @return mixed|bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function trigger(\string $cmd, $para) + { + if (array_key_exists($cmd, $this->commands)) { + return $this->commands[$cmd][0]($para); + } + + return false; + } + + /** + * Count commands. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function count() : \int + { + return $this->count; + } + +} diff --git a/Contract/ArrayableInterface.php b/Contract/ArrayableInterface.php new file mode 100644 index 000000000..eac648ca8 --- /dev/null +++ b/Contract/ArrayableInterface.php @@ -0,0 +1,44 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Contract; + +/** + * Defines an object arrayable. + * + * This stands always in combination with a jsonable instance. + * + * @category Framework + * @package phpOMS\Contract + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface ArrayableInterface +{ + + /** + * Get the instance as an array. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function toArray() : array; + +} diff --git a/Contract/JsonableInterface.php b/Contract/JsonableInterface.php new file mode 100644 index 000000000..503121936 --- /dev/null +++ b/Contract/JsonableInterface.php @@ -0,0 +1,46 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Contract; + +/** + * This is contract expects a class to be serializable via json. + * + * This is used in order to distinguish between serialize and json_encode + * + * @category Framework + * @package phpOMS\Contract + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface JsonableInterface extends ArrayableInterface +{ + + /** + * Convert the object to its JSON representation. + * + * @param \int $options + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function toJson(\int $options = 0) : \string; + +} diff --git a/Contract/Object.php b/Contract/Object.php new file mode 100644 index 000000000..aafdf1991 --- /dev/null +++ b/Contract/Object.php @@ -0,0 +1,31 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Contract; + +/** + * Degenerate interface for type hinting object. + * + * @category Framework + * @package phpOMS\Contract + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface Object +{ +} diff --git a/Contract/RenderableInterface.php b/Contract/RenderableInterface.php new file mode 100644 index 000000000..04227d310 --- /dev/null +++ b/Contract/RenderableInterface.php @@ -0,0 +1,45 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Contract; + +/** + * Make a class renderable. + * + * This is primarily used for classes that provide formatted output or output, + * that get's rendered in third party applications. + * + * @category Framework + * @package phpOMS\Contract + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface RenderableInterface +{ + + /** + * Get the evaluated contents of the object. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function render() : \string; + +} diff --git a/DataStorage/Cache/CacheInterface.php b/DataStorage/Cache/CacheInterface.php new file mode 100644 index 000000000..10189f9e1 --- /dev/null +++ b/DataStorage/Cache/CacheInterface.php @@ -0,0 +1,135 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +/** + * Cache interface. + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface CacheInterface +{ + + /** + * Updating or adding cache data. + * + * @param mixed $key Unique cache key + * @param mixed $value Cache value + * @param CacheStatus $type Cache type + * @param \int $expire Valid duration (in s) + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000); + + /** + * Adding new data if it doesn't exist. + * + * @param mixed $key Unique cache key + * @param mixed $value Cache value + * @param CacheStatus $type Cache type + * @param \int $expire Valid duration (in s) + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) : \bool; + + /** + * Get cache by key. + * + * @param mixed $key Unique cache key + * @param CacheStatus $type Cache status/type + * + * @return mixed Cache value + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get($key, CacheStatus $type = null); + + /** + * Remove value by key. + * + * @param mixed $key Unique cache key + * @param CacheStatus $type Cache status/type + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function delete($key, CacheStatus $type = null) : \bool; + + /** + * Removing all elements from cache (invalidate cache). + * + * @param CacheStatus $type Cache status/type + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function flush(CacheStatus $type = null); + + /** + * Updating existing value/key. + * + * @param mixed $key Unique cache key + * @param mixed $value Cache value + * @param CacheType $type Cache type + * @param \int $expire Timestamp + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) : \bool; + + /** + * Requesting cache stats. + * + * @return mixed[] Stats array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function stats() : array; + + /** + * Get the threshold required to cache data using this cache. + * + * @return \int Storage threshold + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getThreshold() : \int; + +} diff --git a/DataStorage/Cache/CacheManager.php b/DataStorage/Cache/CacheManager.php new file mode 100644 index 000000000..d72f7677d --- /dev/null +++ b/DataStorage/Cache/CacheManager.php @@ -0,0 +1,248 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +use phpOMS\Config\OptionsInterface; +use phpOMS\Config\OptionsTrait; + + +use phpOMS\DataStorage\Database\Pool; + +/** + * Cache class. + * + * Responsible for caching scalar data types and arrays. + * Caching HTML output and objects coming soon/is planned. + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class CacheManager implements OptionsInterface +{ + use OptionsTrait; + + /** + * MemCache instance. + * + * @var \phpOMS\DataStorage\Cache\MemCache + * @since 1.0.0 + */ + private $memc = null; + + /** + * RedisCache instance. + * + * @var \phpOMS\DataStorage\Cache\RedisCache + * @since 1.0.0 + */ + private $redisc = null; + + /** + * RedisCache instance. + * + * @var \phpOMS\DataStorage\Cache\WinCache + * @since 1.0.0 + */ + private $winc = null; + + /** + * FileCache instance. + * + * @var \phpOMS\DataStorage\Cache\FileCache + * @since 1.0.0 + */ + private $filec = null; + + /** + * FileCache instance. + * + * @var Pool + * @since 1.0.0 + */ + private $dbPool = null; + + /** + * Constructor. + * + * @param Pool $dbPool Database pool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(Pool $dbPool) + { + $this->dbPool = $dbPool; + } + + /** + * Init cache. + * + * @param mixed $options Options used to initialize the different caching types + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function init($options = null) + { + if ($options === null) { + /* This is costing me 1ms, maybe init settings first cause i'm making another settings call later on -> same call 2 times */ + $sth = $this->dbPool->get('core')->con->prepare('SELECT `content` FROM `' . $this->dbPool->get('core')->prefix . 'settings` WHERE `id` = 1000000015'); + $sth->execute(); + $cache_data = $sth->fetchAll(); + + $this->setOption('cache:type', (int) $cache_data[0][0]); + } else { + $this->options = $options; + } + } + + /** + * {@inheritdoc} + */ + public function update() + { + } + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + $this->getInstance($type)->set($key, $value, $type = null, $expire); + } + + /** + * Requesting caching instance. + * + * @param CacheStatus $type Cache to request + * + * @return \phpOMS\DataStorage\Cache\MemCache|\phpOMS\DataStorage\Cache\FileCache|null + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getInstance(CacheStatus $type = null) + { + if (($type === null || $type === CacheStatus::MEMCACHE) && $this->memc !== null) { + return $this->memc; + } + + if (($type === null || $type === CacheStatus::REDISCACHE) && $this->redisc !== null) { + return $this->redisc; + } + + if (($type === null || $type === CacheStatus::WINCACHE) && $this->winc !== null) { + return $this->winc; + } + + if (($type === null || $type === CacheStatus::FILECACHE) && $this->filec !== null) { + return $this->filec; + } + + return new NullCache(); + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + $this->getInstance($type)->add($key, $value, $type = null, $expire); + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + return $this->getInstance($type)->get($key); + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + $this->getInstance($type)->delete($key); + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + if ($type === null) { + $this->filec->flush(); + $this->memc->flush(); + } elseif ($type === CacheStatus::MEMCACHE) { + $this->memc->flush(); + } elseif ($type === CacheStatus::FILECACHE) { + $this->filec->flush(); + } + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null) + { + $this->getInstance($type)->replace($key, $value); + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + $stats = []; + + if ($this->memc !== null) { + $stats['memc'] = $this->memc->stats(); + } + + if ($this->filec !== null) { + $stats['filec'] = $this->filec->stats(); + } + + return $stats; + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : array + { + $threshold = []; + + if ($this->memc !== null) { + $threshold['memc'] = $this->memc->getThreshold(); + } + + if ($this->filec !== null) { + $threshold['filec'] = $this->filec->getThreshold(); + } + + return $threshold; + } + +} diff --git a/DataStorage/Cache/CacheStatus.php b/DataStorage/Cache/CacheStatus.php new file mode 100644 index 000000000..b7fc47e34 --- /dev/null +++ b/DataStorage/Cache/CacheStatus.php @@ -0,0 +1,41 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +use phpOMS\Datatypes\Enum; + +/** + * Cache status enum. + * + * Possible caching status + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class CacheStatus extends Enum +{ + const INACTIVE = 0; /* Caching is disabled */ + const ERROR = 1; /* Caching failed */ + const MEMCACHE = 2; /* Caching OK */ + const FILECACHE = 3; /* Caching OK */ + const REDISCACHE = 4; /* Caching OK */ + const WINCACHE = 5; /* Caching OK */ +} diff --git a/DataStorage/Cache/CacheType.php b/DataStorage/Cache/CacheType.php new file mode 100644 index 000000000..8fec57649 --- /dev/null +++ b/DataStorage/Cache/CacheType.php @@ -0,0 +1,40 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +use phpOMS\Datatypes\Enum; + +/** + * Cache type enum. + * + * Possible caching types + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class CacheType extends Enum +{ + const _NUMERIC = 0; /* Data is numeric */ + const _STRING = 1; /* Data is string */ + const _ARRAY = 2; /* Data is array */ + const _OBJECT = 3; /* Data is object */ + const _HEX = 4; /* Data is object */ +} diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php new file mode 100644 index 000000000..d9f922589 --- /dev/null +++ b/DataStorage/Cache/FileCache.php @@ -0,0 +1,116 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +use phpOMS\System\FileSystem; + +/** + * MemCache class. + * + * PHP Version 5.6 + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class FileCache implements CacheInterface +{ + + /** + * Cache path. + * + * @var \string + * @since 1.0.0 + */ + const CACHE_PATH = __DIR__ . '/../../../Cache'; + + /** + * Only cache if data is larger than threshold (0-100). + * + * @var \int + * @since 1.0.0 + */ + private $threshold = 50; + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + array_map('unlink', glob(self::CACHE_PATH . '/*')); + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) + { + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + $stats = []; + $stats['count'] = FileSystem::getFileCount(self::CACHE_PATH); + + // size, avg. last change compared to now + + return $stats; + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : \int + { + return $this->threshold; + } + +} diff --git a/DataStorage/Cache/MemCache.php b/DataStorage/Cache/MemCache.php new file mode 100644 index 000000000..1647211b0 --- /dev/null +++ b/DataStorage/Cache/MemCache.php @@ -0,0 +1,163 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +/** + * Memcache class. + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class MemCache implements CacheInterface +{ + + /** + * Memcache instance. + * + * @var \Memcache + * @since 1.0.0 + */ + private $memc = null; + + /** + * Only cache if data is larger than threshold (0-100). + * + * @var \int + * @since 1.0.0 + */ + private $threshold = 10; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + $this->memc = new self(); + } + + /** + * Adding server to server pool. + * + * @param mixed $data Server data array + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addServer($data) + { + $this->memc->addServer($data['host'], $data['port'], $data['timeout']); + } + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + $this->memc->set($key, $value, false, $expire); + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + return $this->memc->add($key, $value, false, $expire); + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + return $this->memc->get($key); + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + $this->memc->delete($key); + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + $this->memc->flush(); + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) + { + $this->memc->replace($key, $value, false, $expire); + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + return $this->memc->getExtendedStats(); + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : \int + { + return $this->threshold; + } + + /** + * Destructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __destruct() + { + $this->close(); + } + + /** + * Closing cache. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function close() + { + if ($this->memc !== null) { + $this->memc->close(); + $this->memc = null; + } + } + +} diff --git a/DataStorage/Cache/NullCache.php b/DataStorage/Cache/NullCache.php new file mode 100644 index 000000000..7581a43d3 --- /dev/null +++ b/DataStorage/Cache/NullCache.php @@ -0,0 +1,96 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +/** + * Null cache class. + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class NullCache implements CacheInterface +{ + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement set() method. + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement add() method. + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + // TODO: Implement get() method. + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + // TODO: Implement delete() method. + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + // TODO: Implement flush() method. + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) + { + // TODO: Implement replace() method. + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + // TODO: Implement stats() method. + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : \int + { + // TODO: Implement getThreshold() method. + } + +} diff --git a/DataStorage/Cache/RedisCache.php b/DataStorage/Cache/RedisCache.php new file mode 100644 index 000000000..044e906ef --- /dev/null +++ b/DataStorage/Cache/RedisCache.php @@ -0,0 +1,98 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +/** + * RedisCache class. + * + * PHP Version 5.6 + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class RedisCache implements CacheInterface +{ + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement set() method. + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement add() method. + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + // TODO: Implement get() method. + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + // TODO: Implement delete() method. + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + // TODO: Implement flush() method. + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) + { + // TODO: Implement replace() method. + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + // TODO: Implement stats() method. + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : \int + { + // TODO: Implement getThreshold() method. + } + +} diff --git a/DataStorage/Cache/WinCache.php b/DataStorage/Cache/WinCache.php new file mode 100644 index 000000000..daf6c3572 --- /dev/null +++ b/DataStorage/Cache/WinCache.php @@ -0,0 +1,98 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Cache; + +/** + * WinCache class. + * + * PHP Version 5.6 + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class WinCache implements CacheInterface +{ + + /** + * {@inheritdoc} + */ + public function set($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement set() method. + } + + /** + * {@inheritdoc} + */ + public function add($key, $value, CacheStatus $type = null, \int $expire = 2592000) + { + // TODO: Implement add() method. + } + + /** + * {@inheritdoc} + */ + public function get($key, CacheStatus $type = null) + { + // TODO: Implement get() method. + } + + /** + * {@inheritdoc} + */ + public function delete($key, CacheStatus $type = null) + { + // TODO: Implement delete() method. + } + + /** + * {@inheritdoc} + */ + public function flush(CacheStatus $type = null) + { + // TODO: Implement flush() method. + } + + /** + * {@inheritdoc} + */ + public function replace($key, $value, CacheType $type = null, \int $expire = -1) + { + // TODO: Implement replace() method. + } + + /** + * {@inheritdoc} + */ + public function stats() : array + { + // TODO: Implement stats() method. + } + + /** + * {@inheritdoc} + */ + public function getThreshold() : \int + { + // TODO: Implement getThreshold() method. + } + +} diff --git a/DataStorage/Cookie/CookieJar.php b/DataStorage/Cookie/CookieJar.php new file mode 100644 index 000000000..e4a156e69 --- /dev/null +++ b/DataStorage/Cookie/CookieJar.php @@ -0,0 +1,29 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +/* +NOT IN USE +Will be implemented later +*/ +/* TODO: implement */ +namespace phpOMS\Cookie; + +/** + * @since 1.0.0 + * @author Dennis Eichhorn + */ +class CookieJar +{ +} diff --git a/DataStorage/DataMapperInterface.php b/DataStorage/DataMapperInterface.php new file mode 100644 index 000000000..6de07d072 --- /dev/null +++ b/DataStorage/DataMapperInterface.php @@ -0,0 +1,138 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage; + +use phpOMS\DataStorage\Database\Query\Builder; + +/** + * Datamapper interface. + * + * DB, Cache, Session + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface DataMapperInterface +{ + + /** + * Update data. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function update(); + + /** + * Save data. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function save(); + + /** + * Delete data. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function delete(); + + /** + * Find data. + * + * @param array $columns Columns + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function find(...$columns) : Builder; + + /** + * List data. + * + * @param Builder $query Query + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function listResults(Builder $query); + + /** + * Populate data. + * + * @param array $result Result set + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function populate(array $result); + + /** + * Populate data. + * + * @param array $result Result set + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function populateIterable(array $result) : array; + + /** + * Load. + * + * @param array $objects Objects to load + * + * @return $this + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function with(...$objects); + + /** + * Get object. + * + * @param mixed $primaryKey Key + * + * @return self + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get($primaryKey); + +} diff --git a/DataStorage/Database/Connection/ConnectionAbstract.php b/DataStorage/Database/Connection/ConnectionAbstract.php new file mode 100644 index 000000000..e2717476e --- /dev/null +++ b/DataStorage/Database/Connection/ConnectionAbstract.php @@ -0,0 +1,153 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Connection; + +use phpOMS\DataStorage\Database\DatabaseStatus; +use phpOMS\DataStorage\Database\Query\Grammar\Grammar; + +/** + * Database handler. + * + * Handles the database connection. + * Implementing wrapper functions for multiple databases is planned (far away). + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class ConnectionAbstract implements ConnectionInterface +{ + + /** + * Connection object. + * + * This can be used externally to define queries and execute them. + * + * @var \PDO + * @since 1.0.0 + */ + public $con = null; + + /** + * Database prefix. + * + * The database prefix name for unique table names + * + * @var \string + * @since 1.0.0 + */ + public $prefix = ''; + + /** + * Database data. + * + * @var \string[] + * @since 1.0.0 + */ + protected $dbdata = null; + + /** + * Database type. + * + * @var \phpOMS\DataStorage\Database\DatabaseType + * @since 1.0.0 + */ + protected $type = null; + + /** + * Database status. + * + * @var DatabaseStatus + * @since 1.0.0 + */ + protected $status = DatabaseStatus::CLOSED; + + /** + * Database grammar. + * + * @var Grammar + * @since 1.0.0 + */ + protected $grammar = null; + + /** + * {@inheritdoc} + */ + public function getType() : \int + { + return $this->type; + } + + /** + * {@inheritdoc} + */ + public function getStatus() : \int + { + return $this->status; + } + + /** + * Get table prefix. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getPrefix() : \string + { + return $this->prefix; + } + + /** + * {@inheritdoc} + */ + public function getGrammar() : Grammar + { + if (!isset($this->grammar)) { + $this->grammar = new Grammar(); + } + + return $this->grammar; + } + + /** + * Object destructor. + * + * Sets the database connection to null + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __destruct() + { + $this->close(); + } + + /** + * {@inheritdoc} + */ + public function close() + { + $this->con = null; + $this->status = DatabaseStatus::CLOSED; + } + +} diff --git a/DataStorage/Database/Connection/ConnectionFactory.php b/DataStorage/Database/Connection/ConnectionFactory.php new file mode 100644 index 000000000..50c40a5bb --- /dev/null +++ b/DataStorage/Database/Connection/ConnectionFactory.php @@ -0,0 +1,67 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Connection; + + +/** + * Database connection factory. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ConnectionFactory +{ + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function __construct() + { + } + + /** + * Create database connection. + * + * Overwrites current connection if existing + * + * @param \string[] $dbdata the basic database information for establishing a connection + * + * @return ConnectionInterface + * + * @throws \Exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function create(array $dbdata) : ConnectionInterface + { + switch ($dbdata['db']) { + case 'mysql': + return new MysqlConnection($dbdata); + break; + default: + throw new \InvalidArgumentException('Database "' . $dbdata['db'] . '" is not supported.'); + } + } +} diff --git a/DataStorage/Database/Connection/ConnectionInterface.php b/DataStorage/Database/Connection/ConnectionInterface.php new file mode 100644 index 000000000..c7fad96d3 --- /dev/null +++ b/DataStorage/Database/Connection/ConnectionInterface.php @@ -0,0 +1,88 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Connection; + +use phpOMS\DataStorage\Database\Query\Grammar\Grammar; + +/** + * Database connection interface. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface ConnectionInterface +{ + + /** + * Connect to database. + * + * Overwrites current connection if existing + * + * @param \string[] $dbdata the basic database information for establishing a connection + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function connect(array $dbdata); + + /** + * Get the database type. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getType() : \int; + + /** + * Get the database status. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getStatus() : \int; + + /** + * Close database connection. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function close(); + + /** + * Return grammar for this connection. + * + * @return Grammar + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getGrammar() : Grammar; + +} diff --git a/DataStorage/Database/Connection/MysqlConnection.php b/DataStorage/Database/Connection/MysqlConnection.php new file mode 100644 index 000000000..05896f05d --- /dev/null +++ b/DataStorage/Database/Connection/MysqlConnection.php @@ -0,0 +1,78 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Connection; + +use phpOMS\DataStorage\Database\DatabaseStatus; +use phpOMS\DataStorage\Database\DatabaseType; +use phpOMS\DataStorage\Database\Query\Grammar\MysqlGrammar; + +/** + * Database handler. + * + * Handles the database connection. + * Implementing wrapper functions for multiple databases is planned (far away). + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class MysqlConnection extends ConnectionAbstract +{ + + /** + * Object constructor. + * + * Creates the database object and overwrites all default values. + * + * @param \string[] $dbdata the basic database information for establishing a connection + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(array $dbdata) + { + $this->type = DatabaseType::MYSQL; + $this->grammar = new MysqlGrammar(); + $this->connect($dbdata); + } + + /** + * {@inheritdoc} + */ + public function connect(array $dbdata = null) + { + $this->close(); + + $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->prefix = $dbdata['prefix']; + + try { + $this->con = new \PDO($this->dbdata['db'] . ':host=' . $this->dbdata['host'] . ';dbname=' . $this->dbdata['database'] . ';charset=utf8', $this->dbdata['login'], $this->dbdata['password']); + $this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + $this->status = DatabaseStatus::OK; + } catch (\PDOException $e) { + $this->status = DatabaseStatus::MISSING_DATABASE; + $this->con = null; + } + } + +} diff --git a/DataStorage/Database/Connection/PostgresConnection.php b/DataStorage/Database/Connection/PostgresConnection.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Connection/SQLiteConnection.php b/DataStorage/Database/Connection/SQLiteConnection.php new file mode 100644 index 000000000..3790f3384 --- /dev/null +++ b/DataStorage/Database/Connection/SQLiteConnection.php @@ -0,0 +1,78 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Connection; + +use phpOMS\DataStorage\Database\DatabaseStatus; +use phpOMS\DataStorage\Database\DatabaseType; +use phpOMS\DataStorage\Database\Query\Grammar\SqliteGrammar; + +/** + * Database handler. + * + * Handles the database connection. + * Implementing wrapper functions for multiple databases is planned (far away). + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class SqliteConnection extends ConnectionAbstract +{ + + /** + * Object constructor. + * + * Creates the database object and overwrites all default values. + * + * @param \string[] $dbdata the basic database information for establishing a connection + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(array $dbdata) + { + $this->type = DatabaseType::MYSQL; + $this->grammar = new SqliteGrammar(); + $this->connect($dbdata); + } + + /** + * {@inheritdoc} + */ + public function connect(array $dbdata = null) + { + $this->close(); + + $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->prefix = $dbdata['prefix']; + + try { + $this->con = new \PDO($this->dbdata['db'] . ':' . $this->dbdata['path']); + $this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + $this->status = DatabaseStatus::OK; + } catch (\PDOException $e) { + $this->status = DatabaseStatus::MISSING_DATABASE; + $this->con = null; + } + } + +} diff --git a/DataStorage/Database/Connection/SqlServerConnection.php b/DataStorage/Database/Connection/SqlServerConnection.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Connector/MysqlConnector.php b/DataStorage/Database/Connector/MysqlConnector.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Connector/PostgresConnector.php b/DataStorage/Database/Connector/PostgresConnector.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Connector/SQLiteConnector.php b/DataStorage/Database/Connector/SQLiteConnector.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Connector/SqlServerConnector.php b/DataStorage/Database/Connector/SqlServerConnector.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php new file mode 100644 index 000000000..b96f2ea30 --- /dev/null +++ b/DataStorage/Database/DataMapperAbstract.php @@ -0,0 +1,600 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database; + +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; +use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\DataMapperInterface; + +/** + * Datamapper for databases. + * + * DB, Cache, Session + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class DataMapperAbstract implements DataMapperInterface +{ + + /** + * Database connection. + * + * @var \phpOMS\DataStorage\Database\Connection\ConnectionAbstract + * @since 1.0.0 + */ + protected $db = null; + + /** + * Overwriting extended values. + * + * @var \bool + * @since 1.0.0 + */ + protected static $overwrite = true; + + /** + * Primary field name. + * + * @var \string + * @since 1.0.0 + */ + protected static $primaryField = ''; + + /** + * Primary field name. + * + * @var \string + * @since 1.0.0 + */ + protected static $createdAt = ''; + + /** + * Columns. + * + * @var array + * @since 1.0.0 + */ + protected static $columns = []; + + /** + * Relations. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $hasMany = []; + + /** + * Relations. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $ownsMany = []; + + /** + * Relations. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $hasOne = []; + + /** + * Extending relations. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $extends = []; + + /** + * Relations. + * + * @var \string[] + * @since 1.0.0 + */ + protected static $ownsOne = []; + + /** + * Table. + * + * @var \string + * @since 1.0.0 + */ + protected static $table = ''; + + /** + * Extended value collection. + * + * @var array + * @since 1.0.0 + */ + protected $collection = [ + 'primaryField' => [], + 'createdAt' => [], + 'columns' => [], + 'hasMany' => [], + 'hasOne' => [], + 'ownsMany' => [], + 'ownsOne' => [], + 'table' => [], + ]; + + /** + * Constructor. + * + * @param ConnectionAbstract $con Database connection + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(ConnectionAbstract $con) + { + $this->db = $con; + $this->extend($this); + } + + /** + * Collect values from extension. + * + * @param mixed $class Current extended mapper + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function extend($class) + { + $this->collection['primaryField'][] = $class::$primaryField; + $this->collection['createdAt'][] = $class::$createdAt; + $this->collection['columns'][] = $class::$columns; + $this->collection['hasMany'][] = $class::$hasMany; + $this->collection['hasOne'][] = $class::$hasOne; + $this->collection['ownsMany'][] = $class::$ownsMany; + $this->collection['ownsOne'][] = $class::$ownsOne; + $this->collection['table'][] = $class::$table; + + if (($parent = get_parent_class($class)) !== false && !$class::$overwrite) { + $this->extend($parent); + } + } + + /** + * Update data. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function update() + { + } + + /** + * Save data. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function save() + { + } + + /** + * Delete data. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function delete() + { + } + + /** + * Find data. + * + * @param array $columns Columns + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function find(...$columns) : Builder + { + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()); + + return $query->select(...$columns)->from(static::$table); + } + + /** + * Create object in db. + * + * @param mixed $obj Object reference (gets filled with insert id) + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function create(&$obj) + { + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->into(static::$table); + + $reflectionClass = new \ReflectionClass(get_class($obj)); + $properties = $reflectionClass->getProperties(); + + foreach ($properties as $property) { + foreach (static::$columns as $key => $column) { + if ($column['internal'] === $property) { + $value = $property->getValue($obj); + + if ($column['type'] === 'DateTime') { + $value = isset($value) ? $value->format('Y-m-d H:i:s') : null; + } + + $query->insert($column['name']) + ->values($value); + + break; + } + } + } + + $this->db->con->prepare($query->toSql())->execute(); + $objId = $this->db->con->lastInsertId(); + + $reflectionProperty = $reflectionClass->getProperty('id'); + + if (!($accessible = $reflectionProperty->isPublic())) { + $reflectionProperty->setAccessible(true); + } + + settype($objId, static::$columns[static::$primaryField]['type']); + $reflectionProperty->setValue($obj, $objId); + + if (!$accessible) { + $reflectionProperty->setAccessible(false); + } + + return $objId; + } + + /** + * Find data. + * + * @param Builder $query Query + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function listResults(Builder $query) + { + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + + return $this->populateIterable($sth->fetchAll(\PDO::FETCH_ASSOC)); + } + + /** + * Populate data. + * + * @param array $result Result set + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function populate(array $result) + { + $class = get_class($this); + $class = str_replace('Mapper', '', $class); + $obj = new $class(); + + return $this->populateAbstract($result, $obj); + } + + /** + * Populate data. + * + * @param array $result Query result set + * @param mixed $obj Object to populate + * + * @return mixed + * + * @throws \Exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function populateAbstract(array $result, $obj) + { + $reflectionClass = new \ReflectionClass(get_class($obj)); + + foreach ($result as $column => $value) { + if ($reflectionClass->hasProperty(static::$columns[$column]['internal'])) { + $reflectionProperty = $reflectionClass->getProperty(static::$columns[$column]['internal']); + + if (!($accessible = $reflectionProperty->isPublic())) { + $reflectionProperty->setAccessible(true); + } + + if (in_array(static::$columns[$column]['type'], ['string', 'int', 'float'])) { + settype($value, static::$columns[$column]['type']); + $reflectionProperty->setValue($obj, $value); + } elseif (static::$columns[$column]['type'] === 'DateTime') { + $reflectionProperty->setValue($obj, new \DateTime($value)); + } else { + throw new \UnexpectedValueException('Value "' . static::$columns[$column]['type'] . '" is not supported.'); + } + + if (!$accessible) { + $reflectionProperty->setAccessible(false); + } + } + } + + return $obj; + } + + /** + * Get newest. + * + * This will fall back to the insert id if no datetime column is present. + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getNewest() + { + if (!isset(static::$createdAt) || !isset(static::$columns[static::$createdAt])) { + throw new \BadMethodCallException('Method "' . __METHOD__ . '" is not supported.'); + } + + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->select('*') + ->from(static::$table) + ->limit(1); + + if(isset(static::$createdAt)) { + $query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC'); + } else { + $query->orderBy(static::$table . '.' . static::$columns[static::$primaryField]['name'], 'DESC'); + } + + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + + $results = $sth->fetch(\PDO::FETCH_ASSOC); + + return $this->populate(is_bool($results) ? [] : $results); + + } + + /** + * Populate data. + * + * @param array $result Result set + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function populateIterable(array $result) : array + { + $row = []; + + foreach ($result as $element) { + if (isset($element[static::$primaryField])) { + $row[$element[static::$primaryField]] = $this->populate($element); + } else { + $row[] = $this->populate($element); + } + } + + return $row; + } + + /** + * Load. + * + * @param array $objects Objects to load + * + * @return null + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function with(...$objects) + { + } + + /** + * Get object. + * + * @param mixed $primaryKey Key + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get($primaryKey) + { + $obj = $this->populate($this->getRaw($primaryKey)); + + if (isset($obj)) { + return $obj; + } else { + $class = get_class($this); + $class = str_replace('Mapper', '', $class); + $class = explode('\\', $class); + $pos = count($class) - 1; + $class[$pos] = 'Null' . $class[$pos]; + $class = implode('\\', $class); + + return $class; + } + } + + /** + * Get object. + * + * @param mixed $primaryKey Key + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getRaw($primaryKey) : array + { + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->select('*') + ->from(static::$table) + ->where(static::$table . '.' . static::$primaryField, '=', $primaryKey); + + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + + $results = $sth->fetch(\PDO::FETCH_ASSOC); + + return is_bool($results) ? [] : $results; + } + + /** + * Get all by custom query. + * + * @param Builder $query Query + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getAllByQuery(Builder $query) : array + { + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + + $results = $sth->fetchAll(\PDO::FETCH_ASSOC); + $results = is_bool($results) ? [] : $results; + + return $this->populateIterable($results); + } + + public function getRelations($primaryKey) : array + { + $result = []; + + foreach (static::$hasMany as $member => $value) { + if (!isset($value['mapper'])) { + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->select($value['table'] . '.' . $value['src']) + ->from($value['table']) + ->where($value['table'] . '.' . $value['dst'], '=', $primaryKey); + + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + $result[$member] = $sth->fetchAll(\PDO::FETCH_ASSOC); + } + } + + return $result; + } + + /** + * Get object. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getAll() + { + return $this->populateIterable($this->getAllRaw()); + } + + /** + * Get all in raw output. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getAllRaw() : array + { + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->select('*') + ->from(static::$table); + + $sth = $this->db->con->prepare($query->toSql()); + $sth->execute(); + + $results = $sth->fetchAll(\PDO::FETCH_ASSOC); + + return is_bool($results) ? [] : $results; + } + + /** + * Get primary field. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getPrimaryField() : \string + { + return static::$primaryField; + } + + /** + * Get main table. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getTable() : \string + { + return static::$table; + } + +} diff --git a/DataStorage/Database/DatabaseStatus.php b/DataStorage/Database/DatabaseStatus.php new file mode 100644 index 000000000..f7ba77c81 --- /dev/null +++ b/DataStorage/Database/DatabaseStatus.php @@ -0,0 +1,41 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database; + +use phpOMS\Datatypes\Enum; + +/** + * Database status enum. + * + * Possible database connection status + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class DatabaseStatus extends Enum +{ + const OK = 0; /* Database connection successful */ + const MISSING_DATABASE = 1; /* Couldn't find database */ + const MISSING_TABLE = 2; /* One of the core tables couldn't be found */ + const FAILURE = 3; /* Unknown failure */ + const READONLY = 4; /* Database connection is in readonly (but ok) */ + const CLOSED = 5; /* Database connection closed */ +} diff --git a/DataStorage/Database/DatabaseType.php b/DataStorage/Database/DatabaseType.php new file mode 100644 index 000000000..10132b020 --- /dev/null +++ b/DataStorage/Database/DatabaseType.php @@ -0,0 +1,40 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database; + +use phpOMS\Datatypes\Enum; + +/** + * Database type enum. + * + * Database types that are supported by the application + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class DatabaseType extends Enum +{ + const MYSQL = 0; /* MySQL */ + const SQLITE = 1; /* SQLITE */ + const PGSQL = 2; /* PostgreSQL */ + const ORACLE = 3; /* Oracle */ + const SQLSRV = 4; /* Microsoft SQL Server */ +} diff --git a/DataStorage/Database/Grammar.php b/DataStorage/Database/Grammar.php new file mode 100644 index 000000000..2f873b48b --- /dev/null +++ b/DataStorage/Database/Grammar.php @@ -0,0 +1,39 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database; + +abstract class Grammar +{ + + protected $tablePrefix = ''; + + public function getDateFormat() : \string + { + return 'Y-m-d H:i:s'; + } + + public function getTablePrefix() : \string + { + return $this->tablePrefix; + } + + public function setTablePrefix(\string $prefix) + { + $this->tablePrefix = $prefix; + } + +} diff --git a/DataStorage/Database/Pool.php b/DataStorage/Database/Pool.php new file mode 100644 index 000000000..9083e8ea1 --- /dev/null +++ b/DataStorage/Database/Pool.php @@ -0,0 +1,136 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database; + +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; +use phpOMS\DataStorage\Database\Connection\ConnectionFactory; + +/** + * Database pool handler. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Pool +{ + + /** + * Databases. + * + * @var ConnectionAbstract[] + * @since 1.0.0 + */ + private $pool = []; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * Add database. + * + * @param mixed $key Database key + * @param ConnectionAbstract $db Database + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function add($key = 'core', ConnectionAbstract $db) : \bool + { + if (isset($this->pool[$key])) { + return false; + } + + $this->pool[$key] = $db; + + return true; + } + + /** + * Get database. + * + * @param mixed $key Database key + * + * @return ConnectionAbstract + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get($key = 'core') + { + if (!isset($this->pool[$key])) { + return false; + } + + return $this->pool[$key]; + } + + /** + * Remove database. + * + * @param mixed $key Database key + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function remove($key) : \bool + { + if (!isset($this->pool[$key])) { + return false; + } + + unset($this->pool[$key]); + + return true; + } + + /** + * Create database. + * + * @param mixed $key Database key + * @param array $config Database config data + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function create($key, array $config) + { + if (isset($this->pool[$key])) { + return; + } + + $this->pool[$key] = ConnectionFactory::create($config); + } + +} diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php new file mode 100644 index 000000000..ebe184ac0 --- /dev/null +++ b/DataStorage/Database/Query/Builder.php @@ -0,0 +1,883 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query; + +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; + +/** + * Database query builder. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Builder +{ + + /** + * Columns. + * + * @var array + * @since 1.0.0 + */ + public $selects = []; + + /** + * Into. + * + * @var array + * @since 1.0.0 + */ + public $into = null; + + /** + * Into columns. + * + * @var array + * @since 1.0.0 + */ + public $inserts = []; + + /** + * Into columns. + * + * @var array + * @since 1.0.0 + */ + public $values = []; + + /** + * Distinct. + * + * @var \bool + * @since 1.0.0 + */ + public $distinct = false; + + /** + * From. + * + * @var array + * @since 1.0.0 + */ + public $from = []; + + /** + * Joins. + * + * @var array + * @since 1.0.0 + */ + public $joins = []; + + /** + * Where. + * + * @var array + * @since 1.0.0 + */ + public $wheres = []; + + /** + * Group. + * + * @var array + * @since 1.0.0 + */ + public $groups = []; + + /** + * Order. + * + * @var array + * @since 1.0.0 + */ + public $orders = []; + + /** + * Limit. + * + * @var array + * @since 1.0.0 + */ + public $limit = null; + + /** + * Offset. + * + * @var array + * @since 1.0.0 + */ + public $offset = null; + + /** + * Binds. + * + * @var array + * @since 1.0.0 + */ + private $binds = []; + + /** + * Union. + * + * @var array + * @since 1.0.0 + */ + public $unions = []; + + /** + * Lock. + * + * @var \bool + * @since 1.0.0 + */ + public $lock = false; + + /** + * Database connectino. + * + * @var ConnectionAbstract + * @since 1.0.0 + */ + protected $connection = null; + + /** + * Grammar. + * + * @var \phpOMS\DataStorage\Database\Query\Grammar\GrammarInterface + * @since 1.0.0 + */ + protected $grammar = null; + + /** + * Query type. + * + * @var QueryType + * @since 1.0.0 + */ + protected $type = null; + + protected $unionLimit = null; + + protected $unionOffset = null; + + protected $unionOrders = []; + + /** + * Prefix. + * + * @var \string + * @since 1.0.0 + */ + protected $prefix = ''; + + /** + * Comparison operators. + * + * @var \string[] + * @since 1.0.0 + */ + const operators = [ + '=', + '<', + '>', + '<=', + '>=', + '<>', + '!=', + 'like', + 'like binary', + 'not like', + 'between', + 'ilike', + '&', + '|', + '^', + '<<', + '>>', + 'rlike', + 'regexp', + 'not regexp', + '~', + '~*', + '!~', + '!~*', + 'similar to', + 'not similar to', + 'in', + ]; + + /** + * Constructor. + * + * @param ConnectionAbstract $connection Database connection + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(ConnectionAbstract $connection) + { + $this->connection = $connection; + $this->grammar = $connection->getGrammar(); + } + + /** + * Set prefix. + * + * @param \string $prefix Prefix + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function prefix(\string $prefix) + { + $this->prefix = $prefix; + + return $this; + } + + /** + * Get prefix. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getPrefix() : \string + { + return $this->prefix; + } + + /** + * Select. + * + * @param array $columns Columns + * + * @return Builder + * + * @todo Closure is not working this way, needs to be evaluated befor assigning + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function select(...$columns) : Builder + { + // todo handle wrong parameters + + $this->type = QueryType::SELECT; + + foreach ($columns as $key => $column) { + if (is_string($column) || $column instanceof \Closure) { + $this->selects[] = $column; + } else { + throw new \InvalidArgumentException(); + } + } + + return $this; + } + + /** + * Bind parameter. + * + * @param \string|array|\Closure $binds Binds + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function bind($binds) : Builder + { + if (is_array($binds)) { + $this->binds += $binds; + } elseif (is_string($binds) || $binds instanceof \Closure) { + $this->binds[] = $binds; + } else { + throw new \InvalidArgumentException(); + } + + return $this; + } + + /** + * Creating new. + * + * @return self + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function newQuery() : Builder + { + return new static($this->connection, $this->grammar); + } + + /** + * Parsing to string. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function toSql() : \string + { + return $this->grammar->compileQuery($this); + } + + /** + * Executing. + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function execute() + { + // todo: handle different query types (select, insert etc) + } + + /** + * Make raw column selection. + * + * @param \string|\Closure $expression Raw expression + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function selectRaw($expression) : Builder + { + $this->selects[null][] = $expression; + + return $this; + } + + /** + * Is distinct. + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function distinct() : Builder + { + $this->distinct = true; + + return $this; + } + + /** + * From. + * + * @param array $tables Tables + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function from(...$tables) : Builder + { + foreach ($tables as $key => $table) { + if (is_string($table) || $table instanceof \Closure) { + $this->from[] = $table; + } else { + throw new \InvalidArgumentException(); + } + } + + return $this; + } + + /** + * Make raw from. + * + * @param \string|array|\Closure $expression Expression + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function fromRaw($expression) : Builder + { + $this->from[null][] = $expression; + + return $this; + } + + /** + * Or Where. + * + * @param \string|array|\Closure $columns Columns + * @param \string $operator Operator + * @param \string|array|\Closure $values Values + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function orWhere($columns, \string $operator = null, $values = null) : Builder + { + return $this->where($columns, $operator, $values, 'or'); + } + + /** + * Where. + * + * @param \string|array|\Closure $columns Columns + * @param \string|array $operator Operator + * @param mixed $values Values + * @param \string|array $boolean Boolean condition + * + * @return Builder + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function where($columns, $operator = null, $values = null, $boolean = 'and') : Builder + { + // TODO: handle $value is null -> operator NULL + if (isset($operator) && !is_array($operator) && !in_array($operator, self::operators)) { + throw new \InvalidArgumentException('Unknown operator.'); + } + + if (is_array($columns)) { + $i = 0; + foreach ($columns as $key => $column) { + if (isset($operator[$i]) && !in_array($operator[$i], self::operators)) { + throw new \InvalidArgumentException('Unknown operator.'); + } + + $this->wheres[$key][] = ['column' => $column, 'operator' => $operator[$i], + 'value' => $values[$i], + 'boolean' => $boolean[$i],]; + $i++; + } + } elseif (is_string($columns)) { + if (isset($operator) && !in_array($operator, self::operators)) { + throw new \InvalidArgumentException('Unknown operator.'); + } + + $this->wheres[null][] = ['column' => $columns, 'operator' => $operator, 'value' => $values, + 'boolean' => $boolean,]; + } else { + throw new \InvalidArgumentException(); + } + + return $this; + } + + /** + * Where in. + * + * @param \string|array|\Closure $column Column + * @param mixed $values Values + * @param \string $boolean Boolean condition + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function whereIn($column, $values = null, \string $boolean = 'and') : Builder + { + $this->where($column, 'in', $values, $boolean); + + return $this; + } + + /** + * Where null. + * + * @param \string|array|\Closure $column Column + * @param \string $boolean Boolean condition + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function whereNull($column, \string $boolean = 'and') : Builder + { + $this->where($column, '=', null, $boolean); + + return $this; + } + + /** + * Where not null. + * + * @param \string|array|\Closure $column Column + * @param \string $boolean Boolean condition + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function whereNotNull($column, \string $boolean = 'and') : Builder + { + $this->where($column, '!=', null, $boolean); + + return $this; + } + + /** + * Group by. + * + * @param \string|array|\Closure $columns Grouping result + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function groupBy(...$columns) : Builder + { + foreach ($columns as $key => $column) { + if (is_string($column) || $column instanceof \Closure) { + $this->groups[] = $column; + } else { + throw new \InvalidArgumentException(); + } + } + + return $this; + } + + /** + * Order by newest. + * + * @param \string|\Closure $column Column + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function newest($column) : Builder + { + $this->orderBy($column, 'DESC'); + + return $this; + } + + /** + * Order by oldest. + * + * @param \string|\Closure $column Column + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function oldest($column) : Builder + { + $this->orderBy($column, 'ASC'); + + return $this; + } + + /** + * Order by oldest. + * + * @param \string|array|\Closure $columns Columns + * @param \string|string[] $order Orders + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function orderBy($columns, $order = 'DESC') : Builder + { + if (is_string($columns) || $columns instanceof \Closure) { + $this->orders[] = ['column' => $columns, 'order' => $order]; + } elseif (is_array($columns)) { + foreach ($columns as $key => $column) { + $this->orders[] = ['column' => $column, 'order' => $order[$key]]; + } + } else { + throw new \InvalidArgumentException(); + } + + return $this; + } + + /** + * Offset. + * + * @param \int|\Closure $offset Offset + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function offset($offset) : Builder + { + $this->offset = $offset; + + return $this; + } + + /** + * Limit. + * + * @param \int|\Closure $limit Limit + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function limit($limit) : Builder + { + $this->limit = $limit; + + return $this; + } + + /** + * Limit. + * + * @param \string|\phpOMS\DataStorage\Database\Query\Builder $query Query + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function union($query) : Builder + { + if (!is_array($query)) { + $this->unions[] = $query; + } else { + $this->unions += $query; + } + + return $this; + } + + public function lock() + { + } + + public function lockUpdate() + { + } + + public function __toString() + { + return $this->grammar->compileQuery($this); + } + + public function find() + { + } + + public function count() + { + } + + public function exists() + { + } + + public function min() + { + } + + public function max() + { + } + + public function sum() + { + } + + public function avg() + { + } + + public function get() + { + return $this; + } + + /** + * Insert into columns. + * + * @param array $columns Columns + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function insert(...$columns) : Builder + { + $this->type = QueryType::INSERT; + + foreach ($columns as $key => $column) { + $this->inserts[] = $column; + } + + return $this; + } + + /** + * Table to insert into. + * + * @param \string|\Closure $table Table + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function into($table) : Builder + { + $this->into = $table; + + return $this; + } + + /** + * Values to insert. + * + * @param array $values Values + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function values(...$values) : Builder + { + $this->values[] = $values; + + return $this; + } + + public function update() : Builder + { + $this->type = QueryType::UPDATE; + + return $this; + } + + public function increment() + { + } + + public function decrement() + { + } + + public function delete() + { + $this->type = QueryType::DELETE; + + return $this; + } + + public function create($table) + { + return $this; + } + + public function drop() + { + return $this; + } + + public function raw() + { + return $this; + } + + public function join($table1, $table2, $column1, $opperator, $column2) + { + return $this; + } + + public function joinWhere() + { + } + + public function leftJoin() + { + } + + public function leftJoinWhere() + { + } + + public function rightJoin() + { + } + + public function rightJoinWhere() + { + } + + public function rollback() + { + return $this; + } + + public function commit() : Builder + { + return $this; + } + + /** + * Get query type. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getType() : \int + { + return $this->type; + } + + public function merge($query) : Builder { + return clone($this); + } +} diff --git a/DataStorage/Database/Query/Column.php b/DataStorage/Database/Query/Column.php new file mode 100644 index 000000000..91ca433e4 --- /dev/null +++ b/DataStorage/Database/Query/Column.php @@ -0,0 +1,58 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query; + +/** + * Database query builder. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Column +{ + + private $column = ''; + + /** + * Constructor. + * + * @param \string $column Column + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(\string $column) + { + $this->column = $column; + } + + public function getColumn() : \string + { + return $this->column; + } + + public function setColumn(\string $column) + { + $this->column = $column; + } + +} diff --git a/DataStorage/Database/Query/Expression.php b/DataStorage/Database/Query/Expression.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php new file mode 100644 index 000000000..e8ebde746 --- /dev/null +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -0,0 +1,533 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\Database\Query\Column; +use phpOMS\DataStorage\Database\Query\QueryType; + +/** + * Database query grammar. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Grammar extends \phpOMS\DataStorage\Database\Grammar +{ + + /** + * Comment style. + * + * @var \string + * @since 1.0.0 + */ + protected $comment = '--'; + + /** + * String quotes style. + * + * @var \string + * @since 1.0.0 + */ + protected $valueQuotes = '\''; + + /** + * System identifier. + * + * @var \string + * @since 1.0.0 + */ + public $systemIdentifier = '"'; + + /** + * And operator. + * + * @var \string + * @since 1.0.0 + */ + protected $and = 'AND'; + + /** + * Or operator. + * + * @var \string + * @since 1.0.0 + */ + protected $or = 'OR'; + + /** + * Select components. + * + * @var \string[] + * @since 1.0.0 + */ + protected $selectComponents = [ + 'aggregate', + 'selects', + 'from', + 'joins', + 'wheres', + 'havings', + 'groups', + 'orders', + 'limit', + 'offset', + 'unions', + 'lock', + ]; + + /** + * Insert components. + * + * @var \string[] + * @since 1.0.0 + */ + protected $insertComponents = [ + 'into', + 'inserts', + 'values', + ]; + + /** + * Update components. + * + * @var \string[] + * @since 1.0.0 + */ + protected $updateComponents = [ + 'updates', + 'sets', + 'wheres', + ]; + + /** + * Compile to query. + * + * @param Builder $query Builder + * + * @return \string + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function compileQuery($query) : \string + { + return trim( + implode(' ', + array_filter( + $this->compileComponents($query), + function ($value) { + return (string) $value !== ''; + } + ) + ) + ) . ';'; + } + + /** + * Compile components. + * + * @param Builder $query Builder + * + * @return \string[] + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileComponents(Builder $query) : array + { + $sql = []; + + switch ($query->getType()) { + case QueryType::SELECT: + $components = $this->selectComponents; + break; + case QueryType::INSERT: + $components = $this->insertComponents; + break; + case QueryType::UPDATE: + $components = null; + break; + case QueryType::DELETE: + $components = null; + break; + default: + throw new \InvalidArgumentException('Unknown query type.'); + } + + /* Loop all possible query components and if they exist compile them. */ + foreach ($components as $component) { + if (isset($query->{$component}) && !empty($query->{$component})) { + $sql[$component] = $this->{'compile' . ucfirst($component)}($query, $query->{$component}); + } + } + + return $sql; + } + + /** + * Compile select. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileSelects(Builder $query, array $columns) : \string + { + $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); + + if ($expression == '') { + return ''; + } + + return ($query->distinct ? 'SELECT DISTINCT ' : 'SELECT ') . $expression; + } + + /** + * Expressionize elements. + * + * @param array $elements Elements + * @param \string $prefix Prefix for table + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function expressionizeTableColumn(array $elements, \string $prefix = '') : \string + { + $expression = ''; + + foreach ($elements as $key => $element) { + if (is_string($element) && $element !== '*') { + $expression .= $this->compileSystem($element, $prefix) . ', '; + } elseif (is_string($element) && $element === '*') { + $expression .= '*, '; + } elseif ($element instanceof \Closure) { + $expression .= $element() . ', '; + } elseif ($element instanceof Builder) { + $expression .= $element->toSql() . ', '; + } else { + throw new \InvalidArgumentException(); + } + } + + return rtrim($expression, ', '); + } + + /** + * Compile from. + * + * @param Builder $query Builder + * @param array $table Tables + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileFrom(Builder $query, array $table) : \string + { + $expression = $this->expressionizeTableColumn($table, $query->getPrefix()); + + if ($expression == '') { + return ''; + } + + return 'FROM ' . $expression; + } + + /** + * Compile where. + * + * @param Builder $query Builder + * @param array $wheres Where elmenets + * @param \bool $first Is first element (usefull for nesting) + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileWheres(Builder $query, array $wheres, \bool $first = true) : \string + { + $expression = ''; + + foreach ($wheres as $key => $where) { + foreach ($where as $key2 => $element) { + if (!$first) { + $expression .= ' ' . strtoupper($element['boolean']) . ' '; + } + + if (is_string($element['column'])) { + $expression .= $this->compileSystem($element['column'], $query->getPrefix()) . ' ' . strtoupper($element['operator']) . ' ' . $this->compileValue($element['value'], $query->getPrefix()); + } elseif ($element['column'] instanceof \Closure) { + } elseif ($element['column'] instanceof Builder) { + } + + if (is_string($element['value'])) { + } elseif ($element['value'] instanceof \Closure) { + } elseif ($element['value'] instanceof Builder) { + } elseif ($element['value'] instanceof \DateTime) { + } elseif (is_int($element['value'])) { + } elseif (is_bool($element['value'])) { + } elseif (is_null($element['value'])) { + } elseif (is_float($element['value'])) { + } elseif (is_array($element['value'])) { + // is bind + } + + $first = false; + } + } + + if ($expression == '') { + return ''; + } + + return 'WHERE ' . $expression; + } + + /** + * Compile value. + * + * @param array|string|\Closure $value Value + * @param \string $prefix Prefix in case value is a table + * + * @return \string + * + * @throws \InvalidArgumentException + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileValue($value, $prefix = '') : \string + { + if (is_string($value)) { + return $this->valueQuotes . $value . $this->valueQuotes; + } elseif (is_int($value)) { + return $value; + } elseif (is_array($value)) { + $values = ''; + + foreach ($value as $val) { + $values .= $this->compileValue($val) . ', '; + } + + return '(' . rtrim($values, ', ') . ')'; + } elseif ($value instanceof \DateTime) { + return $this->valueQuotes . $value->format('Y-m-d h:m:s') . $this->valueQuotes; + } elseif (is_null($value)) { + return 'NULL'; + } elseif (is_bool($value)) { + return (string) ((int) $value); + } elseif ($value instanceof Column) { + return $this->compileSystem($value->getColumn(), $prefix); + } else { + throw new \InvalidArgumentException(); + } + } + + /** + * Compile system. + * + * @param array|\string $system System + * @param \string $prefix Prefix for table + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileSystem($system, \string $prefix = '') : \string + { + if (count($split = explode('.', $system)) == 2) { + return $this->compileSystem($prefix . $split[0]) . '.' . $this->compileSystem($split[1]); + } else { + return $this->systemIdentifier . $prefix . $system . $this->systemIdentifier; + } + } + + /** + * Compile limit. + * + * @param Builder $query Builder + * @param \int $limit Limit + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileLimit(Builder $query, $limit) : \string + { + return 'LIMIT ' . $limit; + } + + /** + * Compile offset. + * + * @param Builder $query Builder + * @param \int $offset Offset + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileOffset(Builder $query, $offset) : \string + { + return 'OFFSET ' . $offset; + } + + private function compileJoins() + { + return ''; + } + + private function compileGroups(Builder $query, array $groups) + { + $expression = ''; + + foreach ($groups as $group) { + $expression .= $this->compileSystem($group, $query->getPrefix()) . ', '; + } + + $expression = rtrim($expression, ', '); + + return 'GROUP BY ' . $expression; + } + + /** + * Compile offset. + * + * @param Builder $query Builder + * @param array $orders Order + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function compileOrders(Builder $query, array $orders) : \string + { + $expression = ''; + + foreach ($orders as $order) { + $expression .= $this->compileSystem($order['column'], $query->getPrefix()) . ' ' . $order['order'] . ', '; + } + + if ($expression == '') { + return ''; + } + + $expression = rtrim($expression, ', '); + + return 'ORDER BY ' . $expression; + } + + private function compileUnions() + { + return ''; + } + + private function compileLock() + { + return ''; + } + + /** + * Compile insert into table. + * + * @param Builder $query Builder + * @param \string $table Table + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileInto(Builder $query, $table) : \string + { + return 'INSERT INTO ' . $this->compileSystem($table, $query->getPrefix()); + } + + /** + * Compile insert into columns. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileInserts(Builder $query, array $columns) : \string + { + $cols = ''; + + foreach ($columns as $column) { + $cols .= $this->compileSystem($column) . ', '; + } + + if ($cols == '') { + return ''; + } + + return '(' . rtrim($cols, ', ') . ')'; + } + + /** + * Compile insert values. + * + * @param Builder $query Builder + * @param array $values Values + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileValues(Builder $query, array $values) : \string + { + $vals = ''; + + foreach ($values as $value) { + $vals .= $this->compileValue($value) . ', '; + } + + if ($vals == '') { + return ''; + } + + return 'VALUES ' . rtrim($vals, ', '); + } +} diff --git a/DataStorage/Database/Query/Grammar/GrammarInterface.php b/DataStorage/Database/Query/Grammar/GrammarInterface.php new file mode 100644 index 000000000..1da09b5a1 --- /dev/null +++ b/DataStorage/Database/Query/Grammar/GrammarInterface.php @@ -0,0 +1,23 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +use phpOMS\DataStorage\Database\Grammar; + +class GrammarInterface extends Grammar +{ +} diff --git a/DataStorage/Database/Query/Grammar/MysqlGrammar.php b/DataStorage/Database/Query/Grammar/MysqlGrammar.php new file mode 100644 index 000000000..de0f83028 --- /dev/null +++ b/DataStorage/Database/Query/Grammar/MysqlGrammar.php @@ -0,0 +1,29 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +class MysqlGrammar extends Grammar +{ + + /** + * System identifier. + * + * @var \string + * @since 1.0.0 + */ + public $systemIdentifier = '`'; +} diff --git a/DataStorage/Database/Query/Grammar/PostgresGrammar.php b/DataStorage/Database/Query/Grammar/PostgresGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php new file mode 100644 index 000000000..3e189748b --- /dev/null +++ b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php @@ -0,0 +1,29 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Query\Grammar; + +class SqliteGrammar extends Grammar +{ + + /** + * System identifier. + * + * @var \string + * @since 1.0.0 + */ + public $systemIdentifier = '`'; +} diff --git a/DataStorage/Database/Query/Grammar/SqlServerGrammar.php b/DataStorage/Database/Query/Grammar/SqlServerGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Query/QueryType.php b/DataStorage/Database/Query/QueryType.php new file mode 100644 index 000000000..89ea4a162 --- /dev/null +++ b/DataStorage/Database/Query/QueryType.php @@ -0,0 +1,37 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Query; + +use phpOMS\Datatypes\Enum; + +/** + * Query type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class QueryType extends Enum +{ + const SELECT = 0; + const INSERT = 1; + const UPDATE = 2; + const DELETE = 3; +} diff --git a/DataStorage/Database/Schema/Builder.php b/DataStorage/Database/Schema/Builder.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Schema/Grammar/GrammarInterface.php b/DataStorage/Database/Schema/Grammar/GrammarInterface.php new file mode 100644 index 000000000..eb966bd3b --- /dev/null +++ b/DataStorage/Database/Schema/Grammar/GrammarInterface.php @@ -0,0 +1,61 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Schema; + +interface GrammarInterface +{ + + public function typeTinyInt($places); + + public function typeSmallInt($places); + + public function typeMediumInt($places); + + public function typeInt($places); + + public function typeBigInt($places); + + public function typeFloat($m, $e, $b = 10); + + public function typeDouble($m, $e, $b = 10); + + public function typeDecimal($a, $b); + + public function typeBoolean(); + + public function typeJson(); + + public function typeDate(); + + public function typeTime(); + + public function typeDateTime(); + + public function typeTimestamp(); + + public function typeBinary(); + + public function typeChar(); + + public function typeString(); + + public function typeMediumText(); + + public function typeText(); + + public function typeLongText(); + +} diff --git a/DataStorage/Database/Schema/Grammar/MysqlGrammar.php b/DataStorage/Database/Schema/Grammar/MysqlGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Schema/Grammar/PostgresGrammar.php b/DataStorage/Database/Schema/Grammar/PostgresGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Schema/Grammar/SQLiteGrammar.php b/DataStorage/Database/Schema/Grammar/SQLiteGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Schema/Grammar/SqlServerGrammar.php b/DataStorage/Database/Schema/Grammar/SqlServerGrammar.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/Schema/QueryType.php b/DataStorage/Database/Schema/QueryType.php new file mode 100644 index 000000000..bd834cf07 --- /dev/null +++ b/DataStorage/Database/Schema/QueryType.php @@ -0,0 +1,38 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Schema; + +use phpOMS\Datatypes\Enum; + +/** + * Database type enum. + * + * Database types that are supported by the application + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class QueryType extends Enum +{ + const CREATE = 0; + const DROP = 1; + const ALTER = 2; +} diff --git a/DataStorage/Session/ConsoleSession.php b/DataStorage/Session/ConsoleSession.php new file mode 100644 index 000000000..cb1bcf170 --- /dev/null +++ b/DataStorage/Session/ConsoleSession.php @@ -0,0 +1,99 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Session; + +/** + * Console session class. + * + * @category Framework + * @package phpOMS\DataStorage\Session + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ConsoleSession implements SessionInterface +{ + + /** + * Session ID. + * + * @var \string|\int + * @since 1.0.0 + */ + private $sid = null; + + /** + * Constructor. + * + * @param \string|\int $sid Session id + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct($sid) + { + $this->sid = $sid; + } + + /** + * {@inheritdoc} + */ + public function get($key) + { + } + + /** + * {@inheritdoc} + */ + public function set($key, $value, \bool $overwrite = true) : \bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function remove($key) : \bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function getSID() + { + return $this->sid; + } + + /** + * {@inheritdoc} + */ + public function setSID($sid) + { + $this->sid = $sid; + } + + /** + * {@inheritdoc} + */ + public function save() + { + } + +} diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php new file mode 100644 index 000000000..90a42061f --- /dev/null +++ b/DataStorage/Session/HttpSession.php @@ -0,0 +1,128 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Session; + +/** + * Http session class. + * + * @category Framework + * @package phpOMS\DataStorage\Session + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class HttpSession implements SessionInterface +{ + + private $sessionData = []; + + /** + * Session ID. + * + * @var \string|int + * @since 1.0.0 + */ + private $sid = null; + + /** + * Constructor. + * + * @param \int $liftetime Session life time + * @param \string|int|\bool $sid Session id + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(\int $liftetime = 3600, $sid = false) + { + if ($sid !== false) { + session_id($sid); + } + + session_set_cookie_params($liftetime); + session_start(); + $this->sessionData = $_SESSION; + + $this->sid = session_id(); + session_write_close(); + } + + /** + * {@inheritdoc} + */ + public function get($key) + { + return $this->sessionData[$key] ?? null; + } + + /** + * {@inheritdoc} + */ + public function set($key, $value, \bool $overwrite = true) : \bool + { + if($overwrite || !isset($this->sessionData[$key])) { + $this->sessionData[$key] = $value; + + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function save() + { + session_id($this->sid); + session_start(); + $_SESSION = $this->sessionData; + session_write_close(); + } + + /** + * {@inheritdoc} + */ + public function remove($key) : \bool + { + if (isset($this->sessionData[$key])) { + unset($this->sessionData[$key]); + + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function getSID() + { + return $this->sid; + } + + /** + * {@inheritdoc} + */ + public function setSID($sid) + { + $this->sid = $sid; + } + +} diff --git a/DataStorage/Session/README.md b/DataStorage/Session/README.md new file mode 100644 index 000000000..7068b1076 --- /dev/null +++ b/DataStorage/Session/README.md @@ -0,0 +1 @@ +# Session # diff --git a/DataStorage/Session/SessionInterface.php b/DataStorage/Session/SessionInterface.php new file mode 100644 index 000000000..c39b223a3 --- /dev/null +++ b/DataStorage/Session/SessionInterface.php @@ -0,0 +1,102 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Session; + +/** + * Session interface. + * + * Sessions can be used by http requests, console interaction and socket connections + * + * @category Framework + * @package phpOMS\DataStorage\Cache + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface SessionInterface +{ + + /** + * Get session variable by key. + * + * @param \string|\int $key Value key + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get($key); + + /** + * Store session value by key. + * + * @param \string|\int $key Value key + * @param mixed $value Value to store + * @param \bool $overwrite Overwrite existing values + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set($key, $value, \bool $overwrite = true) : \bool; + + /** + * Remove value from session by key. + * + * @param \string|\int $key Value key + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function remove($key) : \bool; + + /** + * Save session. + * + * @todo : implement save type (session, cache, database) + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function save(); + + /** + * @return \int|\string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getSID(); + + /** + * @param \int|\string $sid + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setSID($sid); + +} diff --git a/DataStorage/Session/SocketSession.php b/DataStorage/Session/SocketSession.php new file mode 100644 index 000000000..05bc2c7a7 --- /dev/null +++ b/DataStorage/Session/SocketSession.php @@ -0,0 +1,99 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Session; + +/** + * Socket session class. + * + * @category Framework + * @package phpOMS\DataStorage\Session + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class SocketSession implements SessionInterface +{ + + /** + * Session ID. + * + * @var \string|\int + * @since 1.0.0 + */ + private $sid = null; + + /** + * Constructor. + * + * @param \string|\int $sid Session id + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct($sid) + { + $this->sid = $sid; + } + + /** + * {@inheritdoc} + */ + public function get($key) + { + } + + /** + * {@inheritdoc} + */ + public function set($key, $value, \bool $overwrite = true) : \bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function remove($key) : \bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function getSID() + { + return $this->sid; + } + + /** + * {@inheritdoc} + */ + public function setSID($sid) + { + $this->sid = $sid; + } + + /** + * {@inheritdoc} + */ + public function save() + { + } + +} diff --git a/Datatypes/Address.php b/Datatypes/Address.php new file mode 100644 index 000000000..e9ebfb029 --- /dev/null +++ b/Datatypes/Address.php @@ -0,0 +1,168 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +use phpOMS\Contract\JsonableInterface; + +/** + * Address class. + * + * @category Framework + * @package phpOMS\Datatypes + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Address implements JsonableInterface +{ + + /** + * Name of the receiver. + * + * @var \string + * @since 1.0.0 + */ + private $recipient = null; + + /** + * Sub of the address. + * + * @var \string + * @since 1.0.0 + */ + private $fao = null; + + /** + * Location. + * + * @var Location + * @since 1.0.0 + */ + private $location = null; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + $this->location = new Location(); + } + + /** + * Get recipient. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getRecipient() : \string + { + return $this->recipient; + } + + /** + * Set recipient. + * + * @param \string $recipient Recipient + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setRecipient(\string $recipient) + { + $this->recipient = $recipient; + } + + /** + * Get FAO. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getFAO() : \string + { + return $this->fao; + } + + /** + * Set FAO. + * + * @param \string $fao FAO + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setFAO(\string $fao) + { + $this->fao = $fao; + } + + /** + * Get location. + * + * @return Location + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getLocation() : Location + { + return $this->location; + } + + /** + * Set location. + * + * @param Location $location Location + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setLocation(Location $location) + { + $this->location = $location; + } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return ['recipient' => $this->recipient, 'fao' => $this->fao, 'location' => $this->location->toArray()]; + } + + /** + * {@inheritdoc} + */ + public function toJson(\int $option = 0) : \string + { + return json_encode($this->toArray()); + } +} diff --git a/Datatypes/AddressType.php b/Datatypes/AddressType.php new file mode 100644 index 000000000..76ef3334c --- /dev/null +++ b/Datatypes/AddressType.php @@ -0,0 +1,36 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +/** + * Address type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class AddressType extends Enum +{ + const HOME = 1; + const BUSINESS = 2; + const SHIPPING = 3; + const BILLING = 4; + const WORK = 5; +} diff --git a/Datatypes/Enum.php b/Datatypes/Enum.php new file mode 100644 index 000000000..820cf210f --- /dev/null +++ b/Datatypes/Enum.php @@ -0,0 +1,98 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +/** + * Enum class. + * + * Replacing the SplEnum class and providing basic enum. + * + * @category Framework + * @package phpOMS\Datatypes + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class Enum +{ + + /** + * Caching constant values. + * + * @var array + * @since 1.0.0 + */ + private static $constCache = null; + + /** + * Checking enum name. + * + * Checking if a certain const name exists (case sensitive) + * + * @param \string $name Name of the value (case sensitive) + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function isValidName(\string $name) : \bool + { + $constants = self::getConstants(); + + return array_key_exists($name, $constants); + } + + /** + * Getting all constants of this enum. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getConstants() : array + { + if (!isset(self::$constCache)) { + $reflect = new \ReflectionClass(get_called_class()); + self::$constCache = $reflect->getConstants(); + } + + return self::$constCache; + } + + /** + * Check enum value. + * + * Checking if a given value is part of this enum + * + * @param mixed $value Value to check + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function isValidValue($value) : \bool + { + $values = array_values(self::getConstants()); + + return in_array($value, $values, $strict = true); + } + +} diff --git a/Datatypes/EnumArray.php b/Datatypes/EnumArray.php new file mode 100644 index 000000000..a015d6bee --- /dev/null +++ b/Datatypes/EnumArray.php @@ -0,0 +1,109 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +/** + * Enum class. + * + * Replacing the SplEnum class and providing basic enum. + * + * @category Framework + * @package phpOMS\Datatypes + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class EnumArray +{ + + /** + * Checking enum name. + * + * Checking if a certain const name exists (case sensitive) + * + * @param \string $name Name of the value (case sensitive) + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function isValidName(\string $name) : \bool + { + $constants = self::getConstants(); + + return array_key_exists($name, $constants); + } + + /** + * Getting all constants of this enum. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getConstants() : array + { + /** @var array $constants */ + return (new static())::$constants; + } + + /** + * Check enum value. + * + * Checking if a given value is part of this enum + * + * @param mixed $value Value to check + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function isValidValue($value) : \bool + { + $constants = self::getConstants(); + + return in_array($value, $constants, $strict = true); + } + + /** + * Get enum value by name. + * + * @param mixed $key Key to look for + * + * @return mixed + * + * @throws \Exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function get($key) + { + $constants = self::getConstants()[$key]; + + if (!isset($constants[$key])) { + throw new \OutOfBoundsException('Key "' . $key . '" is not valid.'); + } + + return $constants[$key]; + } + +} diff --git a/Datatypes/ExactFloat.php b/Datatypes/ExactFloat.php new file mode 100644 index 000000000..7ea6dace6 --- /dev/null +++ b/Datatypes/ExactFloat.php @@ -0,0 +1,130 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes\ExactFloat; + +class ExactFloat +{ + + private static $length = 0; + + public static function setLength($length) + { + self::$length = (int) $length; + } + + public static function sum($a, $b, $length = null) + { + $length = $length ?? self::$length; + $split_a = explode('.', $a) + ['0', '0']; + $split_b = explode('.', $b) + ['0', '0']; + + $sign_a = '+'; + $sign_b = '+'; + $sign_sum = ''; + + $decimals = [0, 0]; + $decimals[0] = (int) $split_a[0] + (int) $split_b[0]; + + /* Get sign and length */ + $len_a = strlen($split_a[1]); + + if ($a[0] === '-') { + $sign_a = '-'; + } + + $len_b = strlen($split_b[1]); + + if ($b[0] === '-') { + $sign_b = '-'; + } + + /* Pad to same length */ + $len_max = max($len_a, $len_b); + + $val_a = (int) ($sign_a . str_pad($split_a[1], $len_max, '0')); + $val_b = (int) ($sign_b . str_pad($split_b[1], $len_max, '0')); + + $temp_sum = $val_a + $val_b; + + if ($sign_a === '+' && $sign_b === '+') { + if (strlen((string) $temp_sum) > $len_max) { + $decimals[0]++; + $decimals[1] = (int) substr((string) $temp_sum, 1); + } + } elseif ($sign_a === '+' && $sign_b === '-') { + // todo: keep 0.5 in mind maybe decimals0--?!?!?! most likely not + if ((int) $split_a[0] > (int) $split_b[0]) { + if ($val_a < $val_b) { + $temp_sum = 10 * strlen((string) $temp_sum) - $temp_sum; + } + } else { + if ($val_a > $val_b) { + $temp_sum = 10 * strlen((string) $temp_sum) - $temp_sum; + } + } + + $decimals[1] = (int) ltrim((string) $temp_sum, '-'); + } elseif ($sign_a === '-' && $sign_b === '+') { + if ((int) $split_a[0] < (int) $split_b[0]) { + if ($val_a > $val_b) { + $temp_sum = 10 * strlen((string) $temp_sum) - $temp_sum; + } + } else { + if ($val_a < $val_b) { + $temp_sum = 10 * strlen((string) $temp_sum) - $temp_sum; + } + } + + $decimals[1] = (int) ltrim((string) $temp_sum, '-'); + } elseif ($sign_a === '-' && $sign_b === '-') { + if (strlen((string) $temp_sum) > $len_max + 1) { + $decimals[0]--; + $decimals[1] = (int) substr((string) $temp_sum, 2); + } + + if ($decimals[0] === 0) { + $sign_sum = '-'; + } + } + + /* Too short */ + $str_sum = str_pad((string) $decimals[1], $length, '0'); + + /* Too long */ + /* TODO: handle rounding, carefull with e.g. 1.99995 -> 2.00 */ + if ($sign_sum === '-') { + $str_sum = substr($str_sum, 0, $length + 1); + } else { + $str_sum = substr($str_sum, 0, $length); + } + + return $sign_sum . $decimals[0] . ($length > 0 ? '.' . $str_sum : ''); + } + + public static function mult($a, $b, $length = null) + { + } + + public static function div($a, $b, $length = null) + { + } + + public static function __toString() + { + return ''; + } + +} diff --git a/Datatypes/Exception/InvalidEnumName.php b/Datatypes/Exception/InvalidEnumName.php new file mode 100644 index 000000000..b32b52619 --- /dev/null +++ b/Datatypes/Exception/InvalidEnumName.php @@ -0,0 +1,39 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes\Exception; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class InvalidEnumName extends \UnexpectedValueException +{ + + public function __construct($message, $code = 0, \Exception $previous = null) + { + parent::__construct('The enum name "' . $message . '" is not valid.', $code, $previous); + } + +} diff --git a/Datatypes/Exception/InvalidEnumValue.php b/Datatypes/Exception/InvalidEnumValue.php new file mode 100644 index 000000000..32c04b1d3 --- /dev/null +++ b/Datatypes/Exception/InvalidEnumValue.php @@ -0,0 +1,39 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes\Exception; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class InvalidEnumValue extends \UnexpectedValueException +{ + + public function __construct($message, $code = 0, \Exception $previous = null) + { + parent::__construct('The enum value "' . $message . '" is not valid.', $code, $previous); + } + +} diff --git a/Datatypes/Location.php b/Datatypes/Location.php new file mode 100644 index 000000000..3e7537723 --- /dev/null +++ b/Datatypes/Location.php @@ -0,0 +1,259 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +use phpOMS\Contract\JsonableInterface; + +/** + * Location class. + * + * @category Framework + * @package phpOMS\Datatypes + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Location implements JsonableInterface +{ + + /** + * Zip or postal. + * + * @var \string + * @since 1.0.0 + */ + private $postal = null; + + /** + * Name of city. + * + * @var \string + * @since 1.0.0 + */ + private $city = null; + + /** + * Name of the country. + * + * @var \string + * @since 1.0.0 + */ + private $country = null; + + /** + * Street & district. + * + * @var \string + * @since 1.0.0 + */ + private $address = null; + + /** + * State. + * + * @var \string + * @since 1.0.0 + */ + private $state = null; + + /** + * Geo coordinates. + * + * @var \float[] + * @since 1.0.0 + */ + private $geo = null; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getPostal() : \string + { + return $this->postal; + } + + /** + * @param \string $postal + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setPostal(\string $postal) + { + $this->postal = $postal; + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getCity() : \string + { + return $this->city; + } + + /** + * @param \string $city + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setCity(\string $city) + { + $this->city = $city; + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getCountry() : \string + { + return $this->country; + } + + /** + * @param \string $country + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setCountry(\string $country) + { + $this->country = $country; + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getAddress() : \string + { + return $this->address; + } + + /** + * @param \string $address + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setAddress(\string $address) + { + $this->address = $address; + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getState() : \string + { + return $this->state; + } + + /** + * @param \string $state + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setState(\string $state) + { + $this->state = $state; + } + + /** + * @return \float[] + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getGeo() : array + { + return $this->geo; + } + + /** + * @param \float[] $geo + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setGeo(array $geo) + { + $this->geo = $geo; + } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'postal' => $this->postal, + 'city' => $this->city, + 'country' => $this->country, + 'address' => $this->address, + 'state' => $this->state, + 'geo' => $this->geo, + ]; + } + + /** + * {@inheritdoc} + */ + public function toJson(\int $option = 0) : \string + { + return json_encode($this->toArray()); + } + +} diff --git a/Datatypes/PhoneType.php b/Datatypes/PhoneType.php new file mode 100644 index 000000000..279718947 --- /dev/null +++ b/Datatypes/PhoneType.php @@ -0,0 +1,35 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +/** + * Address type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class PhoneType extends Enum +{ + const HOME = 1; + const BUSINESS = 2; + const MOBILE = 3; + const WORK = 4; +} diff --git a/Datatypes/SmartDateTime.php b/Datatypes/SmartDateTime.php new file mode 100644 index 000000000..d6ca053f9 --- /dev/null +++ b/Datatypes/SmartDateTime.php @@ -0,0 +1,102 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Datatypes; + +/** + * SmartDateTime. + * + * Providing smarter datetimes + * + * @category Modules + * @package phpOMS\Datatypes + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class SmartDateTime extends \DateTime +{ + const FORMAT = 'Y-m-d hh:mm:ss'; + const TIMEZONE = 'UTC'; + + /** + * {@inheritdoc} + */ + public function __construct($time = 'now', $timezone = null) + { + parent::__construct($time, $timezone); + } + + /** + * Modify datetime in a smart way. + * + * @param \int $y Year + * @param \int $m Month + * @param \int $d Day + * @param \int $calendar Calendar + * + * @return SmartDateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function createModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN) : SmartDateTime + { + $dt = clone $this; + $dt->smartModify($y, $m, $d, $calendar); + + return $dt; + } + + /** + * Modify datetime in a smart way. + * + * @param \int $y Year + * @param \int $m Month + * @param \int $d Day + * @param \int $calendar Calendar + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function smartModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN) + { + $y_new = (int) $this->format('Y') + $y; + $m_new = ((int) $this->format('m') + $m) % 12; + $m_new = $m_new === 0 ? 12 : $m_new; + $d_month_old = cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y')); + $d_month_new = cal_days_in_month($calendar, $m_new, $y_new); + $d_old = (int) $this->format('d'); + + if ($d_old > $d_month_new) { + $d_new = $d_month_new; + } elseif ($d_old < $d_month_new && $d_old === $d_month_old) { + $d_new = $d_month_new; + } else { + $d_new = $d_old; + } + + $this->setDate($y_new, $m_new, $d_new); + + if ($d !== 0) { + $this->modify($d . ' day'); + } + } + +} diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php new file mode 100644 index 000000000..8a83c5353 --- /dev/null +++ b/Dispatcher/Dispatcher.php @@ -0,0 +1,160 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Dispatcher; + +use phpOMS\ApplicationAbstract; +use phpOMS\Message\RequestAbstract; +use phpOMS\Message\ResponseAbstract; +use phpOMS\Module\ModuleAbstract; +use phpOMS\Views\ViewLayout; + +/** + * Dispatcher class. + * + * @category Framework + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Dispatcher +{ + + /** + * Application. + * + * @var ApplicationAbstract + * @since 1.0.0 + */ + private $app = null; + + /** + * Controller. + * + * Set in the module manager on module initialization. + * + * @var array + * @since 1.0.0 + */ + private $controllers = []; + + /** + * Constructor. + * + * @param ApplicationAbstract $app Appliaction + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(ApplicationAbstract $app) + { + $this->app = $app; + } + + /** + * Dispatch controller. + * + * @param \string|array|\Closure $controller Controller string + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param mixed $data Data + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function dispatch($controller, RequestAbstract $request, ResponseAbstract $response, $data = null) : array + { + $views = []; + $type = ViewLayout::UNDEFINED; + + if (isset($controller['type'])) { + $type = $controller['type']; + $controller = $controller['dest']; + } + + if (is_string($controller)) { + $dispatch = explode(':', $controller); + $this->get($dispatch[0]); + + if (count($dispatch) == 3) { + /* Handling static functions */ + $views[$type][$controller] = $dispatch[0]::$dispatch[2](); + } else { + $views[$type][$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); + } + } elseif (is_array($controller)) { + foreach ($controller as $controllerSingle) { + foreach ($controllerSingle as $c) { + $views += $this->dispatch($c, $request, $response, $data); + } + } + } elseif ($controller instanceof \Closure) { + $views[$type][] = $controller($this->app, $request, $response, $data); + } else { + throw new \UnexpectedValueException('Unexpected controller type.'); + } + + return $views; + } + + /** + * Get controller. + * + * @param \string $controller Controller string + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get(\string $controller) : \bool + { + if (!isset($this->controllers[$controller])) { + $this->controllers[$controller] = new $controller($this->app); + + return true; + } + + return false; + } + + /** + * Set controller by alias. + * + * @param ModuleAbstract $controller Controller + * @param \string $name Controller string + * + * @return \bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(ModuleAbstract $controller, \string $name) : \bool + { + if (!isset($this->controllers[$name])) { + $this->controllers[$name] = $controller; + + return true; + } + + return false; + } + +} diff --git a/Event/EventManager.php b/Event/EventManager.php new file mode 100644 index 000000000..3568d0bc6 --- /dev/null +++ b/Event/EventManager.php @@ -0,0 +1,134 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Event; + +use phpOMS\Pattern\Mediator; +use phpOMS\Utils\ArrayUtils; + +/** + * EventManager class. + * + * @category Framework + * @package phpOMS\Event + * @since 1.0.0 + * + * @todo : make cachable + database storable -> can reload user defined listeners (persistent events) + */ +class EventManager implements Mediator +{ + + /** + * Events. + * + * @var array + * @since 1.0.0 + */ + private $events = []; + + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct() + { + } + + /** + * {@inheritdoc} + */ + public function attach(\string $event, \Closure $callback = null, \string $listener = null) : \string + { + $this->events[$event][$listener] = $callback; + + return $event . '/' . $listener; + } + + /** + * {@inheritdoc} + */ + public function trigger(\string $event, \Closure $callback = null, \string $source = null) : \int + { + $count = 0; + foreach ($this->events[$event] as $event) { + $event($source); + $count++; + } + + if (isset($callback)) { + /** @var $callback Callable */ + $callback(); + } + + return $count; + } + + /** + * Trigger event. + * + * An object fires an event until it's callback returns false + * + * @param \string $event Event ID + * @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks. + * @param \string $source What class is invoking this event + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function triggerUntil(\string $event, \Closure $callback = null, \string $source = null) : \int + { + $run = true; + $count = 0; + + do { + foreach ($this->events[$event] as $event) { + $run = $event($source); + $count++; + } + } while ($run); + + if ($callback !== null) { + $callback(); + } + + return $count; + } + + /** + * {@inheritdoc} + */ + public function detach(\int $event) + { + $this->events = ArrayUtils::unsetArray($event, $this->events, '/'); + } + + /** + * Count event listenings. + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function count() : \int + { + return count($this->events); + } + +} diff --git a/Html/TagType.php b/Html/TagType.php new file mode 100644 index 000000000..8f9671228 --- /dev/null +++ b/Html/TagType.php @@ -0,0 +1,41 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Html; + +use phpOMS\Datatypes\Enum; + +/** + * Tag type enum. + * + * @category Framework + * @package phpOMS\Html + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class TagType extends Enum +{ + const INPUT = 0; /* */ + const BUTTON = 1; /*