Skip to content

Commit ec70fce

Browse files
Amir Tockertocker
authored andcommitted
Make utility methods private
1 parent 05d59ac commit ec70fce

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/Cloudinary.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public static function build_array($value)
157157
* - Valid assoc array: array("k" => "v", "k2"=> "v2")
158158
* - Array of assoc arrays: array(array("k" => "v"), array("k2" =>"v2"))
159159
* - JSON decodable string: '{"k": "v"}', or '[{"k": "v"}]'
160-
* - Array of JSON decodable strings: array('{"k": "v"}', '{"k2","v2"}')
161160
*
162161
* Invalid values examples:
163162
* - array("not", "an", "assoc", "array")
@@ -168,15 +167,19 @@ public static function build_array($value)
168167
*
169168
* @throws InvalidArgumentException in case value cannot be converted to an array of associative arrays
170169
*/
171-
public static function build_array_of_assoc_arrays($value)
170+
private static function build_array_of_assoc_arrays($value)
172171
{
173172
if (is_string($value)) {
174173
$value = Cloudinary::json_decode_cb($value, 'Cloudinary::ensure_assoc');
175174
if (is_null($value)) {
176175
throw new InvalidArgumentException("Failed parsing JSON string value");
177176
}
178177
}
179-
return Cloudinary::build_array($value);
178+
$value = Cloudinary::build_array($value);
179+
if (!self::is_array_of_assoc($value)) {
180+
throw new InvalidArgumentException("Expected an array of associative arrays");
181+
}
182+
return $value;
180183
}
181184

182185
static function ensure_assoc($item)
@@ -204,7 +207,7 @@ static function ensure_assoc($item)
204207
*
205208
* @throws InvalidArgumentException in case the value is not an array of associative arrays
206209
*/
207-
public static function json_encode_array_of_assoc_arrays($array)
210+
private static function json_encode_array_of_assoc_arrays($array)
208211
{
209212
return self::json_encode_cb($array, 'Cloudinary::encode_dates');
210213
}
@@ -288,7 +291,7 @@ public static function json_decode_cb($json, $decoder)
288291
throw new InvalidArgumentException("Expected an string");
289292
}
290293
$array = json_decode($json, true);
291-
if(!is_null($decoder))
294+
if(!is_null($decoder) && !is_null($array))
292295
{
293296
foreach ($array as $key => $value) {
294297

tests/CloudinaryTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,32 +1736,33 @@ public function test_build_array_of_assoc_arrays()
17361736
{
17371737
$assoc_array_data = array("one" => 1, "two" => 2, "three" => 3);
17381738
$array_of_assoc_array = array($assoc_array_data);
1739-
1739+
$method = new ReflectionMethod('Cloudinary', 'build_array_of_assoc_arrays');
1740+
$method->setAccessible(true);
17401741
# should convert an assoc array to an array of assoc arrays
1741-
$this->assertEquals(array($assoc_array_data), Cloudinary::build_array_of_assoc_arrays($assoc_array_data));
1742+
$this->assertEquals(array($assoc_array_data), $method->invoke(null, $assoc_array_data));
17421743

17431744
# should leave as is
1744-
$this->assertEquals($array_of_assoc_array, Cloudinary::build_array_of_assoc_arrays($array_of_assoc_array));
1745+
$this->assertEquals($array_of_assoc_array, $method->invoke(null, $array_of_assoc_array));
17451746

17461747
# should convert a JSON string representing an assoc array to an array of assoc arrays
17471748
$string_data = '{"one": 1, "two": 2, "three": 3}';
1748-
$this->assertEquals($array_of_assoc_array, Cloudinary::build_array_of_assoc_arrays($string_data));
1749+
$this->assertEquals($array_of_assoc_array, $method->invoke(null, $string_data));
17491750

17501751
# should convert a JSON string representing an array of assoc arrays to an array of assoc arrays
17511752
$string_array_data = '[{"one": 1, "two": 2, "three": 3}]';
1752-
$this->assertEquals($array_of_assoc_array, Cloudinary::build_array_of_assoc_arrays($string_array_data));
1753+
$this->assertEquals($array_of_assoc_array, $method->invoke(null, $string_array_data));
17531754

17541755
# should return an empty array on null
1755-
$this->assertEquals(array(), Cloudinary::build_array_of_assoc_arrays(null));
1756+
$this->assertEquals(array(), $method->invoke(null, null));
17561757

17571758
# should return an empty array on array()
1758-
$this->assertEquals(array(), Cloudinary::build_array_of_assoc_arrays(array()));
1759+
$this->assertEquals(array(), $method->invoke(null, array()));
17591760

17601761
# should throw InvalidArgumentException on invalid value
17611762
$invalid_values = array("", array(array()), array("not_an_array"), array(7357));
17621763
foreach ($invalid_values as $value) {
17631764
try {
1764-
Cloudinary::build_array_of_assoc_arrays($value);
1765+
$method->invoke(null, $value);
17651766
$this->fail('InvalidArgumentException was not thrown');
17661767
} catch (\InvalidArgumentException $e) {
17671768
}
@@ -1773,23 +1774,25 @@ public function test_build_array_of_assoc_arrays()
17731774
*/
17741775
public function test_json_encode_array_of_assoc_arrays()
17751776
{
1777+
$method = new ReflectionMethod('Cloudinary', 'json_encode_array_of_assoc_arrays');
1778+
$method->setAccessible(true);
17761779
# should encode simple values
1777-
$this->assertEquals('[]', Cloudinary::json_encode_array_of_assoc_arrays(array()));
1778-
$this->assertEquals('[{"k":"v"}]', Cloudinary::json_encode_array_of_assoc_arrays(array(array("k" =>"v"))));
1780+
$this->assertEquals('[]', $method->invoke(null, (array())));
1781+
$this->assertEquals('[{"k":"v"}]', $method->invoke(null, array(array("k" =>"v"))));
17791782

17801783
# should encode DateTime to ISO format
17811784
$this->assertEquals(
17821785
'[{"k":"2019-02-22T00:00:00+0000"}]',
1783-
Cloudinary::json_encode_array_of_assoc_arrays(array(array("k" =>new \DateTime("2019-02-22"))))
1786+
$method->invoke(null, array(array("k" =>new \DateTime("2019-02-22"))))
17841787
);
17851788
$this->assertEquals(
17861789
'[{"k":"2019-02-22T16:20:57+0000"}]',
1787-
Cloudinary::json_encode_array_of_assoc_arrays(array(array("k" =>new \DateTime("2019-02-22 16:20:57Z"))))
1790+
$method->invoke(null, array(array("k" =>new \DateTime("2019-02-22 16:20:57Z"))))
17881791
);
17891792

17901793
# should throw InvalidArgumentException on invalid value
17911794
try {
1792-
Cloudinary::json_encode_array_of_assoc_arrays("not_valid");
1795+
$method->invoke(null, "not_valid");
17931796
$this->fail('InvalidArgumentException was not thrown');
17941797
} catch (\InvalidArgumentException $e) {
17951798
}
@@ -1803,12 +1806,14 @@ public function test_json_encode_array_of_assoc_arrays()
18031806
*/
18041807
public function test_encode_array_to_json()
18051808
{
1809+
$method = new ReflectionMethod('Cloudinary', 'json_encode_array_of_assoc_arrays');
1810+
$method->setAccessible(true);
18061811
# should handle null value
18071812
$this->assertEquals(null, Cloudinary::encode_array_to_json(null));
18081813

18091814
# should handle regular case
18101815
$this->assertEquals('[{"k":"v"}]', Cloudinary::encode_array_to_json('[{"k":"v"}]'));
1811-
$this->assertEquals('[{"k":"v"}]', Cloudinary::json_encode_array_of_assoc_arrays(array(array("k" =>"v"))));
1816+
$this->assertEquals('[{"k":"v"}]', $method->invoke(null, array(array("k" =>"v"))));
18121817
}
18131818

18141819
private function cloudinary_url_assertion($source, $options, $expected, $expected_options = array())

tests/UploaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,14 @@ public function test_access_control()
558558
assertParam($this, "access_control", $exp_acl_str);
559559

560560
# Should accept an array of all the above values
561-
$array_of_acl = array($acl, $acl_2, $acl_str);
561+
$array_of_acl = array($acl, $acl_2);
562562
$exp_array_of_acl = '[' . implode(
563563
",",
564564
array_map(
565565
function ($v) {
566566
return substr($v, 1, -1);
567567
},
568-
array($exp_acl, $exp_acl_2, $exp_acl_str)
568+
array($exp_acl, $exp_acl_2)
569569
)
570570
) . ']';
571571

0 commit comments

Comments
 (0)