From 4f5046107cced0dea04eb2557489056b4154091a Mon Sep 17 00:00:00 2001 From: Ammar Hamilcar Tijani Date: Sat, 26 Aug 2023 21:19:52 +0200 Subject: [PATCH 1/2] * fixed incorrect parsing of field new_email in Contacts. * improved parsing of email field in Contacts. * added support for user tags to add/update user. * added basic phpstan static test run. --- .github/workflows/php.yml | 45 +++++++++++++++++++++ .gitignore | 13 ++++++ composer.json | 20 ++++++--- composer.lock | 83 ++++++++++++++++++++++++++++++++++++++ phpstan.neon | 4 ++ src/Enormail/ApiClient.php | 2 +- src/Enormail/Base.php | 4 +- src/Enormail/Contacts.php | 42 ++++++++++++++----- src/Enormail/Rest.php | 4 +- 9 files changed, 198 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/php.yml create mode 100644 .gitignore create mode 100644 composer.lock create mode 100644 phpstan.neon diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..8d8630b --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,45 @@ +name: PHP Composer + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - name: Initialise GitHub Actions env + uses: actions/checkout@v2 + + - name: Setup PHP with specific version (phpstan requires 7.2+) + uses: shivammathur/setup-php@v2 + with: + php-version: '5.3' + extensions: ext-curl, ext-json + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + php-version: '5.3' + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: composer install --prefer-dist --no-progress --no-suggest + + # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" + # Docs: https://getcomposer.org/doc/articles/scripts.md + + - name: Run test suite + run: composer run-script test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41e4ab1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +### project/IDE files +.settings +.buildpath +.project +.DS_Store +*.tmp +.idea + +### packages +/vendor*/ + +## phpstan +/src/phpstan.log diff --git a/composer.json b/composer.json index a041838..98dcbd8 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,9 @@ { "name": "enormail/enormail-php-api", - "description" : "A PHP Library for the Enormail API", - "keywords": ["enormail"], + "description": "A PHP Library for the Enormail API", + "keywords": [ + "enormail" + ], "homepage": "https://github.com/Enormail/enormail-php-api.git", "authors": [ { @@ -10,11 +12,19 @@ } ], "license": "GNU2", - "minimum-stability": "dev", + "minimum-stability": "stable", "require": { "php": ">=5.3.0" }, "autoload": { - "psr-0": { "Enormail": "src/" } + "psr-0": { + "Enormail": "src/" + } + }, + "scripts": { + "test": "phpstan" + }, + "require-dev": { + "phpstan/phpstan": "^1.10" } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..c4b74c3 --- /dev/null +++ b/composer.lock @@ -0,0 +1,83 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "2f3fa20a9a2e3ef8138b0bf0a013d2c4", + "packages": [], + "packages-dev": [ + { + "name": "phpstan/phpstan", + "version": "1.10.32", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/c47e47d3ab03137c0e121e77c4d2cb58672f6d44", + "reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-08-24T21:54:50+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.0" + }, + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e79efe0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: 1 + paths: + - src/ diff --git a/src/Enormail/ApiClient.php b/src/Enormail/ApiClient.php index 9ee81ca..8dc6fcc 100644 --- a/src/Enormail/ApiClient.php +++ b/src/Enormail/ApiClient.php @@ -22,7 +22,7 @@ class ApiClient { * The constructor * * @access public - * @return nill + * @return void */ public function __construct($key, $format = 'json') { diff --git a/src/Enormail/Base.php b/src/Enormail/Base.php index 7b675f5..4c09b94 100644 --- a/src/Enormail/Base.php +++ b/src/Enormail/Base.php @@ -12,13 +12,15 @@ abstract class Base { * Default response format */ public $format = 'json'; + + protected $rest; /** * Constructor * * @access public * @param object $rest em_rest object - * @return nill + * @return void */ public function __construct(Rest $rest) { diff --git a/src/Enormail/Contacts.php b/src/Enormail/Contacts.php index 4974db4..855acbd 100644 --- a/src/Enormail/Contacts.php +++ b/src/Enormail/Contacts.php @@ -45,16 +45,26 @@ public function details($listid, $email) * @param string $email the contact's e-mail address * @param array $fields an array with optional fields, example: array('lastname' => 'Contact lastname', 'city' => 'City name') * @param integer $activate_autoresponder a flag to activate the autoresponder when the contact is added (1 or 0, default 1) + * @param array $tags an array with optional tags, example: array(0 => 'tag1', 1 => 'tag2') * @return string results (json|xml) */ - public function add($listid, $name, $email, array $fields = null, $activate_autoresponder = 1) + public function add($listid, $name, $email, array $fields = null, $activate_autoresponder = 1, array $tags = null) { - return $this->rest->post("/contacts/{$listid}.{$this->format}", array( + $payload = array( 'name' => $name, 'email' => $email, - 'fields' => $fields, + 'fields' => $fields, 'activate_autoresponder' => $activate_autoresponder - )); + ); + + if ($tags) { + $payload['tags'] = $tags; + } elseif (isset($payload['fields']['tags'])) { + $payload['tags'] = $payload['fields']['tags']; + } + unset($payload['fields']['tags']); + + return $this->rest->post("/contacts/{$listid}.{$this->format}", $payload); } /** @@ -67,20 +77,30 @@ public function add($listid, $name, $email, array $fields = null, $activate_auto * @param array $fields an array with optional fields, example: array('lastname' => 'Contact lastname', 'city' => 'City name') * @param string $new_email the contact's new e-email address (optional) * @param string $move_to_listid the list of the list you want the contact to move to (optional) + * @param array $tags an array with optional tags, example: array(0 => 'tag1', 1 => 'tag2') * @return string results (json|xml) */ - public function update($listid, $name, $email, array $fields = null, $new_email = null, $move_to_listid = null) + public function update($listid, $name, $email, array $fields = null, $new_email = null, $move_to_listid = null, array $tags = null) { $aData['name'] = $name; $aData['fields'] = $fields; - - if (!is_null($new_email)) - $aData['email'] = $new_email; - - if (!is_null($move_to_listid)) + + if ($tags) { + $aData['tags'] = $tags; + } elseif (isset($aData['fields']['tags'])) { + $aData['tags'] = $aData['fields']['tags']; + } + unset($aData['fields']['tags']); + + if (!is_null($new_email)) { + $aData['new_email'] = $new_email; + } + + if (!is_null($move_to_listid)) { $aData['listid'] = $move_to_listid; + } - return $this->rest->put("/contacts/{$listid}.{$this->format}?email={$email}", $aData); + return $this->rest->put("/contacts/{$listid}.{$this->format}?email=".urldecode(urlencode($email)), $aData); } /** diff --git a/src/Enormail/Rest.php b/src/Enormail/Rest.php index 169f037..fd557d6 100644 --- a/src/Enormail/Rest.php +++ b/src/Enormail/Rest.php @@ -133,7 +133,9 @@ private function _prep_post_vars($vars, $sep = '&') } class Response { - + + protected $http_code, $http_response; + public function __construct($response) { // Set response From 18e7b87d1f4fb7de010a7aa52a59ca2f1c09afee Mon Sep 17 00:00:00 2001 From: Ammar Hamilcar Tijani Date: Mon, 28 Aug 2023 10:56:28 +0200 Subject: [PATCH 2/2] * optimised github php workflow --- .github/workflows/php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8d8630b..825ab8a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -18,7 +18,7 @@ jobs: - name: Setup PHP with specific version (phpstan requires 7.2+) uses: shivammathur/setup-php@v2 with: - php-version: '5.3' + php-version: '7.2' extensions: ext-curl, ext-json - name: Validate composer.json and composer.lock @@ -28,7 +28,7 @@ jobs: id: composer-cache uses: actions/cache@v2 with: - php-version: '5.3' + php-version: '7.2' path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} restore-keys: |