summaryrefslogtreecommitdiffstats
path: root/vendor/jfcherng/php-diff/src/Utility
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/jfcherng/php-diff/src/Utility')
-rw-r--r--vendor/jfcherng/php-diff/src/Utility/Arr.php65
-rw-r--r--vendor/jfcherng/php-diff/src/Utility/Language.php131
-rw-r--r--vendor/jfcherng/php-diff/src/Utility/ReverseIterator.php51
-rw-r--r--vendor/jfcherng/php-diff/src/Utility/Str.php30
4 files changed, 277 insertions, 0 deletions
diff --git a/vendor/jfcherng/php-diff/src/Utility/Arr.php b/vendor/jfcherng/php-diff/src/Utility/Arr.php
new file mode 100644
index 0000000..97dccf0
--- /dev/null
+++ b/vendor/jfcherng/php-diff/src/Utility/Arr.php
@@ -0,0 +1,65 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Jfcherng\Diff\Utility;
+
+final class Arr
+{
+ /**
+ * Get a partial array slice with start/end indexes.
+ *
+ * @param array $array the array
+ * @param int $start the starting index (negative = count from backward)
+ * @param null|int $end the ending index (negative = count from backward)
+ * if is null, it returns a slice from $start to the end
+ *
+ * @return array array of all of the lines between the specified range
+ */
+ public static function getPartialByIndex(array $array, int $start = 0, ?int $end = null): array
+ {
+ $count = \count($array);
+
+ // make $end set
+ $end = $end ?? $count;
+
+ // make $start non-negative
+ if ($start < 0) {
+ $start += $count;
+
+ if ($start < 0) {
+ $start = 0;
+ }
+ }
+
+ // make $end non-negative
+ if ($end < 0) {
+ $end += $count;
+
+ if ($end < 0) {
+ $end = 0;
+ }
+ }
+
+ // make the length non-negative
+ return \array_slice($array, $start, \max(0, $end - $start));
+ }
+
+ /**
+ * Determines whether the array is associative.
+ *
+ * @param array $arr the array
+ *
+ * @return bool `true` if the array is associative, `false` otherwise
+ */
+ public static function isAssociative($arr): bool
+ {
+ foreach ($arr as $key => $value) {
+ if (\is_string($key)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/vendor/jfcherng/php-diff/src/Utility/Language.php b/vendor/jfcherng/php-diff/src/Utility/Language.php
new file mode 100644
index 0000000..cbd415f
--- /dev/null
+++ b/vendor/jfcherng/php-diff/src/Utility/Language.php
@@ -0,0 +1,131 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Jfcherng\Diff\Utility;
+
+final class Language
+{
+ /**
+ * @var string[] the translation dict
+ */
+ private $translations = [];
+
+ /**
+ * @var string the language name
+ */
+ private $language = '_custom_';
+
+ /**
+ * The constructor.
+ *
+ * @param array<int,string|string[]>|string|string[] $target the language ID or translations dict
+ */
+ public function __construct($target = 'eng')
+ {
+ $this->load($target);
+ }
+
+ /**
+ * Gets the language.
+ *
+ * @return string the language
+ */
+ public function getLanguage(): string
+ {
+ return $this->language;
+ }
+
+ /**
+ * Gets the translations.
+ *
+ * @return array the translations
+ */
+ public function getTranslations(): array
+ {
+ return $this->translations;
+ }
+
+ /**
+ * Loads the target language.
+ *
+ * @param array<int,string|string[]>|string|string[] $target the language ID or translations dict
+ */
+ public function load($target): void
+ {
+ $this->translations = $this->resolve($target);
+ $this->language = \is_string($target) ? $target : '_custom_';
+ }
+
+ /**
+ * Translates the text.
+ *
+ * @param string $text the text
+ */
+ public function translate(string $text): string
+ {
+ return $this->translations[$text] ?? "![{$text}]";
+ }
+
+ /**
+ * Get the translations from the language file.
+ *
+ * @param string $language the language
+ *
+ * @throws \Exception fail to decode the JSON file
+ * @throws \LogicException path is a directory
+ * @throws \RuntimeException path cannot be opened
+ *
+ * @return string[]
+ */
+ private static function getTranslationsByLanguage(string $language): array
+ {
+ $filePath = __DIR__ . "/../languages/{$language}.json";
+ $file = new \SplFileObject($filePath, 'r');
+ $fileContent = $file->fread($file->getSize());
+
+ /** @todo PHP ^7.3 JSON_THROW_ON_ERROR */
+ $decoded = \json_decode($fileContent, true);
+
+ if (\json_last_error() !== \JSON_ERROR_NONE) {
+ $msg = \sprintf('Fail to decode JSON file (code %d): %s', \json_last_error(), \realpath($filePath));
+ throw new \Exception($msg); // workaround single-line throw + 120-char limit
+ }
+
+ return (array) $decoded;
+ }
+
+ /**
+ * Resolves the target language.
+ *
+ * @param array<int,string|string[]>|string|string[] $target the language ID or translations array
+ *
+ * @throws \InvalidArgumentException
+ *
+ * @return string[] the resolved translations
+ */
+ private function resolve($target): array
+ {
+ if (\is_string($target)) {
+ return self::getTranslationsByLanguage($target);
+ }
+
+ if (\is_array($target)) {
+ // $target is an associative array
+ if (Arr::isAssociative($target)) {
+ return $target;
+ }
+
+ // $target is a list of "key-value pairs or language ID"
+ return \array_reduce(
+ $target,
+ function ($carry, $translation) {
+ return \array_merge($carry, $this->resolve($translation));
+ },
+ []
+ );
+ }
+
+ throw new \InvalidArgumentException('$target is not in valid form');
+ }
+}
diff --git a/vendor/jfcherng/php-diff/src/Utility/ReverseIterator.php b/vendor/jfcherng/php-diff/src/Utility/ReverseIterator.php
new file mode 100644
index 0000000..e684ae1
--- /dev/null
+++ b/vendor/jfcherng/php-diff/src/Utility/ReverseIterator.php
@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Jfcherng\Diff\Utility;
+
+final class ReverseIterator
+{
+ public const ITERATOR_GET_VALUE = 0;
+ public const ITERATOR_GET_KEY = 1 << 0;
+ public const ITERATOR_GET_BOTH = 1 << 1;
+
+ /**
+ * The constructor.
+ */
+ private function __construct()
+ {
+ }
+
+ /**
+ * Iterate the array reversely.
+ *
+ * @param array $array the array
+ * @param int $flags the flags
+ */
+ public static function fromArray(array $array, int $flags = self::ITERATOR_GET_VALUE): \Generator
+ {
+ // iterate [key => value] pair
+ if ($flags & self::ITERATOR_GET_BOTH) {
+ for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
+ yield $key => \current($array);
+ }
+
+ return;
+ }
+
+ // iterate only key
+ if ($flags & self::ITERATOR_GET_KEY) {
+ for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
+ yield $key;
+ }
+
+ return;
+ }
+
+ // iterate only value
+ for (\end($array); \key($array) !== null; \prev($array)) {
+ yield \current($array);
+ }
+ }
+}
diff --git a/vendor/jfcherng/php-diff/src/Utility/Str.php b/vendor/jfcherng/php-diff/src/Utility/Str.php
new file mode 100644
index 0000000..c7478d4
--- /dev/null
+++ b/vendor/jfcherng/php-diff/src/Utility/Str.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Jfcherng\Diff\Utility;
+
+final class Str
+{
+ /**
+ * Determine if a given string starts with a given substring.
+ *
+ * @param string $haystack the haystack
+ * @param string $needle the needle
+ */
+ public static function startsWith(string $haystack, string $needle): bool
+ {
+ return \substr($haystack, 0, \strlen($needle)) === $needle;
+ }
+
+ /**
+ * Determine if a given string ends with a given substring.
+ *
+ * @param string $haystack the haystack
+ * @param string $needle the needle
+ */
+ public static function endsWith(string $haystack, string $needle): bool
+ {
+ return \substr($haystack, -\strlen($needle)) === $needle;
+ }
+}