From ce02ea5c81d24102075bd7d3b0fcb20209dab865 Mon Sep 17 00:00:00 2001 From: Martin Brecht-Precht Date: Tue, 29 Jan 2019 15:30:17 +0100 Subject: [PATCH 1/3] Added a test --- test/StringBuilderTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/StringBuilderTest.php b/test/StringBuilderTest.php index d359a28..9af05b8 100644 --- a/test/StringBuilderTest.php +++ b/test/StringBuilderTest.php @@ -37,7 +37,7 @@ public function testBuilder() $this->assertEquals(9, $builder->indexOf('e', 4)); $this->assertNull($builder->indexOf('e', 10)); $this->assertEquals(9, $builder->lastIndexOf('e')); - $this->assertNull($builder->lastIndexOf('e',10)); + $this->assertNull($builder->lastIndexOf('e', 10)); $this->assertTrue($builder->contains('21b')); $this->assertEquals('b', $builder->firstChar()); $this->assertEquals('รถ', $builder->lastChar()); @@ -53,6 +53,12 @@ public function testBuilder() $this->assertEquals('02356', $builder->build()); } + public function testBuilderNull() + { + $builder = new StringBuilder(); + $this->assertNull($builder->build()); + } + public function testBuilderConstructFail() { $this->setExpectedException(get_class(new \InvalidArgumentException())); From c76c37d08f8772dcaace7c62af83ad8809260217 Mon Sep 17 00:00:00 2001 From: Martin Brecht-Precht Date: Mon, 18 Jan 2021 16:43:33 +0100 Subject: [PATCH 2/3] Switched the root namespace and vendor prefix. --- LICENSE | 2 +- README.md | 20 ++++++------- composer.json | 10 +++---- src/StringBuilder.php | 51 +++++++++++++++++----------------- src/Util/ArgumentValidator.php | 25 +++++++++-------- test/StringBuilderTest.php | 2 +- 6 files changed, 57 insertions(+), 53 deletions(-) diff --git a/LICENSE b/LICENSE index efdbef2..2ee9c3a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Martin Brecht-Precht, Markenwerk GmbH +Copyright (c) 2016 Martin Brecht-Precht, Chroma Experience GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 56cc7b3..4015c7f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # PHP String Builder -[![Build Status](https://travis-ci.org/markenwerk/php-string-builder.svg?branch=master)](https://travis-ci.org/markenwerk/php-string-builder) -[![Test Coverage](https://codeclimate.com/github/markenwerk/php-string-builder/badges/coverage.svg)](https://codeclimate.com/github/markenwerk/php-string-builder/coverage) +[![Build Status](https://travis-ci.org/chroma-x/php-string-builder.svg?branch=master)](https://travis-ci.org/chroma-x/php-string-builder) +[![Test Coverage](https://codeclimate.com/github/chroma-x/php-string-builder/badges/coverage.svg)](https://codeclimate.com/github/chroma-x/php-string-builder/coverage) [![Dependency Status](https://www.versioneye.com/user/projects/57aa33adf27cc20050102f0e/badge.svg)](https://www.versioneye.com/user/projects/57aa33adf27cc20050102f0e) [![SensioLabs Insight](https://img.shields.io/sensiolabs/i/ec36917d-baa1-482c-8916-41e2a2c48d5c.svg)](https://insight.sensiolabs.com/projects/ec36917d-baa1-482c-8916-41e2a2c48d5c) -[![Code Climate](https://codeclimate.com/github/markenwerk/php-string-builder/badges/gpa.svg)](https://codeclimate.com/github/markenwerk/php-string-builder) -[![Latest Stable Version](https://poser.pugx.org/markenwerk/string-builder/v/stable)](https://packagist.org/packages/markenwerk/string-builder) -[![Total Downloads](https://poser.pugx.org/markenwerk/string-builder/downloads)](https://packagist.org/packages/markenwerk/string-builder) -[![License](https://poser.pugx.org/markenwerk/string-builder/license)](https://packagist.org/packages/markenwerk/string-builder) +[![Code Climate](https://codeclimate.com/github/chroma-x/php-string-builder/badges/gpa.svg)](https://codeclimate.com/github/chroma-x/php-string-builder) +[![Latest Stable Version](https://poser.pugx.org/chroma-x/string-builder/v/stable)](https://packagist.org/packages/chroma-x/string-builder) +[![Total Downloads](https://poser.pugx.org/chroma-x/string-builder/downloads)](https://packagist.org/packages/chroma-x/string-builder) +[![License](https://poser.pugx.org/chroma-x/string-builder/license)](https://packagist.org/packages/chroma-x/string-builder) A basic string builder library providing different string methods written in PHP. @@ -16,7 +16,7 @@ A basic string builder library providing different string methods written in PHP ```{json} { "require": { - "markenwerk/string-builder": "~1.0" + "chroma-x/string-builder": "~1.0" } } ``` @@ -32,7 +32,7 @@ require_once('path/to/vendor/autoload.php'); ### Building and modifying a string ```{php} -use Markenwerk\StringBuilder\StringBuilder; +use ChromaX\StringBuilder\StringBuilder; $builder = new StringBuilder('rolod muspi meroL'); $builder @@ -121,7 +121,7 @@ will output the following All methods throw an `\InvalidArgumentException` if misconfigured except `indexOf` and `lastIndexOf` wich return `null` if the given subtring is not contained by the string to build. ```{php} -use Markenwerk\StringBuilder\StringBuilder; +use ChromaX\StringBuilder\StringBuilder; try { $builder = new StringBuilder(); @@ -153,7 +153,7 @@ Exception with message Position invalid ## Contribution Contributing to our projects is always very appreciated. -**But: please follow the contribution guidelines written down in the [CONTRIBUTING.md](https://github.com/markenwerk/php-string-builder/blob/master/CONTRIBUTING.md) document.** +**But: please follow the contribution guidelines written down in the [CONTRIBUTING.md](https://github.com/chroma-x/php-string-builder/blob/master/CONTRIBUTING.md) document.** ## License diff --git a/composer.json b/composer.json index 55a5b0c..5adb5e8 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,23 @@ { - "name": "markenwerk/string-builder", + "name": "chroma-x/string-builder", "type": "library", "description": "A string builder library providing different string methods written in PHP.", "keywords": [ "String", "Builder" ], - "homepage": "http://markenwerk.net/", + "homepage": "http://chroma-x.de/", "license": "MIT", "authors": [ { "name": "Martin Brecht-Precht", - "email": "mb@markenwerk.net", - "homepage": "http://markenwerk.net" + "email": "mb@chroma-x.de", + "homepage": "http://chroma-x.de" } ], "autoload": { "psr-4": { - "Markenwerk\\StringBuilder\\": "src/" + "ChromaX\\StringBuilder\\": "src/" } }, "require": { diff --git a/src/StringBuilder.php b/src/StringBuilder.php index 8b0899d..8492886 100644 --- a/src/StringBuilder.php +++ b/src/StringBuilder.php @@ -1,13 +1,14 @@ = $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } $this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position); return $this; @@ -87,7 +88,7 @@ public function insert($position, $string) * @param int $length * @param string $string * @return $this - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function replace($position, $length, $string) { @@ -95,10 +96,10 @@ public function replace($position, $length, $string) ArgumentValidator::validateUnsignedInteger($length); ArgumentValidator::validateScalar($string); if ($position >= $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } if ($position + $length > $this->length()) { - throw new \InvalidArgumentException('Length invalid'); + throw new InvalidArgumentException('Length invalid'); } $this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position + $length); return $this; @@ -110,17 +111,17 @@ public function replace($position, $length, $string) * @param int $position * @param string $character * @return $this - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setCharAt($position, $character) { ArgumentValidator::validateUnsignedInteger($position); ArgumentValidator::validateScalar($character); if ($position >= $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } if (mb_strlen((string)$character) !== 1) { - throw new \InvalidArgumentException('Expected a scalar value of length 1'); + throw new InvalidArgumentException('Expected a scalar value of length 1'); } $this->string = mb_substr($this->string, 0, $position) . (string)$character . mb_substr($this->string, $position + 1); return $this; @@ -148,14 +149,14 @@ public function reverse() * @param int $position * @param int $length * @return $this - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function delete($position, $length = null) { ArgumentValidator::validateUnsignedInteger($position); ArgumentValidator::validateUnsignedIntegerOrNull($length); if ($position >= $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } if ($length === null) { $this->string = mb_substr($this->string, 0, $position); @@ -170,13 +171,13 @@ public function delete($position, $length = null) * * @param int $position * @return $this - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function deleteCharAt($position) { ArgumentValidator::validateUnsignedInteger($position); if ($position >= $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } $this->string = mb_substr($this->string, 0, $position) . mb_substr($this->string, $position + 1); return $this; @@ -187,7 +188,7 @@ public function deleteCharAt($position) * * @param string $substring * @return bool - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function contains($substring) { @@ -203,7 +204,7 @@ public function contains($substring) * @param string $string * @param int $offset * @return int - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function indexOf($string, $offset = 0) { @@ -222,7 +223,7 @@ public function indexOf($string, $offset = 0) * @param string $string * @param int $offset * @return int - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function lastIndexOf($string, $offset = 0) { @@ -258,13 +259,13 @@ public function length() * * @param int $position * @return string - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function charAt($position) { ArgumentValidator::validateUnsignedInteger($position); if ($position >= $this->length()) { - throw new \InvalidArgumentException('Position invalid'); + throw new InvalidArgumentException('Position invalid'); } return mb_substr($this->string, $position, 1); } @@ -295,14 +296,14 @@ public function lastChar() * @param int $startPosition * @param int $length * @return string - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function buildSubstring($startPosition, $length = null) { ArgumentValidator::validateUnsignedInteger($startPosition); ArgumentValidator::validateUnsignedIntegerOrNull($length); if ($startPosition >= $this->length()) { - throw new \InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid'); + throw new InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid'); } if ($length === null) { return mb_substr($this->string, $startPosition); diff --git a/src/Util/ArgumentValidator.php b/src/Util/ArgumentValidator.php index 16e176c..f114b24 100644 --- a/src/Util/ArgumentValidator.php +++ b/src/Util/ArgumentValidator.php @@ -1,44 +1,47 @@ Date: Tue, 19 Nov 2024 10:41:31 +0100 Subject: [PATCH 3/3] Add empty string as default value for $string property to avoid issues in PHP >=8 version for nullable parameters --- composer.json | 56 +++++++++++++++++++++---------------------- src/StringBuilder.php | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 5adb5e8..ba8d92f 100644 --- a/composer.json +++ b/composer.json @@ -1,30 +1,30 @@ { - "name": "chroma-x/string-builder", - "type": "library", - "description": "A string builder library providing different string methods written in PHP.", - "keywords": [ - "String", - "Builder" - ], - "homepage": "http://chroma-x.de/", - "license": "MIT", - "authors": [ - { - "name": "Martin Brecht-Precht", - "email": "mb@chroma-x.de", - "homepage": "http://chroma-x.de" - } - ], - "autoload": { - "psr-4": { - "ChromaX\\StringBuilder\\": "src/" - } - }, - "require": { - "php": ">=5.4" - }, - "require-dev": { - "phpunit/phpunit": "~4.8", - "codeclimate/php-test-reporter": "dev-master" - } + "name": "chroma-x/string-builder", + "type": "library", + "description": "A string builder library providing different string methods written in PHP.", + "keywords": [ + "String", + "Builder" + ], + "homepage": "https://chroma-x.de/", + "license": "MIT", + "authors": [ + { + "name": "Martin Brecht-Precht", + "email": "mb@chroma-x.de", + "homepage": "https://chroma-x.de" + } + ], + "autoload": { + "psr-4": { + "ChromaX\\StringBuilder\\": "src/" + } + }, + "require": { + "php": ">=5.4" + }, + "require-dev": { + "phpunit/phpunit": "~4.8", + "codeclimate/php-test-reporter": "dev-master" + } } diff --git a/src/StringBuilder.php b/src/StringBuilder.php index 8492886..8a83526 100644 --- a/src/StringBuilder.php +++ b/src/StringBuilder.php @@ -16,7 +16,7 @@ class StringBuilder /** * @var string */ - private $string; + private $string = ''; /** * SimpleStringBuilder constructor