ReviewOS

also looking at this

meemalabs/meema-client-php

fix: Only use uuid for media and check for validity

#78
Merged glennmichael123 wants to merge media-uuid into main
11 files +65 -32
src/Exceptions/InvalidFormatException.phpmodified+4-2
Changes to src/Exceptions/InvalidFormatException.php
@@ -13,7 +13,9 @@ class InvalidFormatException extends Exception
1313 */
1414 public function __construct(string $message = null)
1515 {
16 parent::__construct($message ?? 'Error on line '.$this->getLine().' in '.$this->getFile()
17 .' ID is not a valid integer');
16 $customMessage = $message ? " {$message}" : ' ID is not a valid integer';
17
18 parent::__construct('Error on line '.$this->getLine().' in '.$this->getFile()
19 .$customMessage);
1820 }
1921}
src/Functions/Image.phpmodified+2-2
Changes to src/Functions/Image.php
@@ -135,9 +135,9 @@ class Image
135135 *
136136 * @return $this
137137 */
138 public function br($value)
138 public function bri($value)
139139 {
140 $this->args = array_merge($this->args, ['br' => $value]);
140 $this->args = array_merge($this->args, ['bri' => $value]);
141141
142142 return $this;
143143 }
src/Models/Media.phpmodified+20-19
Changes to src/Models/Media.php
@@ -6,10 +6,11 @@ use Meema\MeemaClient\Client;
66use Meema\MeemaClient\Exceptions\InvalidFormatException;
77use Meema\MeemaClient\Response\Response;
88use Meema\MeemaClient\Traits\CollectionsResponse;
9use Meema\MeemaClient\Traits\ValidsUuid;
910
1011class Media
1112{
12 use CollectionsResponse;
13 use CollectionsResponse, ValidsUuid;
1314
1415 /**
1516 * @var \Meema\MeemaClient\Client
@@ -68,8 +69,8 @@ class Media
6869 $ids = is_array($ids) ? $ids : func_get_args();
6970
7071 foreach ($ids as $id) {
71 if (! is_int($id)) {
72 throw new InvalidFormatException();
72 if (! $this->isValidUuid($id)) {
73 throw new InvalidFormatException('ID is not a valid UUID');
7374 }
7475 }
7576
@@ -103,8 +104,8 @@ class Media
103104 */
104105 public function find($id)
105106 {
106 if (! is_int($id)) {
107 throw new InvalidFormatException();
107 if (! $this->isValidUuid($id)) {
108 throw new InvalidFormatException('ID is not a valid UUID');
108109 }
109110
110111 $response = $this->client->request('GET', "media/${id}");
@@ -138,8 +139,8 @@ class Media
138139 */
139140 public function update($id, $name)
140141 {
141 if (! is_int($id)) {
142 throw new InvalidFormatException();
142 if (! $this->isValidUuid($id)) {
143 throw new InvalidFormatException('ID is not a valid UUID');
143144 }
144145
145146 $name = is_array($name) ? $name : compact('name');
@@ -161,8 +162,8 @@ class Media
161162 $ids = is_array($ids) ? $ids : func_get_args();
162163
163164 foreach ($ids as $id) {
164 if (! is_int($id)) {
165 throw new InvalidFormatException();
165 if (! $this->isValidUuid($id)) {
166 throw new InvalidFormatException('ID is not a valid UUID');
166167 }
167168 }
168169
@@ -190,8 +191,8 @@ class Media
190191 $ids = is_array($ids) ? $ids : func_get_args();
191192
192193 foreach ($ids as $id) {
193 if (! is_int($id)) {
194 throw new InvalidFormatException();
194 if (! $this->isValidUuid($id)) {
195 throw new InvalidFormatException('ID is not a valid UUID');
195196 }
196197 }
197198
@@ -219,8 +220,8 @@ class Media
219220 $ids = is_array($ids) ? $ids : func_get_args();
220221
221222 foreach ($ids as $id) {
222 if (! is_int($id)) {
223 throw new InvalidFormatException();
223 if (! $this->isValidUuid($id)) {
224 throw new InvalidFormatException('ID is not a valid UUID');
224225 }
225226 }
226227
@@ -248,8 +249,8 @@ class Media
248249 $ids = is_array($ids) ? $ids : func_get_args();
249250
250251 foreach ($ids as $id) {
251 if (! is_int($id)) {
252 throw new InvalidFormatException();
252 if (! $this->isValidUuid($id)) {
253 throw new InvalidFormatException('ID is not a valid UUID');
253254 }
254255 }
255256
@@ -277,8 +278,8 @@ class Media
277278 $ids = is_array($ids) ? $ids : func_get_args();
278279
279280 foreach ($ids as $id) {
280 if (! is_int($id)) {
281 throw new InvalidFormatException();
281 if (! $this->isValidUuid($id)) {
282 throw new InvalidFormatException('ID is not a valid UUID');
282283 }
283284 }
284285
@@ -306,8 +307,8 @@ class Media
306307 $ids = is_array($ids) ? $ids : func_get_args();
307308
308309 foreach ($ids as $id) {
309 if (! is_int($id)) {
310 throw new InvalidFormatException();
310 if (! $this->isValidUuid($id)) {
311 throw new InvalidFormatException('ID is not a valid UUID');
311312 }
312313 }
313314
src/Response/Response.phpmodified+1-3
formatting only · 8 hidden lines
Changes to src/Response/Response.php
@@ -73,7 +73,7 @@ class Response formatting only
7373 /**
7474 * Get response content.
7575 *
76 * @param mixed $key
76 * @param mixed $key
7777 *
7878 * @return array
7979 */
Changes to src/Response/Response.php
@@ -89,8 +89,6 @@ class Response
8989 /**
9090 * Get response content to array.
9191 *
92 * @param mixed $key
93 *
9492 * @return array
9593 */
9694 public function toArray()
src/Traits/DetectsFeature.phpmodifiedformatting only+1-1
Changes to src/Traits/DetectsFeature.php
@@ -11,7 +11,7 @@ trait DetectsFeature
1111 * Detect if the function called doesn't exist or a missing feature.
1212 *
1313 * @return void
14 *
14 *
1515 * @throws BadMethodCallException
1616 * @throws FeatureNotImplementedException
1717 */
src/Traits/ValidsUuid.phpadded+22-0
Changes to src/Traits/ValidsUuid.php
@@ -0,0 +1,22 @@
1<?php
2
3namespace Meema\MeemaClient\Traits;
4
5trait ValidsUuid
6{
7 /**
8 * Check if a given string is a valid UUID.
9 *
10 * @param string $uuid
11 *
12 * @return bool
13 */
14 public function isValidUuid($uuid)
15 {
16 if (! is_string($uuid) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid) !== 1)) {
17 return false;
18 }
19
20 return true;
21 }
22}
tests/MeemaFavoriteTest.phpmodified+3-1
Changes to tests/MeemaFavoriteTest.php
@@ -1,6 +1,8 @@
11<?php
22
3uses(Tests\ClientTest::class);
3use Tests\ClientTest;
4
5uses(ClientTest::class);
46
57beforeEach(function () {
68 $this->initializeDotEnv();
tests/MeemaFolderTest.phpmodified+3-1
Changes to tests/MeemaFolderTest.php
@@ -1,6 +1,8 @@
11<?php
22
3uses(Tests\ClientTest::class);
3use Tests\ClientTest;
4
5uses(ClientTest::class);
46
57beforeEach(function () {
68 $this->initializeDotEnv();
tests/MeemaMediaTest.phpmodified+3-1
Changes to tests/MeemaMediaTest.php
@@ -1,6 +1,8 @@
11<?php
22
3uses(Tests\ClientTest::class);
3use Tests\ClientTest;
4
5uses(ClientTest::class);
46
57beforeEach(function () {
68 $this->initializeDotEnv();
tests/MeemaTagTest.phpmodified+3-1
Changes to tests/MeemaTagTest.php
@@ -1,6 +1,8 @@
11<?php
22
3uses(Tests\ClientTest::class);
3use Tests\ClientTest;
4
5uses(ClientTest::class);
46
57beforeEach(function () {
68 $this->initializeDotEnv();
tests/Pest.phpmodified+3-1
Changes to tests/Pest.php
@@ -1,3 +1,5 @@
11<?php
22
3uses(Tests\ClientTest::class);
3use Tests\ClientTest;
4
5uses(ClientTest::class);