فهرست منبع

add UrlBuilder service (with doc and tests)

Olivier Massot 4 سال پیش
والد
کامیت
c43895160d
2فایلهای تغییر یافته به همراه140 افزوده شده و 0 حذف شده
  1. 79 0
      src/Service/Utils/UrlBuilder.php
  2. 61 0
      tests/Service/Utils/UrlBuilderTest.php

+ 79 - 0
src/Service/Utils/UrlBuilder.php

@@ -0,0 +1,79 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Utils;
+
+/**
+ * Building url utilities
+ */
+class UrlBuilder
+{
+    /**
+     * Concatenate a base url and a path
+     *
+     * @param string $url The base url
+     * @param string $path The following path
+     * @return string
+     */
+    public static function concatPath(string $url, string $path): string {
+        return rtrim($url, '/') . '/' . ltrim($path, '/');
+    }
+
+    /**
+     * Concatenate an url and a list of parameters
+     *
+     * @param string $url
+     * @param array $parameters
+     * @return string
+     */
+    public static function concatParameters(string $url, array $parameters = []): string {
+        if (!$parameters) { return $url; }
+        $url = rtrim($url, '?&/');
+        $query = join(
+            '&',
+            array_map(
+                function ($k, $v = '') {
+                    $q = urlencode((string)$k);
+                    if ((string)$v) {
+                        $q .= '=' . urlencode((string)$v);
+                    }
+                    return $q;
+                },
+                array_keys($parameters),
+                array_values($parameters)
+            )
+        );
+        return $url . (str_contains($url, '?') ?  '&' : '?') . $query;
+    }
+
+    /**
+     * Prepend the 'https://' part if neither 'http://' of 'https://' is present, does nothing else
+     *
+     * @param $url
+     * @return string
+     */
+    public static function preprendHttps($url): string
+    {
+        if (!preg_match('/^https?:\/\/.*/', $url)) {
+            $url = 'https://' . $url;
+        }
+        return $url;
+    }
+
+    /**
+     * Build an url
+     *
+     * @param string $url The base url
+     * @param string $path A path to append (can be an empty string)
+     * @param array $parameters A list of parameters (can be an empty array)
+     * @param bool $preprendHttps Should the 'https://' be prepended if missing
+     * @return string
+     */
+    public static function concat(string $url, string $path, array $parameters, bool $preprendHttps = false): string {
+        $url = self::concatParameters(self::concatPath($url, $path), $parameters);
+        if ($preprendHttps) {
+            $url = self::preprendHttps($url);
+        }
+        return $url;
+    }
+}

+ 61 - 0
tests/Service/Utils/UrlBuilderTest.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Tests\Service\Utils;
+
+use App\Service\Utils\UrlBuilder;
+use PHPUnit\Framework\TestCase;
+
+class UrlBuilderTest extends TestCase
+{
+    public function testConcatPath() {
+        $this->assertEquals(
+            'https://domain.org/abc/def',
+            UrlBuilder::concatPath('https://domain.org', 'abc/def')
+        );
+        $this->assertEquals(
+            'https://domain.org/abc/def',
+            UrlBuilder::concatPath('https://domain.org/', '/abc/def')
+        );
+        $this->assertEquals(
+            'https://domain.org/',
+            UrlBuilder::concatPath('https://domain.org', '')
+        );
+    }
+
+    public function testConcatParameters() {
+        $this->assertEquals(
+            'https://domain.org/abc',
+            UrlBuilder::concatParameters('https://domain.org/abc', [])
+        );
+        $this->assertEquals(
+            'https://domain.org/abc?a=1&b&c=d%3De',
+            UrlBuilder::concatParameters('https://domain.org/abc', ['a' => 1, 'b' => '', 'c' => 'd=e'])
+        );
+        $this->assertEquals(
+            'https://domain.org/abc?a=1&b=2',
+            UrlBuilder::concatParameters('https://domain.org/abc?a=1', ['b' => 2])
+        );
+    }
+
+    public function testPrependHttps() {
+        $this->assertEquals(
+            'https://domain.org/abc',
+            UrlBuilder::preprendHttps('https://domain.org/abc')
+        );
+        $this->assertEquals(
+            'http://domain.org/abc',
+            UrlBuilder::preprendHttps('http://domain.org/abc')
+        );
+        $this->assertEquals(
+            'https://domain.org/abc',
+            UrlBuilder::preprendHttps('domain.org/abc')
+        );
+    }
+
+    public function testConcat() {
+        $this->assertEquals(
+            'https://domain.org/abc?a=1',
+            UrlBuilder::concat('domain.org', 'abc', ['a' => 1], true)
+        );
+    }
+}