@@ -7,87 +7,87 @@ |
||
| 7 | 7 | |
| 8 | 8 | final class TranslatorTest extends TestCase |
| 9 | 9 | { |
| 10 | - /** |
|
| 10 | +/** |
|
| 11 | 11 | * @var Translator |
| 12 | 12 | */ |
| 13 | - protected $xTranslator; |
|
| 13 | +protected $xTranslator; |
|
| 14 | 14 | |
| 15 | - protected function setUp(): void |
|
| 16 | - { |
|
| 17 | - $this->xTranslator = new Translator(); |
|
| 18 | - $this->xTranslator->loadTranslations(__DIR__ . '/../translations/test.en.php', 'en'); |
|
| 19 | - $this->xTranslator->loadTranslations(__DIR__ . '/../translations/test.fr.php', 'fr'); |
|
| 20 | - } |
|
| 15 | +protected function setUp(): void |
|
| 16 | +{ |
|
| 17 | +$this->xTranslator = new Translator(); |
|
| 18 | +$this->xTranslator->loadTranslations(__DIR__ . '/../translations/test.en.php', 'en'); |
|
| 19 | +$this->xTranslator->loadTranslations(__DIR__ . '/../translations/test.fr.php', 'fr'); |
|
| 20 | +} |
|
| 21 | 21 | |
| 22 | - public function testFileWithBadData() |
|
| 23 | - { |
|
| 24 | - $this->assertFalse($this->xTranslator->loadTranslations(__DIR__ . '/../translations/error.php', 'en')); |
|
| 25 | - } |
|
| 22 | +public function testFileWithBadData() |
|
| 23 | +{ |
|
| 24 | +$this->assertFalse($this->xTranslator->loadTranslations(__DIR__ . '/../translations/error.php', 'en')); |
|
| 25 | +} |
|
| 26 | 26 | |
| 27 | - public function testFileNotFound() |
|
| 28 | - { |
|
| 29 | - $this->assertFalse($this->xTranslator->loadTranslations(__DIR__ . '/../translations/not-found.php', 'en')); |
|
| 30 | - } |
|
| 27 | +public function testFileNotFound() |
|
| 28 | +{ |
|
| 29 | +$this->assertFalse($this->xTranslator->loadTranslations(__DIR__ . '/../translations/not-found.php', 'en')); |
|
| 30 | +} |
|
| 31 | 31 | |
| 32 | - public function testMissingEnTranslations() |
|
| 33 | - { |
|
| 34 | - $this->xTranslator->setLocale('en'); |
|
| 35 | - $this->assertEquals('salutations.hello', $this->xTranslator->trans('salutations.hello')); |
|
| 36 | - } |
|
| 32 | +public function testMissingEnTranslations() |
|
| 33 | +{ |
|
| 34 | +$this->xTranslator->setLocale('en'); |
|
| 35 | +$this->assertEquals('salutations.hello', $this->xTranslator->trans('salutations.hello')); |
|
| 36 | +} |
|
| 37 | 37 | |
| 38 | - public function testMissingFrTranslations() |
|
| 39 | - { |
|
| 40 | - $this->xTranslator->setLocale('fr'); |
|
| 41 | - $this->assertEquals('salutations.hello', $this->xTranslator->trans('salutations.hello')); |
|
| 42 | - } |
|
| 38 | +public function testMissingFrTranslations() |
|
| 39 | +{ |
|
| 40 | +$this->xTranslator->setLocale('fr'); |
|
| 41 | +$this->assertEquals('salutations.hello', $this->xTranslator->trans('salutations.hello')); |
|
| 42 | +} |
|
| 43 | 43 | |
| 44 | - public function testSimpleEnTranslations() |
|
| 45 | - { |
|
| 46 | - $this->xTranslator->setLocale('en'); |
|
| 47 | - $this->assertEquals('Good morning', $this->xTranslator->trans('salutations.morning')); |
|
| 48 | - $this->assertEquals('Good afternoon', $this->xTranslator->trans('salutations.afternoon')); |
|
| 49 | - } |
|
| 44 | +public function testSimpleEnTranslations() |
|
| 45 | +{ |
|
| 46 | +$this->xTranslator->setLocale('en'); |
|
| 47 | +$this->assertEquals('Good morning', $this->xTranslator->trans('salutations.morning')); |
|
| 48 | +$this->assertEquals('Good afternoon', $this->xTranslator->trans('salutations.afternoon')); |
|
| 49 | +} |
|
| 50 | 50 | |
| 51 | - public function testSimpleFrTranslations() |
|
| 52 | - { |
|
| 53 | - $this->xTranslator->setLocale('fr'); |
|
| 54 | - $this->assertEquals('Bonjour', $this->xTranslator->trans('salutations.morning')); |
|
| 55 | - $this->assertEquals('Bonsoir', $this->xTranslator->trans('salutations.afternoon')); |
|
| 56 | - } |
|
| 51 | +public function testSimpleFrTranslations() |
|
| 52 | +{ |
|
| 53 | +$this->xTranslator->setLocale('fr'); |
|
| 54 | +$this->assertEquals('Bonjour', $this->xTranslator->trans('salutations.morning')); |
|
| 55 | +$this->assertEquals('Bonsoir', $this->xTranslator->trans('salutations.afternoon')); |
|
| 56 | +} |
|
| 57 | 57 | |
| 58 | - public function testEnTranslationsWithPlaceholders() |
|
| 59 | - { |
|
| 60 | - $this->xTranslator->setLocale('en'); |
|
| 61 | - $this->assertEquals('Good morning Mr. Johnson', |
|
| 62 | - $this->xTranslator->trans('placeholders.morning', ['title' => 'Mr.', 'name' => 'Johnson'])); |
|
| 63 | - $this->assertEquals('Good afternoon Mrs. Smith', |
|
| 64 | - $this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mrs.', 'name' => 'Smith'])); |
|
| 65 | - } |
|
| 58 | +public function testEnTranslationsWithPlaceholders() |
|
| 59 | +{ |
|
| 60 | +$this->xTranslator->setLocale('en'); |
|
| 61 | +$this->assertEquals('Good morning Mr. Johnson', |
|
| 62 | +$this->xTranslator->trans('placeholders.morning', ['title' => 'Mr.', 'name' => 'Johnson'])); |
|
| 63 | +$this->assertEquals('Good afternoon Mrs. Smith', |
|
| 64 | +$this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mrs.', 'name' => 'Smith'])); |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - public function testFrTranslationsWithPlaceholders() |
|
| 68 | - { |
|
| 69 | - $this->xTranslator->setLocale('fr'); |
|
| 70 | - $this->assertEquals('Bonjour M. Pierre', |
|
| 71 | - $this->xTranslator->trans('placeholders.morning', ['title' => 'M.', 'name' => 'Pierre'])); |
|
| 72 | - $this->assertEquals('Bonsoir Mme Paule', |
|
| 73 | - $this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mme', 'name' => 'Paule'])); |
|
| 74 | - } |
|
| 67 | +public function testFrTranslationsWithPlaceholders() |
|
| 68 | +{ |
|
| 69 | +$this->xTranslator->setLocale('fr'); |
|
| 70 | +$this->assertEquals('Bonjour M. Pierre', |
|
| 71 | +$this->xTranslator->trans('placeholders.morning', ['title' => 'M.', 'name' => 'Pierre'])); |
|
| 72 | +$this->assertEquals('Bonsoir Mme Paule', |
|
| 73 | +$this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mme', 'name' => 'Paule'])); |
|
| 74 | +} |
|
| 75 | 75 | |
| 76 | - public function testExplicitEnTranslations() |
|
| 77 | - { |
|
| 78 | - $this->xTranslator->setLocale('fr'); |
|
| 79 | - $this->assertEquals('Good morning Mr. Johnson', |
|
| 80 | - $this->xTranslator->trans('placeholders.morning', ['title' => 'Mr.', 'name' => 'Johnson'], 'en')); |
|
| 81 | - $this->assertEquals('Good afternoon Mrs. Smith', |
|
| 82 | - $this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mrs.', 'name' => 'Smith'], 'en')); |
|
| 83 | - } |
|
| 76 | +public function testExplicitEnTranslations() |
|
| 77 | +{ |
|
| 78 | +$this->xTranslator->setLocale('fr'); |
|
| 79 | +$this->assertEquals('Good morning Mr. Johnson', |
|
| 80 | +$this->xTranslator->trans('placeholders.morning', ['title' => 'Mr.', 'name' => 'Johnson'], 'en')); |
|
| 81 | +$this->assertEquals('Good afternoon Mrs. Smith', |
|
| 82 | +$this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mrs.', 'name' => 'Smith'], 'en')); |
|
| 83 | +} |
|
| 84 | 84 | |
| 85 | - public function testExplicitFrTranslations() |
|
| 86 | - { |
|
| 87 | - $this->xTranslator->setLocale('en'); |
|
| 88 | - $this->assertEquals('Bonjour M. Pierre', |
|
| 89 | - $this->xTranslator->trans('placeholders.morning', ['title' => 'M.', 'name' => 'Pierre'], 'fr')); |
|
| 90 | - $this->assertEquals('Bonsoir Mme Paule', |
|
| 91 | - $this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mme', 'name' => 'Paule'], 'fr')); |
|
| 92 | - } |
|
| 85 | +public function testExplicitFrTranslations() |
|
| 86 | +{ |
|
| 87 | +$this->xTranslator->setLocale('en'); |
|
| 88 | +$this->assertEquals('Bonjour M. Pierre', |
|
| 89 | +$this->xTranslator->trans('placeholders.morning', ['title' => 'M.', 'name' => 'Pierre'], 'fr')); |
|
| 90 | +$this->assertEquals('Bonsoir Mme Paule', |
|
| 91 | +$this->xTranslator->trans('placeholders.afternoon', ['title' => 'Mme', 'name' => 'Paule'], 'fr')); |
|
| 92 | +} |
|
| 93 | 93 | } |
@@ -1,12 +1,12 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | - 'salutations' => [ |
|
| 5 | - 'morning' => "Good morning", |
|
| 6 | - 'afternoon' => "Good afternoon", |
|
| 7 | - ], |
|
| 8 | - 'placeholders' => [ |
|
| 9 | - 'morning' => "Good morning :title :name", |
|
| 10 | - 'afternoon' => "Good afternoon :title :name", |
|
| 11 | - ], |
|
| 4 | +'salutations' => [ |
|
| 5 | +'morning' => "Good morning", |
|
| 6 | +'afternoon' => "Good afternoon", |
|
| 7 | +], |
|
| 8 | +'placeholders' => [ |
|
| 9 | +'morning' => "Good morning :title :name", |
|
| 10 | +'afternoon' => "Good afternoon :title :name", |
|
| 11 | +], |
|
| 12 | 12 | ]; |
@@ -1,12 +1,12 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | - 'salutations' => [ |
|
| 5 | - 'morning' => "Bonjour", |
|
| 6 | - 'afternoon' => "Bonsoir", |
|
| 7 | - ], |
|
| 8 | - 'placeholders' => [ |
|
| 9 | - 'morning' => "Bonjour :title :name", |
|
| 10 | - 'afternoon' => "Bonsoir :title :name", |
|
| 11 | - ], |
|
| 4 | +'salutations' => [ |
|
| 5 | +'morning' => "Bonjour", |
|
| 6 | +'afternoon' => "Bonsoir", |
|
| 7 | +], |
|
| 8 | +'placeholders' => [ |
|
| 9 | +'morning' => "Bonjour :title :name", |
|
| 10 | +'afternoon' => "Bonsoir :title :name", |
|
| 11 | +], |
|
| 12 | 12 | ]; |
@@ -8,202 +8,202 @@ |
||
| 8 | 8 | |
| 9 | 9 | final class UriDetectorTest extends TestCase |
| 10 | 10 | { |
| 11 | - /** |
|
| 11 | +/** |
|
| 12 | 12 | * @var UriDetector |
| 13 | 13 | */ |
| 14 | - protected $xUriDetector; |
|
| 14 | +protected $xUriDetector; |
|
| 15 | 15 | |
| 16 | - protected function setUp(): void |
|
| 17 | - { |
|
| 18 | - $this->xUriDetector = new UriDetector(); |
|
| 19 | - } |
|
| 16 | +protected function setUp(): void |
|
| 17 | +{ |
|
| 18 | +$this->xUriDetector = new UriDetector(); |
|
| 19 | +} |
|
| 20 | 20 | |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * @throws UriException |
| 23 | 23 | */ |
| 24 | - public function testUri() |
|
| 25 | - { |
|
| 26 | - $this->assertEquals('http://example.test/path', $this->xUriDetector->detect([ |
|
| 27 | - 'REQUEST_URI' => 'http://example.test/path' |
|
| 28 | - ])); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 24 | +public function testUri() |
|
| 25 | +{ |
|
| 26 | +$this->assertEquals('http://example.test/path', $this->xUriDetector->detect([ |
|
| 27 | +'REQUEST_URI' => 'http://example.test/path' |
|
| 28 | +])); |
|
| 29 | +} |
|
| 30 | + |
|
| 31 | +/** |
|
| 32 | 32 | * @throws UriException |
| 33 | 33 | */ |
| 34 | - public function testUriWithParam() |
|
| 35 | - { |
|
| 36 | - $this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 37 | - $this->xUriDetector->detect([ |
|
| 38 | - 'REQUEST_URI' => 'http://example.test/path?param1=value1¶m2="value2"', |
|
| 39 | - ]) |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 34 | +public function testUriWithParam() |
|
| 35 | +{ |
|
| 36 | +$this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 37 | +$this->xUriDetector->detect([ |
|
| 38 | + 'REQUEST_URI' => 'http://example.test/path?param1=value1¶m2="value2"', |
|
| 39 | +]) |
|
| 40 | +); |
|
| 41 | +} |
|
| 42 | + |
|
| 43 | +/** |
|
| 44 | 44 | * @throws UriException |
| 45 | 45 | */ |
| 46 | - public function testUriWithUser() |
|
| 47 | - { |
|
| 48 | - $this->assertEquals('http://[email protected]/path', $this->xUriDetector->detect([ |
|
| 49 | - 'REQUEST_URI' => 'http://[email protected]/path' |
|
| 50 | - ])); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 46 | +public function testUriWithUser() |
|
| 47 | +{ |
|
| 48 | +$this->assertEquals('http://[email protected]/path', $this->xUriDetector->detect([ |
|
| 49 | +'REQUEST_URI' => 'http://[email protected]/path' |
|
| 50 | +])); |
|
| 51 | +} |
|
| 52 | + |
|
| 53 | +/** |
|
| 54 | 54 | * @throws UriException |
| 55 | 55 | */ |
| 56 | - public function testUriWithUserAndPass() |
|
| 57 | - { |
|
| 58 | - $this->assertEquals('http://user:[email protected]/path', $this->xUriDetector->detect([ |
|
| 59 | - 'REQUEST_URI' => 'http://user:[email protected]/path' |
|
| 60 | - ])); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 56 | +public function testUriWithUserAndPass() |
|
| 57 | +{ |
|
| 58 | +$this->assertEquals('http://user:[email protected]/path', $this->xUriDetector->detect([ |
|
| 59 | +'REQUEST_URI' => 'http://user:[email protected]/path' |
|
| 60 | +])); |
|
| 61 | +} |
|
| 62 | + |
|
| 63 | +/** |
|
| 64 | 64 | * @throws UriException |
| 65 | 65 | */ |
| 66 | - public function testUriWithEmptyBasename() |
|
| 67 | - { |
|
| 68 | - $this->assertEquals('http://example.test/', $this->xUriDetector->detect([ |
|
| 69 | - 'REQUEST_URI' => 'http://example.test//' |
|
| 70 | - ])); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 66 | +public function testUriWithEmptyBasename() |
|
| 67 | +{ |
|
| 68 | +$this->assertEquals('http://example.test/', $this->xUriDetector->detect([ |
|
| 69 | +'REQUEST_URI' => 'http://example.test//' |
|
| 70 | +])); |
|
| 71 | +} |
|
| 72 | + |
|
| 73 | +/** |
|
| 74 | 74 | * @throws UriException |
| 75 | 75 | */ |
| 76 | - public function testUriWithParts() |
|
| 77 | - { |
|
| 78 | - $this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 79 | - $this->xUriDetector->detect([ |
|
| 80 | - 'HTTP_SCHEME' => 'http', |
|
| 81 | - 'HTTP_HOST' => 'example.test', |
|
| 82 | - 'PATH_INFO' => '/path', |
|
| 83 | - 'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 84 | - ]) |
|
| 85 | - ); |
|
| 86 | - $this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 87 | - $this->xUriDetector->detect([ |
|
| 88 | - 'HTTPS' => 'off', |
|
| 89 | - 'HTTP_HOST' => 'example.test', |
|
| 90 | - 'PATH_INFO' => '/path', |
|
| 91 | - 'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 92 | - ]) |
|
| 93 | - ); |
|
| 94 | - $this->assertEquals('https://example.test:8080/path', |
|
| 95 | - $this->xUriDetector->detect([ |
|
| 96 | - 'HTTPS' => 'on', |
|
| 97 | - 'SERVER_NAME' => 'example.test:8080', |
|
| 98 | - 'PATH_INFO' => '/path', |
|
| 99 | - ]) |
|
| 100 | - ); |
|
| 101 | - $this->assertEquals('https://example.test:8080/path', |
|
| 102 | - $this->xUriDetector->detect([ |
|
| 103 | - 'HTTPS' => 'on', |
|
| 104 | - 'SERVER_NAME' => 'example.test', |
|
| 105 | - 'SERVER_PORT' => '8080', |
|
| 106 | - 'PATH_INFO' => '/path', |
|
| 107 | - ]) |
|
| 108 | - ); |
|
| 109 | - $this->assertEquals('https://example.test:8080/path', |
|
| 110 | - $this->xUriDetector->detect([ |
|
| 111 | - 'HTTP_X_FORWARDED_SSL' => 'on', |
|
| 112 | - 'SERVER_NAME' => 'example.test:8080', |
|
| 113 | - 'PATH_INFO' => '/path', |
|
| 114 | - ]) |
|
| 115 | - ); |
|
| 116 | - $this->assertEquals('https://example.test:8080/path', |
|
| 117 | - $this->xUriDetector->detect([ |
|
| 118 | - 'HTTP_X_FORWARDED_PROTO' => 'https', |
|
| 119 | - 'SERVER_NAME' => 'example.test:8080', |
|
| 120 | - 'PATH_INFO' => '/path', |
|
| 121 | - ]) |
|
| 122 | - ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 76 | +public function testUriWithParts() |
|
| 77 | +{ |
|
| 78 | +$this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 79 | +$this->xUriDetector->detect([ |
|
| 80 | + 'HTTP_SCHEME' => 'http', |
|
| 81 | + 'HTTP_HOST' => 'example.test', |
|
| 82 | + 'PATH_INFO' => '/path', |
|
| 83 | + 'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 84 | +]) |
|
| 85 | +); |
|
| 86 | +$this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 87 | +$this->xUriDetector->detect([ |
|
| 88 | + 'HTTPS' => 'off', |
|
| 89 | + 'HTTP_HOST' => 'example.test', |
|
| 90 | + 'PATH_INFO' => '/path', |
|
| 91 | + 'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 92 | +]) |
|
| 93 | +); |
|
| 94 | +$this->assertEquals('https://example.test:8080/path', |
|
| 95 | +$this->xUriDetector->detect([ |
|
| 96 | + 'HTTPS' => 'on', |
|
| 97 | + 'SERVER_NAME' => 'example.test:8080', |
|
| 98 | + 'PATH_INFO' => '/path', |
|
| 99 | +]) |
|
| 100 | +); |
|
| 101 | +$this->assertEquals('https://example.test:8080/path', |
|
| 102 | +$this->xUriDetector->detect([ |
|
| 103 | + 'HTTPS' => 'on', |
|
| 104 | + 'SERVER_NAME' => 'example.test', |
|
| 105 | + 'SERVER_PORT' => '8080', |
|
| 106 | + 'PATH_INFO' => '/path', |
|
| 107 | +]) |
|
| 108 | +); |
|
| 109 | +$this->assertEquals('https://example.test:8080/path', |
|
| 110 | +$this->xUriDetector->detect([ |
|
| 111 | + 'HTTP_X_FORWARDED_SSL' => 'on', |
|
| 112 | + 'SERVER_NAME' => 'example.test:8080', |
|
| 113 | + 'PATH_INFO' => '/path', |
|
| 114 | +]) |
|
| 115 | +); |
|
| 116 | +$this->assertEquals('https://example.test:8080/path', |
|
| 117 | +$this->xUriDetector->detect([ |
|
| 118 | + 'HTTP_X_FORWARDED_PROTO' => 'https', |
|
| 119 | + 'SERVER_NAME' => 'example.test:8080', |
|
| 120 | + 'PATH_INFO' => '/path', |
|
| 121 | +]) |
|
| 122 | +); |
|
| 123 | +} |
|
| 124 | + |
|
| 125 | +/** |
|
| 126 | 126 | * @throws UriException |
| 127 | 127 | */ |
| 128 | - public function testRemoveJaxonParam() |
|
| 129 | - { |
|
| 130 | - $this->assertEquals('http://example.test/path', $this->xUriDetector->detect([ |
|
| 131 | - 'REQUEST_URI' => 'http://example.test/path?jxnGenerate=true' |
|
| 132 | - ])); |
|
| 133 | - $this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 134 | - $this->xUriDetector->detect([ |
|
| 135 | - 'REQUEST_URI' => 'http://example.test/path?param1=value1&jxnGenerate=true¶m2="value2"', |
|
| 136 | - ]) |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - public function testErrorMissingHost() |
|
| 141 | - { |
|
| 142 | - $this->expectException(UriException::class); |
|
| 143 | - $this->xUriDetector->detect([ |
|
| 144 | - 'HTTPS' => 'on', |
|
| 145 | - 'PATH_INFO' => '/path', |
|
| 146 | - 'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 147 | - ]); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function testRedirectSimpleUrl() |
|
| 151 | - { |
|
| 152 | - $this->assertEquals('http://example.test/path', |
|
| 153 | - $this->xUriDetector->redirect('http://example.test/path', [])); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - public function testRedirectUrlWithAnchor() |
|
| 157 | - { |
|
| 158 | - $this->assertEquals('http://example.test/path?param=value#anchor', |
|
| 159 | - $this->xUriDetector->redirect('http://example.test/path?param=value#anchor', [])); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - public function testRedirectUrlWithParam() |
|
| 163 | - { |
|
| 164 | - $this->assertEquals('http://example.test/path?param=value', |
|
| 165 | - $this->xUriDetector->redirect('http://example.test/path?param=value', [])); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - public function testRedirectUrlWithParams() |
|
| 169 | - { |
|
| 170 | - $this->assertEquals('http://example.test/path?param1=value1¶m2=value2', |
|
| 171 | - $this->xUriDetector->redirect('http://example.test/path?param1=value1¶m2=value2', [])); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - public function testRedirectUrlWithSpecialChars() |
|
| 175 | - { |
|
| 176 | - $this->assertEquals('http://example.test/path?param1=%22value1%22¶m2=%25value2%25#anchor', |
|
| 177 | - $this->xUriDetector->redirect('http://example.test/path?param1="value1"¶m2=%value2%#anchor', [])); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function testRedirectSpecialUrl() |
|
| 181 | - { |
|
| 182 | - $this->assertEquals('http://example.test/?query1234', |
|
| 183 | - $this->xUriDetector->redirect('http://example.test/?query1234', [])); |
|
| 184 | - |
|
| 185 | - $this->assertEquals('http://example.test/?query1234=', |
|
| 186 | - $this->xUriDetector->redirect('http://example.test/?query1234=', [])); |
|
| 187 | - |
|
| 188 | - $this->assertEquals('http://example.test/?param=value&query1234=0', |
|
| 189 | - $this->xUriDetector->redirect('http://example.test/?param=value&query1234=0', [])); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function testRedirectEncodedUrl() |
|
| 193 | - { |
|
| 194 | - $this->assertEquals('http://example.test/path?param1=%22value1%22¶m2=%25value2%25', |
|
| 195 | - $this->xUriDetector->redirect('http://example.test/path?param1=%22value1%22¶m2=%25value2%25', [])); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - public function testRedirectUnexpectedUrl() |
|
| 199 | - { |
|
| 200 | - $this->assertEquals('http://example.test/', |
|
| 201 | - $this->xUriDetector->redirect('http://example.test/? ', [])); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function testRedirectQueryString() |
|
| 205 | - { |
|
| 206 | - $this->assertEquals('http://example.test/?param1=value1', |
|
| 207 | - $this->xUriDetector->redirect('http://example.test/? ', ['QUERY_STRING' => 'param1=value1'])); |
|
| 208 | - } |
|
| 128 | +public function testRemoveJaxonParam() |
|
| 129 | +{ |
|
| 130 | +$this->assertEquals('http://example.test/path', $this->xUriDetector->detect([ |
|
| 131 | +'REQUEST_URI' => 'http://example.test/path?jxnGenerate=true' |
|
| 132 | +])); |
|
| 133 | +$this->assertEquals('http://example.test/path?param1=value1¶m2=%22value2%22', |
|
| 134 | +$this->xUriDetector->detect([ |
|
| 135 | + 'REQUEST_URI' => 'http://example.test/path?param1=value1&jxnGenerate=true¶m2="value2"', |
|
| 136 | +]) |
|
| 137 | +); |
|
| 138 | +} |
|
| 139 | + |
|
| 140 | +public function testErrorMissingHost() |
|
| 141 | +{ |
|
| 142 | +$this->expectException(UriException::class); |
|
| 143 | +$this->xUriDetector->detect([ |
|
| 144 | +'HTTPS' => 'on', |
|
| 145 | +'PATH_INFO' => '/path', |
|
| 146 | +'QUERY_STRING' => 'param1=value1¶m2="value2"', |
|
| 147 | +]); |
|
| 148 | +} |
|
| 149 | + |
|
| 150 | +public function testRedirectSimpleUrl() |
|
| 151 | +{ |
|
| 152 | +$this->assertEquals('http://example.test/path', |
|
| 153 | +$this->xUriDetector->redirect('http://example.test/path', [])); |
|
| 154 | +} |
|
| 155 | + |
|
| 156 | +public function testRedirectUrlWithAnchor() |
|
| 157 | +{ |
|
| 158 | +$this->assertEquals('http://example.test/path?param=value#anchor', |
|
| 159 | +$this->xUriDetector->redirect('http://example.test/path?param=value#anchor', [])); |
|
| 160 | +} |
|
| 161 | + |
|
| 162 | +public function testRedirectUrlWithParam() |
|
| 163 | +{ |
|
| 164 | +$this->assertEquals('http://example.test/path?param=value', |
|
| 165 | +$this->xUriDetector->redirect('http://example.test/path?param=value', [])); |
|
| 166 | +} |
|
| 167 | + |
|
| 168 | +public function testRedirectUrlWithParams() |
|
| 169 | +{ |
|
| 170 | +$this->assertEquals('http://example.test/path?param1=value1¶m2=value2', |
|
| 171 | +$this->xUriDetector->redirect('http://example.test/path?param1=value1¶m2=value2', [])); |
|
| 172 | +} |
|
| 173 | + |
|
| 174 | +public function testRedirectUrlWithSpecialChars() |
|
| 175 | +{ |
|
| 176 | +$this->assertEquals('http://example.test/path?param1=%22value1%22¶m2=%25value2%25#anchor', |
|
| 177 | +$this->xUriDetector->redirect('http://example.test/path?param1="value1"¶m2=%value2%#anchor', [])); |
|
| 178 | +} |
|
| 179 | + |
|
| 180 | +public function testRedirectSpecialUrl() |
|
| 181 | +{ |
|
| 182 | +$this->assertEquals('http://example.test/?query1234', |
|
| 183 | +$this->xUriDetector->redirect('http://example.test/?query1234', [])); |
|
| 184 | + |
|
| 185 | +$this->assertEquals('http://example.test/?query1234=', |
|
| 186 | +$this->xUriDetector->redirect('http://example.test/?query1234=', [])); |
|
| 187 | + |
|
| 188 | +$this->assertEquals('http://example.test/?param=value&query1234=0', |
|
| 189 | +$this->xUriDetector->redirect('http://example.test/?param=value&query1234=0', [])); |
|
| 190 | +} |
|
| 191 | + |
|
| 192 | +public function testRedirectEncodedUrl() |
|
| 193 | +{ |
|
| 194 | +$this->assertEquals('http://example.test/path?param1=%22value1%22¶m2=%25value2%25', |
|
| 195 | +$this->xUriDetector->redirect('http://example.test/path?param1=%22value1%22¶m2=%25value2%25', [])); |
|
| 196 | +} |
|
| 197 | + |
|
| 198 | +public function testRedirectUnexpectedUrl() |
|
| 199 | +{ |
|
| 200 | +$this->assertEquals('http://example.test/', |
|
| 201 | +$this->xUriDetector->redirect('http://example.test/? ', [])); |
|
| 202 | +} |
|
| 203 | + |
|
| 204 | +public function testRedirectQueryString() |
|
| 205 | +{ |
|
| 206 | +$this->assertEquals('http://example.test/?param1=value1', |
|
| 207 | +$this->xUriDetector->redirect('http://example.test/? ', ['QUERY_STRING' => 'param1=value1'])); |
|
| 208 | +} |
|
| 209 | 209 | } |
@@ -29,40 +29,40 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | class UriDetector |
| 31 | 31 | { |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * The URL components |
| 34 | 34 | * |
| 35 | 35 | * @var array |
| 36 | 36 | */ |
| 37 | - protected $aUrl; |
|
| 37 | +protected $aUrl; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * @param array $aServerParams The server environment variables |
| 41 | 41 | * |
| 42 | 42 | * @return void |
| 43 | 43 | */ |
| 44 | - private function setScheme(array $aServerParams): void |
|
| 45 | - { |
|
| 46 | - if(isset($this->aUrl['scheme'])) |
|
| 47 | - { |
|
| 48 | - return; |
|
| 49 | - } |
|
| 50 | - if(isset($aServerParams['HTTP_SCHEME'])) |
|
| 51 | - { |
|
| 52 | - $this->aUrl['scheme'] = $aServerParams['HTTP_SCHEME']; |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - if((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 56 | - (isset($aServerParams['HTTP_X_FORWARDED_SSL']) && $aServerParams['HTTP_X_FORWARDED_SSL'] === 'on') || |
|
| 57 | - (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] === 'https')) |
|
| 58 | - { |
|
| 59 | - $this->aUrl['scheme'] = 'https'; |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - $this->aUrl['scheme'] = 'http'; |
|
| 63 | - } |
|
| 44 | +private function setScheme(array $aServerParams): void |
|
| 45 | +{ |
|
| 46 | +if(isset($this->aUrl['scheme'])) |
|
| 47 | +{ |
|
| 48 | +return; |
|
| 49 | +} |
|
| 50 | +if(isset($aServerParams['HTTP_SCHEME'])) |
|
| 51 | +{ |
|
| 52 | +$this->aUrl['scheme'] = $aServerParams['HTTP_SCHEME']; |
|
| 53 | +return; |
|
| 54 | +} |
|
| 55 | +if((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 56 | +(isset($aServerParams['HTTP_X_FORWARDED_SSL']) && $aServerParams['HTTP_X_FORWARDED_SSL'] === 'on') || |
|
| 57 | +(isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] === 'https')) |
|
| 58 | +{ |
|
| 59 | +$this->aUrl['scheme'] = 'https'; |
|
| 60 | +return; |
|
| 61 | +} |
|
| 62 | +$this->aUrl['scheme'] = 'http'; |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Get the URL from the $aServerParams var |
| 67 | 67 | * |
| 68 | 68 | * @param array $aServerParams The server environment variables |
@@ -70,140 +70,140 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return void |
| 72 | 72 | */ |
| 73 | - private function setHostFromServer(array $aServerParams, string $sKey): void |
|
| 74 | - { |
|
| 75 | - if(isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 76 | - { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - if(strpos($aServerParams[$sKey], ':') === false) |
|
| 80 | - { |
|
| 81 | - $this->aUrl['host'] = $aServerParams[$sKey]; |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - list($this->aUrl['host'], $this->aUrl['port']) = explode(':', $aServerParams[$sKey]); |
|
| 85 | - } |
|
| 73 | +private function setHostFromServer(array $aServerParams, string $sKey): void |
|
| 74 | +{ |
|
| 75 | +if(isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 76 | +{ |
|
| 77 | +return; |
|
| 78 | +} |
|
| 79 | +if(strpos($aServerParams[$sKey], ':') === false) |
|
| 80 | +{ |
|
| 81 | +$this->aUrl['host'] = $aServerParams[$sKey]; |
|
| 82 | +return; |
|
| 83 | +} |
|
| 84 | +list($this->aUrl['host'], $this->aUrl['port']) = explode(':', $aServerParams[$sKey]); |
|
| 85 | +} |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * @param array $aServerParams The server environment variables |
| 89 | 89 | * |
| 90 | 90 | * @return void |
| 91 | 91 | * @throws UriException |
| 92 | 92 | */ |
| 93 | - private function setHost(array $aServerParams): void |
|
| 94 | - { |
|
| 95 | - $this->setHostFromServer($aServerParams, 'HTTP_X_FORWARDED_HOST'); |
|
| 96 | - $this->setHostFromServer($aServerParams, 'HTTP_HOST'); |
|
| 97 | - $this->setHostFromServer($aServerParams, 'SERVER_NAME'); |
|
| 98 | - if(empty($this->aUrl['host'])) |
|
| 99 | - { |
|
| 100 | - throw new UriException(); |
|
| 101 | - } |
|
| 102 | - if(empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 103 | - { |
|
| 104 | - $this->aUrl['port'] = $aServerParams['SERVER_PORT']; |
|
| 105 | - } |
|
| 106 | - } |
|
| 93 | +private function setHost(array $aServerParams): void |
|
| 94 | +{ |
|
| 95 | +$this->setHostFromServer($aServerParams, 'HTTP_X_FORWARDED_HOST'); |
|
| 96 | +$this->setHostFromServer($aServerParams, 'HTTP_HOST'); |
|
| 97 | +$this->setHostFromServer($aServerParams, 'SERVER_NAME'); |
|
| 98 | +if(empty($this->aUrl['host'])) |
|
| 99 | +{ |
|
| 100 | +throw new UriException(); |
|
| 101 | +} |
|
| 102 | +if(empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 103 | +{ |
|
| 104 | +$this->aUrl['port'] = $aServerParams['SERVER_PORT']; |
|
| 105 | +} |
|
| 106 | +} |
|
| 107 | 107 | |
| 108 | - /** |
|
| 108 | +/** |
|
| 109 | 109 | * @param array $aServerParams The server environment variables |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - private function setPath(array $aServerParams): void |
|
| 114 | - { |
|
| 115 | - if(isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 116 | - { |
|
| 117 | - unset($this->aUrl['path']); |
|
| 118 | - } |
|
| 119 | - if(isset($this->aUrl['path'])) |
|
| 120 | - { |
|
| 121 | - return; |
|
| 122 | - } |
|
| 123 | - $aPath = parse_url($aServerParams['PATH_INFO'] ?? ($aServerParams['PHP_SELF'] ?? '')); |
|
| 124 | - if(isset($aPath['path'])) |
|
| 125 | - { |
|
| 126 | - $this->aUrl['path'] = $aPath['path']; |
|
| 127 | - } |
|
| 128 | - } |
|
| 113 | +private function setPath(array $aServerParams): void |
|
| 114 | +{ |
|
| 115 | +if(isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 116 | +{ |
|
| 117 | +unset($this->aUrl['path']); |
|
| 118 | +} |
|
| 119 | +if(isset($this->aUrl['path'])) |
|
| 120 | +{ |
|
| 121 | +return; |
|
| 122 | +} |
|
| 123 | +$aPath = parse_url($aServerParams['PATH_INFO'] ?? ($aServerParams['PHP_SELF'] ?? '')); |
|
| 124 | +if(isset($aPath['path'])) |
|
| 125 | +{ |
|
| 126 | +$this->aUrl['path'] = $aPath['path']; |
|
| 127 | +} |
|
| 128 | +} |
|
| 129 | 129 | |
| 130 | - /** |
|
| 130 | +/** |
|
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - private function getPath(): string |
|
| 134 | - { |
|
| 135 | - return '/' . ltrim($this->aUrl['path'], '/'); |
|
| 136 | - } |
|
| 133 | +private function getPath(): string |
|
| 134 | +{ |
|
| 135 | +return '/' . ltrim($this->aUrl['path'], '/'); |
|
| 136 | +} |
|
| 137 | 137 | |
| 138 | - /** |
|
| 138 | +/** |
|
| 139 | 139 | * @return string |
| 140 | 140 | */ |
| 141 | - private function getUser(): string |
|
| 142 | - { |
|
| 143 | - if(empty($this->aUrl['user'])) |
|
| 144 | - { |
|
| 145 | - return ''; |
|
| 146 | - } |
|
| 147 | - $sUrl = $this->aUrl['user']; |
|
| 148 | - if(isset($this->aUrl['pass'])) |
|
| 149 | - { |
|
| 150 | - $sUrl .= ':' . $this->aUrl['pass']; |
|
| 151 | - } |
|
| 152 | - return $sUrl . '@'; |
|
| 153 | - } |
|
| 141 | +private function getUser(): string |
|
| 142 | +{ |
|
| 143 | +if(empty($this->aUrl['user'])) |
|
| 144 | +{ |
|
| 145 | +return ''; |
|
| 146 | +} |
|
| 147 | +$sUrl = $this->aUrl['user']; |
|
| 148 | +if(isset($this->aUrl['pass'])) |
|
| 149 | +{ |
|
| 150 | +$sUrl .= ':' . $this->aUrl['pass']; |
|
| 151 | +} |
|
| 152 | +return $sUrl . '@'; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * @return string |
| 157 | 157 | */ |
| 158 | - private function getPort(): string |
|
| 159 | - { |
|
| 160 | - if(isset($this->aUrl['port']) && |
|
| 161 | - (($this->aUrl['scheme'] === 'http' && $this->aUrl['port'] != 80) || |
|
| 162 | - ($this->aUrl['scheme'] === 'https' && $this->aUrl['port'] != 443))) |
|
| 163 | - { |
|
| 164 | - return ':' . $this->aUrl['port']; |
|
| 165 | - } |
|
| 166 | - return ''; |
|
| 167 | - } |
|
| 158 | +private function getPort(): string |
|
| 159 | +{ |
|
| 160 | +if(isset($this->aUrl['port']) && |
|
| 161 | +(($this->aUrl['scheme'] === 'http' && $this->aUrl['port'] != 80) || |
|
| 162 | + ($this->aUrl['scheme'] === 'https' && $this->aUrl['port'] != 443))) |
|
| 163 | +{ |
|
| 164 | +return ':' . $this->aUrl['port']; |
|
| 165 | +} |
|
| 166 | +return ''; |
|
| 167 | +} |
|
| 168 | 168 | |
| 169 | - /** |
|
| 169 | +/** |
|
| 170 | 170 | * @param array $aServerParams The server environment variables |
| 171 | 171 | * |
| 172 | 172 | * @return void |
| 173 | 173 | */ |
| 174 | - private function setQuery(array $aServerParams): void |
|
| 175 | - { |
|
| 176 | - if(empty($this->aUrl['query'])) |
|
| 177 | - { |
|
| 178 | - $this->aUrl['query'] = $aServerParams['QUERY_STRING'] ?? ''; |
|
| 179 | - } |
|
| 180 | - } |
|
| 174 | +private function setQuery(array $aServerParams): void |
|
| 175 | +{ |
|
| 176 | +if(empty($this->aUrl['query'])) |
|
| 177 | +{ |
|
| 178 | +$this->aUrl['query'] = $aServerParams['QUERY_STRING'] ?? ''; |
|
| 179 | +} |
|
| 180 | +} |
|
| 181 | 181 | |
| 182 | - /** |
|
| 182 | +/** |
|
| 183 | 183 | * @return string |
| 184 | 184 | */ |
| 185 | - private function getQuery(): string |
|
| 186 | - { |
|
| 187 | - if(empty($this->aUrl['query'])) |
|
| 188 | - { |
|
| 189 | - return ''; |
|
| 190 | - } |
|
| 191 | - $aQueries = explode('&', $this->aUrl['query']); |
|
| 192 | - foreach($aQueries as $sKey => $sQuery) |
|
| 193 | - { |
|
| 194 | - if(substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 195 | - { |
|
| 196 | - unset($aQueries[$sKey]); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - if(empty($aQueries)) |
|
| 200 | - { |
|
| 201 | - return ''; |
|
| 202 | - } |
|
| 203 | - return '?' . implode("&", $aQueries); |
|
| 204 | - } |
|
| 185 | +private function getQuery(): string |
|
| 186 | +{ |
|
| 187 | +if(empty($this->aUrl['query'])) |
|
| 188 | +{ |
|
| 189 | +return ''; |
|
| 190 | +} |
|
| 191 | +$aQueries = explode('&', $this->aUrl['query']); |
|
| 192 | +foreach($aQueries as $sKey => $sQuery) |
|
| 193 | +{ |
|
| 194 | +if(substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 195 | +{ |
|
| 196 | + unset($aQueries[$sKey]); |
|
| 197 | +} |
|
| 198 | +} |
|
| 199 | +if(empty($aQueries)) |
|
| 200 | +{ |
|
| 201 | +return ''; |
|
| 202 | +} |
|
| 203 | +return '?' . implode("&", $aQueries); |
|
| 204 | +} |
|
| 205 | 205 | |
| 206 | - /** |
|
| 206 | +/** |
|
| 207 | 207 | * Detect the URI of the current request |
| 208 | 208 | * |
| 209 | 209 | * @param array $aServerParams The server environment variables |
@@ -211,70 +211,70 @@ discard block |
||
| 211 | 211 | * @return string |
| 212 | 212 | * @throws UriException |
| 213 | 213 | */ |
| 214 | - public function detect(array $aServerParams): string |
|
| 215 | - { |
|
| 216 | - $this->aUrl = []; |
|
| 217 | - // Try to get the request URL |
|
| 218 | - if(isset($aServerParams['REQUEST_URI'])) |
|
| 219 | - { |
|
| 220 | - $this->aUrl = parse_url($aServerParams['REQUEST_URI']); |
|
| 221 | - } |
|
| 214 | +public function detect(array $aServerParams): string |
|
| 215 | +{ |
|
| 216 | +$this->aUrl = []; |
|
| 217 | +// Try to get the request URL |
|
| 218 | +if(isset($aServerParams['REQUEST_URI'])) |
|
| 219 | +{ |
|
| 220 | +$this->aUrl = parse_url($aServerParams['REQUEST_URI']); |
|
| 221 | +} |
|
| 222 | 222 | |
| 223 | - // Fill in the empty values |
|
| 224 | - $this->setScheme($aServerParams); |
|
| 225 | - $this->setHost($aServerParams); |
|
| 226 | - $this->setPath($aServerParams); |
|
| 227 | - $this->setQuery($aServerParams); |
|
| 223 | +// Fill in the empty values |
|
| 224 | +$this->setScheme($aServerParams); |
|
| 225 | +$this->setHost($aServerParams); |
|
| 226 | +$this->setPath($aServerParams); |
|
| 227 | +$this->setQuery($aServerParams); |
|
| 228 | 228 | |
| 229 | - // Build the URL: Start with scheme, user and pass |
|
| 230 | - return $this->aUrl['scheme'] . '://' . $this->getUser() . $this->aUrl['host'] . |
|
| 231 | - $this->getPort() . str_replace(['"', "'", '<', '>'], |
|
| 232 | - ['%22', '%27', '%3C', '%3E'], $this->getPath() . $this->getQuery()); |
|
| 233 | - } |
|
| 229 | +// Build the URL: Start with scheme, user and pass |
|
| 230 | +return $this->aUrl['scheme'] . '://' . $this->getUser() . $this->aUrl['host'] . |
|
| 231 | +$this->getPort() . str_replace(['"', "'", '<', '>'], |
|
| 232 | + ['%22', '%27', '%3C', '%3E'], $this->getPath() . $this->getQuery()); |
|
| 233 | +} |
|
| 234 | 234 | |
| 235 | - /** |
|
| 235 | +/** |
|
| 236 | 236 | * @param string $sQueryPart |
| 237 | 237 | * @param string $sKey |
| 238 | 238 | * @param string $sValue |
| 239 | 239 | * |
| 240 | 240 | * @return string |
| 241 | 241 | */ |
| 242 | - private function makeQueryPart(string $sQueryPart, string $sKey, string $sValue): string |
|
| 243 | - { |
|
| 244 | - return $sValue === '' && strpos($sQueryPart, "$sKey=") === false ? |
|
| 245 | - rawurlencode($sKey) : rawurlencode($sKey) . '=' . rawurlencode($sValue); |
|
| 246 | - } |
|
| 242 | +private function makeQueryPart(string $sQueryPart, string $sKey, string $sValue): string |
|
| 243 | +{ |
|
| 244 | +return $sValue === '' && strpos($sQueryPart, "$sKey=") === false ? |
|
| 245 | +rawurlencode($sKey) : rawurlencode($sKey) . '=' . rawurlencode($sValue); |
|
| 246 | +} |
|
| 247 | 247 | |
| 248 | - /** |
|
| 248 | +/** |
|
| 249 | 249 | * @param string $sQueryPart |
| 250 | 250 | * @param array $aServerParams |
| 251 | 251 | * |
| 252 | 252 | * @return string |
| 253 | 253 | */ |
| 254 | - private function parseQueryPart(string $sQueryPart, array $aServerParams): string |
|
| 255 | - { |
|
| 256 | - $aQueryParts = []; |
|
| 257 | - parse_str($sQueryPart, $aQueryParts); |
|
| 258 | - if(empty($aQueryParts)) |
|
| 259 | - { |
|
| 260 | - // Couldn't break up the query, but there's one there. |
|
| 261 | - // Possibly "http://url/page.html?query1234" type of query? |
|
| 262 | - // Try to get data from the server environment var. |
|
| 263 | - parse_str($aServerParams['QUERY_STRING'] ?? '', $aQueryParts); |
|
| 264 | - } |
|
| 265 | - if(($aQueryParts)) |
|
| 266 | - { |
|
| 267 | - $aNewQueryParts = []; |
|
| 268 | - foreach($aQueryParts as $sKey => $sValue) |
|
| 269 | - { |
|
| 270 | - $aNewQueryParts[] = $this->makeQueryPart($sQueryPart, $sKey, $sValue); |
|
| 271 | - } |
|
| 272 | - return '?' . implode('&', $aNewQueryParts); |
|
| 273 | - } |
|
| 274 | - return trim($sQueryPart); |
|
| 275 | - } |
|
| 254 | +private function parseQueryPart(string $sQueryPart, array $aServerParams): string |
|
| 255 | +{ |
|
| 256 | +$aQueryParts = []; |
|
| 257 | +parse_str($sQueryPart, $aQueryParts); |
|
| 258 | +if(empty($aQueryParts)) |
|
| 259 | +{ |
|
| 260 | +// Couldn't break up the query, but there's one there. |
|
| 261 | +// Possibly "http://url/page.html?query1234" type of query? |
|
| 262 | +// Try to get data from the server environment var. |
|
| 263 | +parse_str($aServerParams['QUERY_STRING'] ?? '', $aQueryParts); |
|
| 264 | +} |
|
| 265 | +if(($aQueryParts)) |
|
| 266 | +{ |
|
| 267 | +$aNewQueryParts = []; |
|
| 268 | +foreach($aQueryParts as $sKey => $sValue) |
|
| 269 | +{ |
|
| 270 | + $aNewQueryParts[] = $this->makeQueryPart($sQueryPart, $sKey, $sValue); |
|
| 271 | +} |
|
| 272 | +return '?' . implode('&', $aNewQueryParts); |
|
| 273 | +} |
|
| 274 | +return trim($sQueryPart); |
|
| 275 | +} |
|
| 276 | 276 | |
| 277 | - /** |
|
| 277 | +/** |
|
| 278 | 278 | * Make the specified URL suitable for redirect |
| 279 | 279 | * |
| 280 | 280 | * @param string $sURL The relative or fully qualified URL |
@@ -282,23 +282,23 @@ discard block |
||
| 282 | 282 | * |
| 283 | 283 | * @return string |
| 284 | 284 | */ |
| 285 | - public function redirect(string $sURL, array $aServerParams): string |
|
| 286 | - { |
|
| 287 | - // We need to parse the query part so that the values are rawurlencode()'ed. |
|
| 288 | - // Can't just use parse_url() cos we could be dealing with a relative URL which parse_url() can't deal with. |
|
| 289 | - $sURL = trim($sURL); |
|
| 290 | - $nQueryStart = strpos($sURL, '?', strrpos($sURL, '/')); |
|
| 291 | - if($nQueryStart === false) |
|
| 292 | - { |
|
| 293 | - return $sURL; |
|
| 294 | - } |
|
| 295 | - $nQueryStart++; |
|
| 296 | - $nQueryEnd = strpos($sURL, '#', $nQueryStart); |
|
| 297 | - if($nQueryEnd === false) |
|
| 298 | - { |
|
| 299 | - $nQueryEnd = strlen($sURL); |
|
| 300 | - } |
|
| 301 | - $sQueryPart = substr($sURL, $nQueryStart, $nQueryEnd - $nQueryStart); |
|
| 302 | - return str_replace('?' . $sQueryPart, $this->parseQueryPart($sQueryPart, $aServerParams), $sURL); |
|
| 303 | - } |
|
| 285 | +public function redirect(string $sURL, array $aServerParams): string |
|
| 286 | +{ |
|
| 287 | +// We need to parse the query part so that the values are rawurlencode()'ed. |
|
| 288 | +// Can't just use parse_url() cos we could be dealing with a relative URL which parse_url() can't deal with. |
|
| 289 | +$sURL = trim($sURL); |
|
| 290 | +$nQueryStart = strpos($sURL, '?', strrpos($sURL, '/')); |
|
| 291 | +if($nQueryStart === false) |
|
| 292 | +{ |
|
| 293 | +return $sURL; |
|
| 294 | +} |
|
| 295 | +$nQueryStart++; |
|
| 296 | +$nQueryEnd = strpos($sURL, '#', $nQueryStart); |
|
| 297 | +if($nQueryEnd === false) |
|
| 298 | +{ |
|
| 299 | +$nQueryEnd = strlen($sURL); |
|
| 300 | +} |
|
| 301 | +$sQueryPart = substr($sURL, $nQueryStart, $nQueryEnd - $nQueryStart); |
|
| 302 | +return str_replace('?' . $sQueryPart, $this->parseQueryPart($sQueryPart, $aServerParams), $sURL); |
|
| 303 | +} |
|
| 304 | 304 | } |
@@ -24,45 +24,45 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | class FileMinifier |
| 26 | 26 | { |
| 27 | - /** |
|
| 27 | +/** |
|
| 28 | 28 | * Read the file content |
| 29 | 29 | * |
| 30 | 30 | * @param string $sPath |
| 31 | 31 | * |
| 32 | 32 | * @return string|false |
| 33 | 33 | */ |
| 34 | - private function readFile(string $sPath): bool|string |
|
| 35 | - { |
|
| 36 | - try |
|
| 37 | - { |
|
| 38 | - return file_get_contents($sPath); |
|
| 39 | - } |
|
| 40 | - catch(Exception $e) |
|
| 41 | - { |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - } |
|
| 34 | +private function readFile(string $sPath): bool|string |
|
| 35 | +{ |
|
| 36 | +try |
|
| 37 | +{ |
|
| 38 | +return file_get_contents($sPath); |
|
| 39 | +} |
|
| 40 | +catch(Exception $e) |
|
| 41 | +{ |
|
| 42 | +return false; |
|
| 43 | +} |
|
| 44 | +} |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * Read the file content |
| 48 | 48 | * |
| 49 | 49 | * @param string $sCode |
| 50 | 50 | * |
| 51 | 51 | * @return string|false |
| 52 | 52 | */ |
| 53 | - private function minifyCode(string $sCode): bool|string |
|
| 54 | - { |
|
| 55 | - try |
|
| 56 | - { |
|
| 57 | - return Minifier::minify($sCode); |
|
| 58 | - } |
|
| 59 | - catch(Exception $e) |
|
| 60 | - { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - } |
|
| 53 | +private function minifyCode(string $sCode): bool|string |
|
| 54 | +{ |
|
| 55 | +try |
|
| 56 | +{ |
|
| 57 | +return Minifier::minify($sCode); |
|
| 58 | +} |
|
| 59 | +catch(Exception $e) |
|
| 60 | +{ |
|
| 61 | +return false; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Minify javascript code |
| 67 | 67 | * |
| 68 | 68 | * @param string $sJsFile The javascript file to be minified |
@@ -70,14 +70,14 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return bool |
| 72 | 72 | */ |
| 73 | - public function minify(string $sJsFile, string $sMinFile): bool |
|
| 74 | - { |
|
| 75 | - if(($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 76 | - ($sMinCode = $this->minifyCode($sJsCode)) === false) |
|
| 77 | - { |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - file_put_contents($sMinFile, $sMinCode); |
|
| 81 | - return is_file($sMinFile); |
|
| 82 | - } |
|
| 73 | +public function minify(string $sJsFile, string $sMinFile): bool |
|
| 74 | +{ |
|
| 75 | +if(($sJsCode = $this->readFile($sJsFile)) === false || |
|
| 76 | +($sMinCode = $this->minifyCode($sJsCode)) === false) |
|
| 77 | +{ |
|
| 78 | +return false; |
|
| 79 | +} |
|
| 80 | +file_put_contents($sMinFile, $sMinCode); |
|
| 81 | +return is_file($sMinFile); |
|
| 82 | +} |
|
| 83 | 83 | } |
@@ -30,54 +30,54 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Context |
| 32 | 32 | { |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * @var Context|null |
| 35 | 35 | */ |
| 36 | - private $__extends__ = null; |
|
| 36 | +private $__extends__ = null; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @var string |
| 40 | 40 | */ |
| 41 | - private $__block_name__; |
|
| 41 | +private $__block_name__; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * @var array |
| 45 | 45 | */ |
| 46 | - private $__properties__ = []; |
|
| 46 | +private $__properties__ = []; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * The constructor |
| 50 | 50 | * |
| 51 | 51 | * @param array $__namespaces__ |
| 52 | 52 | * @param string $__default_namespace__ |
| 53 | 53 | * @param string $__template__ |
| 54 | 54 | */ |
| 55 | - public function __construct(protected array $__namespaces__, |
|
| 56 | - protected string $__default_namespace__, protected string $__template__) |
|
| 57 | - {} |
|
| 55 | +public function __construct(protected array $__namespaces__, |
|
| 56 | +protected string $__default_namespace__, protected string $__template__) |
|
| 57 | +{} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @param string $name |
| 61 | 61 | * |
| 62 | 62 | * @return mixed |
| 63 | 63 | */ |
| 64 | - public function __get(string $name): mixed |
|
| 65 | - { |
|
| 66 | - return $this->__properties__[$name] ?? ''; |
|
| 67 | - } |
|
| 64 | +public function __get(string $name): mixed |
|
| 65 | +{ |
|
| 66 | +return $this->__properties__[$name] ?? ''; |
|
| 67 | +} |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * @param string $name |
| 71 | 71 | * @param mixed $value |
| 72 | 72 | * |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - public function __set(string $name, $value): void |
|
| 76 | - { |
|
| 77 | - $this->__properties__[$name] = $value; |
|
| 78 | - } |
|
| 75 | +public function __set(string $name, $value): void |
|
| 76 | +{ |
|
| 77 | +$this->__properties__[$name] = $value; |
|
| 78 | +} |
|
| 79 | 79 | |
| 80 | - /** |
|
| 80 | +/** |
|
| 81 | 81 | * Include a template |
| 82 | 82 | * |
| 83 | 83 | * @param string $template The name of template to be rendered |
@@ -85,110 +85,110 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return void |
| 87 | 87 | */ |
| 88 | - protected function include(string $template, array $vars = []): void |
|
| 89 | - { |
|
| 90 | - $context = new Context($this->__namespaces__, |
|
| 91 | - $this->__default_namespace__, $template); |
|
| 92 | - echo $context->__render($vars); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 88 | +protected function include(string $template, array $vars = []): void |
|
| 89 | +{ |
|
| 90 | +$context = new Context($this->__namespaces__, |
|
| 91 | +$this->__default_namespace__, $template); |
|
| 92 | +echo $context->__render($vars); |
|
| 93 | +} |
|
| 94 | + |
|
| 95 | +/** |
|
| 96 | 96 | * @param string $template The name of template to be rendered |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function extends(string $template): void |
|
| 101 | - { |
|
| 102 | - $this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | - $this->__default_namespace__, $template); |
|
| 104 | - } |
|
| 100 | +public function extends(string $template): void |
|
| 101 | +{ |
|
| 102 | +$this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | +$this->__default_namespace__, $template); |
|
| 104 | +} |
|
| 105 | 105 | |
| 106 | - /** |
|
| 106 | +/** |
|
| 107 | 107 | * Start a new block |
| 108 | 108 | * |
| 109 | 109 | * @param string $name |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - public function block(string $name): void |
|
| 114 | - { |
|
| 115 | - ob_start(); |
|
| 116 | - $this->__block_name__ = $name; |
|
| 117 | - } |
|
| 113 | +public function block(string $name): void |
|
| 114 | +{ |
|
| 115 | +ob_start(); |
|
| 116 | +$this->__block_name__ = $name; |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * End the current block |
| 121 | 121 | * |
| 122 | 122 | * @param Closure|null $filter |
| 123 | 123 | * |
| 124 | 124 | * @return void |
| 125 | 125 | */ |
| 126 | - public function endblock(?Closure $filter = null): void |
|
| 127 | - { |
|
| 128 | - $content = ob_get_clean(); |
|
| 129 | - $this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | - } |
|
| 126 | +public function endblock(?Closure $filter = null): void |
|
| 127 | +{ |
|
| 128 | +$content = ob_get_clean(); |
|
| 129 | +$this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | +} |
|
| 131 | 131 | |
| 132 | - /** |
|
| 132 | +/** |
|
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - private function __path(): string |
|
| 136 | - { |
|
| 137 | - $template = trim($this->__template__); |
|
| 138 | - // Get the namespace name |
|
| 139 | - $namespace = $this->__default_namespace__; |
|
| 140 | - $separatorPosition = strrpos($template, '::'); |
|
| 141 | - if($separatorPosition !== false) |
|
| 142 | - { |
|
| 143 | - $namespace = substr($template, 0, $separatorPosition); |
|
| 144 | - $template = substr($template, $separatorPosition + 2); |
|
| 145 | - } |
|
| 146 | - // Check if the namespace is defined |
|
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | - { |
|
| 149 | - return $template; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $namespace = $this->__namespaces__[$namespace]; |
|
| 153 | - // Get the template path |
|
| 154 | - return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 135 | +private function __path(): string |
|
| 136 | +{ |
|
| 137 | +$template = trim($this->__template__); |
|
| 138 | +// Get the namespace name |
|
| 139 | +$namespace = $this->__default_namespace__; |
|
| 140 | +$separatorPosition = strrpos($template, '::'); |
|
| 141 | +if($separatorPosition !== false) |
|
| 142 | +{ |
|
| 143 | +$namespace = substr($template, 0, $separatorPosition); |
|
| 144 | +$template = substr($template, $separatorPosition + 2); |
|
| 145 | +} |
|
| 146 | +// Check if the namespace is defined |
|
| 147 | +if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | +{ |
|
| 149 | +return $template; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +$namespace = $this->__namespaces__[$namespace]; |
|
| 153 | +// Get the template path |
|
| 154 | +return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | +} |
|
| 156 | + |
|
| 157 | +/** |
|
| 158 | 158 | * Render a template |
| 159 | 159 | * |
| 160 | 160 | * @param array $vars The template vars |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - public function __render(array $vars): string |
|
| 165 | - { |
|
| 166 | - // Get the template path |
|
| 167 | - $templatePath = $this->__path(); |
|
| 168 | - if(!@is_readable($templatePath)) |
|
| 169 | - { |
|
| 170 | - return ''; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Save the template properties. |
|
| 174 | - foreach($vars as $name => $value) |
|
| 175 | - { |
|
| 176 | - $this->__set((string)$name, $value); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Render the template |
|
| 180 | - $renderer = function() use($templatePath) { |
|
| 181 | - ob_start(); |
|
| 182 | - include $templatePath; |
|
| 183 | - $content = ob_get_clean(); |
|
| 184 | - |
|
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 187 | - $this->__extends__->__render($this->__properties__); |
|
| 188 | - }; |
|
| 189 | - |
|
| 190 | - // Call the closure in the context of this object. |
|
| 191 | - // So the keyword '$this' in the template will refer to this object. |
|
| 192 | - return call_user_func($renderer->bindTo($this)); |
|
| 193 | - } |
|
| 164 | +public function __render(array $vars): string |
|
| 165 | +{ |
|
| 166 | +// Get the template path |
|
| 167 | +$templatePath = $this->__path(); |
|
| 168 | +if(!@is_readable($templatePath)) |
|
| 169 | +{ |
|
| 170 | +return ''; |
|
| 171 | +} |
|
| 172 | + |
|
| 173 | +// Save the template properties. |
|
| 174 | +foreach($vars as $name => $value) |
|
| 175 | +{ |
|
| 176 | +$this->__set((string)$name, $value); |
|
| 177 | +} |
|
| 178 | + |
|
| 179 | +// Render the template |
|
| 180 | +$renderer = function() use($templatePath) { |
|
| 181 | +ob_start(); |
|
| 182 | +include $templatePath; |
|
| 183 | +$content = ob_get_clean(); |
|
| 184 | + |
|
| 185 | +return $this->__extends__ === null ? $content : |
|
| 186 | + // Render the extended template with the same properties. |
|
| 187 | + $this->__extends__->__render($this->__properties__); |
|
| 188 | +}; |
|
| 189 | + |
|
| 190 | +// Call the closure in the context of this object. |
|
| 191 | +// So the keyword '$this' in the template will refer to this object. |
|
| 192 | +return call_user_func($renderer->bindTo($this)); |
|
| 193 | +} |
|
| 194 | 194 | } |
@@ -19,33 +19,33 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | class TemplateEngine |
| 21 | 21 | { |
| 22 | - /** |
|
| 22 | +/** |
|
| 23 | 23 | * The default namespace |
| 24 | 24 | * |
| 25 | 25 | * @var string |
| 26 | 26 | */ |
| 27 | - protected $sDefaultNamespace = ''; |
|
| 27 | +protected $sDefaultNamespace = ''; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The namespaces |
| 31 | 31 | * |
| 32 | 32 | * @var array |
| 33 | 33 | */ |
| 34 | - protected $aNamespaces; |
|
| 34 | +protected $aNamespaces; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * Set the default namespace |
| 38 | 38 | * |
| 39 | 39 | * @param string $sDefaultNamespace |
| 40 | 40 | * |
| 41 | 41 | * @return void |
| 42 | 42 | */ |
| 43 | - public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 44 | - { |
|
| 45 | - $this->sDefaultNamespace = $sDefaultNamespace; |
|
| 46 | - } |
|
| 43 | +public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 44 | +{ |
|
| 45 | +$this->sDefaultNamespace = $sDefaultNamespace; |
|
| 46 | +} |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * Add a namespace to the template system |
| 50 | 50 | * |
| 51 | 51 | * @param string $sNamespace The namespace name |
@@ -54,16 +54,16 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return static |
| 56 | 56 | */ |
| 57 | - public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): static |
|
| 58 | - { |
|
| 59 | - $this->aNamespaces[$sNamespace] = [ |
|
| 60 | - 'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
|
| 61 | - 'extension' => $sExtension, |
|
| 62 | - ]; |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 57 | +public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): static |
|
| 58 | +{ |
|
| 59 | +$this->aNamespaces[$sNamespace] = [ |
|
| 60 | +'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
|
| 61 | +'extension' => $sExtension, |
|
| 62 | +]; |
|
| 63 | +return $this; |
|
| 64 | +} |
|
| 65 | 65 | |
| 66 | - /** |
|
| 66 | +/** |
|
| 67 | 67 | * Render a template |
| 68 | 68 | * |
| 69 | 69 | * @param string $sTemplate The name of template to be rendered |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return string |
| 73 | 73 | */ |
| 74 | - public function render(string $sTemplate, array $aVars = []): string |
|
| 75 | - { |
|
| 76 | - $context = new Context($this->aNamespaces, $this->sDefaultNamespace, $sTemplate); |
|
| 77 | - return $context->__render($aVars); |
|
| 78 | - } |
|
| 74 | +public function render(string $sTemplate, array $aVars = []): string |
|
| 75 | +{ |
|
| 76 | +$context = new Context($this->aNamespaces, $this->sDefaultNamespace, $sTemplate); |
|
| 77 | +return $context->__render($aVars); |
|
| 78 | +} |
|
| 79 | 79 | } |
@@ -26,43 +26,43 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class Translator |
| 28 | 28 | { |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The default locale |
| 31 | 31 | * |
| 32 | 32 | * @var string |
| 33 | 33 | */ |
| 34 | - protected $sDefaultLocale = 'en'; |
|
| 34 | +protected $sDefaultLocale = 'en'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * The translations in a flattened array |
| 38 | 38 | * |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - protected $aTranslations = []; |
|
| 41 | +protected $aTranslations = []; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * The translations as received as input |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $aRawTranslations = []; |
|
| 48 | +protected $aRawTranslations = []; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Set the default locale |
| 52 | 52 | * |
| 53 | 53 | * @param string $sLocale |
| 54 | 54 | * |
| 55 | 55 | * @return void |
| 56 | 56 | */ |
| 57 | - public function setLocale(string $sLocale): void |
|
| 58 | - { |
|
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 60 | - { |
|
| 61 | - $this->sDefaultLocale = $sLocale; |
|
| 62 | - } |
|
| 63 | - } |
|
| 57 | +public function setLocale(string $sLocale): void |
|
| 58 | +{ |
|
| 59 | +if(($sLocale = trim($sLocale))) |
|
| 60 | +{ |
|
| 61 | +$this->sDefaultLocale = $sLocale; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Recursively load translated strings from an array |
| 67 | 67 | * |
| 68 | 68 | * @param string $sLanguage The language of the translations |
@@ -71,26 +71,26 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return void |
| 73 | 73 | */ |
| 74 | - private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | - { |
|
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | - { |
|
| 78 | - $sKey = trim($sKey); |
|
| 79 | - $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | - if(is_array($xTranslation)) |
|
| 81 | - { |
|
| 82 | - // Recursively read the translations in the array |
|
| 83 | - $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | - } |
|
| 85 | - else |
|
| 86 | - { |
|
| 87 | - // Save this translation |
|
| 88 | - $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 74 | +private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | +{ |
|
| 76 | +foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | +{ |
|
| 78 | +$sKey = trim($sKey); |
|
| 79 | +$sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | +if(is_array($xTranslation)) |
|
| 81 | +{ |
|
| 82 | + // Recursively read the translations in the array |
|
| 83 | + $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | +} |
|
| 85 | +else |
|
| 86 | +{ |
|
| 87 | + // Save this translation |
|
| 88 | + $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | +} |
|
| 90 | +} |
|
| 91 | +} |
|
| 92 | 92 | |
| 93 | - /** |
|
| 93 | +/** |
|
| 94 | 94 | * Load translated strings from a file |
| 95 | 95 | * |
| 96 | 96 | * @param string $sFilePath The file full path |
@@ -98,30 +98,30 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | - { |
|
| 103 | - if(!file_exists($sFilePath)) |
|
| 104 | - { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - $aTranslations = require($sFilePath); |
|
| 108 | - if(!is_array($aTranslations)) |
|
| 109 | - { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 101 | +public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | +{ |
|
| 103 | +if(!file_exists($sFilePath)) |
|
| 104 | +{ |
|
| 105 | +return false; |
|
| 106 | +} |
|
| 107 | +$aTranslations = require($sFilePath); |
|
| 108 | +if(!is_array($aTranslations)) |
|
| 109 | +{ |
|
| 110 | +return false; |
|
| 111 | +} |
|
| 112 | 112 | |
| 113 | - // Load the translations |
|
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | - { |
|
| 116 | - $this->aTranslations[$sLanguage] = []; |
|
| 117 | - } |
|
| 118 | - $this->aRawTranslations[$sLanguage] = |
|
| 119 | - array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | - $this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 113 | +// Load the translations |
|
| 114 | +if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | +{ |
|
| 116 | +$this->aTranslations[$sLanguage] = []; |
|
| 117 | +} |
|
| 118 | +$this->aRawTranslations[$sLanguage] = |
|
| 119 | +array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | +$this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | +return true; |
|
| 122 | +} |
|
| 123 | 123 | |
| 124 | - /** |
|
| 124 | +/** |
|
| 125 | 125 | * Get a translated string |
| 126 | 126 | * |
| 127 | 127 | * @param string $sText The key of the translated string |
@@ -130,29 +130,29 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | - { |
|
| 135 | - $sText = trim($sText); |
|
| 136 | - if(empty($sLanguage)) |
|
| 137 | - { |
|
| 138 | - $sLanguage = $this->sDefaultLocale; |
|
| 139 | - } |
|
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | - { |
|
| 142 | - return $sText; |
|
| 143 | - } |
|
| 144 | - $sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | - if(!empty($aPlaceHolders)) |
|
| 146 | - { |
|
| 147 | - $aVars = array_map(function($sVar) { |
|
| 148 | - return ':' . $sVar; |
|
| 149 | - }, array_keys($aPlaceHolders)); |
|
| 150 | - $sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | - } |
|
| 152 | - return $sMessage; |
|
| 153 | - } |
|
| 133 | +public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | +{ |
|
| 135 | +$sText = trim($sText); |
|
| 136 | +if(empty($sLanguage)) |
|
| 137 | +{ |
|
| 138 | +$sLanguage = $this->sDefaultLocale; |
|
| 139 | +} |
|
| 140 | +if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | +{ |
|
| 142 | +return $sText; |
|
| 143 | +} |
|
| 144 | +$sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | +if(!empty($aPlaceHolders)) |
|
| 146 | +{ |
|
| 147 | +$aVars = array_map(function($sVar) { |
|
| 148 | + return ':' . $sVar; |
|
| 149 | +}, array_keys($aPlaceHolders)); |
|
| 150 | +$sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | +} |
|
| 152 | +return $sMessage; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Get all the translations under a given key |
| 157 | 157 | * |
| 158 | 158 | * @param string $sKey |
@@ -160,22 +160,22 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @return array |
| 162 | 162 | */ |
| 163 | - public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | - { |
|
| 165 | - if(empty($sLanguage)) |
|
| 166 | - { |
|
| 167 | - $sLanguage = $this->sDefaultLocale; |
|
| 168 | - } |
|
| 169 | - $aKeys = explode('.', $sKey); |
|
| 163 | +public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | +{ |
|
| 165 | +if(empty($sLanguage)) |
|
| 166 | +{ |
|
| 167 | +$sLanguage = $this->sDefaultLocale; |
|
| 168 | +} |
|
| 169 | +$aKeys = explode('.', $sKey); |
|
| 170 | 170 | |
| 171 | - $aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | - foreach($aKeys as $sKey) |
|
| 173 | - { |
|
| 174 | - if($sKey !== '') |
|
| 175 | - { |
|
| 176 | - $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - return $aTranslations; |
|
| 180 | - } |
|
| 171 | +$aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | +foreach($aKeys as $sKey) |
|
| 173 | +{ |
|
| 174 | +if($sKey !== '') |
|
| 175 | +{ |
|
| 176 | + $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | +} |
|
| 178 | +} |
|
| 179 | +return $aTranslations; |
|
| 180 | +} |
|
| 181 | 181 | } |