Skip to content

Commit

Permalink
Release 1.0.7 - Added camelCase and case-insensitive camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
vanengers committed Oct 18, 2023
1 parent 7589a4b commit 57011e8
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/PhpJsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ public function fromArray($data)

if (method_exists($this, 'set' . ucfirst(strtolower($key)))) {
call_user_func_array([$this, 'set' . ucfirst(strtolower($key))], ['data' => $value]);
} else if (property_exists($this, strtolower($key))) {
} else if (method_exists($this, 'set' . $this->snakeToCamel($key))) {
call_user_func_array([$this, 'set' . $this->snakeToCamel($key)], ['data' => $value]);
}
else if (property_exists($this, strtolower($key))) {
$this->{strtolower($key)} = $value;
} else if (property_exists($this, $key)) {
}
else if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
Expand All @@ -96,6 +100,17 @@ public function toJson(array $configuration = [])
return json_encode($this->toArray());
}

/**
* @param $input
* @return string
* @author George van Engers <[email protected]>
* @since 18-10-2023
*/
private function snakeToCamel($input)
{
return ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input))));
}

/**
* @param $configuration
* @param $key
Expand Down
10 changes: 10 additions & 0 deletions tests/Mocks/TestCamelObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vanengers\PhpJsonObjectLibrary\Tests\Mocks;

use Vanengers\PhpJsonObjectLibrary\PhpJsonObject;

class TestCamelObject extends PhpJsonObject
{
public $camel_test;
}
36 changes: 36 additions & 0 deletions tests/Mocks/TestCamelSetterObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vanengers\PhpJsonObjectLibrary\Tests\Mocks;

use Vanengers\PhpJsonObjectLibrary\PhpJsonObject;

class TestCamelSetterObject extends PhpJsonObject
{
public $camel_test;

public $camel_test_double;
public $camel_test_double_triple;

public function setCamelTest($data)
{
$this->camel_test = get_class($this).$data;
}

/**
* @param mixed $camel_test_double
*/
public function setCamelTestDouble($data): void
{
$this->camel_test_double = get_class($this).$data;
}

/**
* @param mixed $camel_test_double_triple
*/
public function setCamelTestDoubleTriple($data): void
{
$this->camel_test_double_triple = get_class($this).$data;
}


}
19 changes: 19 additions & 0 deletions tests/Mocks/TestCaseInsensitiveSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Vanengers\PhpJsonObjectLibrary\Tests\Mocks;

use Vanengers\PhpJsonObjectLibrary\PhpJsonObject;

class TestCaseInsensitiveSetter extends PhpJsonObject
{
public $createdAt;
public $ThisHasSomeCaseInsentiveFields;

/**
* @param mixed $ThisHasSomeCaseInsentiveFields
*/
public function setThisHasSomeCaseInsentiveFields($data): void
{
$this->ThisHasSomeCaseInsentiveFields = get_class($this).$data;
}
}
21 changes: 21 additions & 0 deletions tests/Mocks/TestCaseInsensitiveSetterCamelCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Vanengers\PhpJsonObjectLibrary\Tests\Mocks;

use Vanengers\PhpJsonObjectLibrary\PhpJsonObject;

class TestCaseInsensitiveSetterCamelCase extends PhpJsonObject
{
public $createdAt;
public $ThisHas_SomeCase_InsentiveFields;

/**
* @param mixed $ThisHas_SomeCase_InsentiveFields
*/
public function setThisHasSomeCaseInsentiveFields($data): void
{
$this->ThisHas_SomeCase_InsentiveFields = get_class($this).$data;
}


}
10 changes: 10 additions & 0 deletions tests/toArray/ArrayFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vanengers\PhpJsonObjectLibrary\Tests\toArray;

use PHPUnit\Framework\TestCase;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestCamelObject;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestObject;

class ArrayFieldTest extends TestCase
Expand Down Expand Up @@ -55,4 +56,13 @@ public function testToArrayGivesResultCount()

$this->assertCount(2, $result);
}

public function testCamelPropGivesResult()
{
$title = "Hello World!";
$test = new TestCamelObject("{\"camel_test\":\"".$title."\"}");
$result = $test->toArray();

$this->assertEquals($title, $result["camel_test"]);
}
}
34 changes: 34 additions & 0 deletions tests/toArray/CaseInsentiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPUnit\Framework\TestCase;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestCaseInsensitiveObject;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestCaseInsensitiveSetter;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestCaseInsensitiveSetterCamelCase;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestObject;

class CaseInsentiveTest extends TestCase
Expand All @@ -23,4 +25,36 @@ public function testToArrayGivesResultFieldsWithResults()
$this->assertEquals($createdAt, $result["createdAt"]);
$this->assertEquals($caseInsentive, $result["ThisHasSomeCaseInsentiveFields"]);
}

public function testToArrayGivesResultFieldsWithResultsSetter()
{
$createdAt = 'cvdnfvgdfvghj344';
$caseInsentive = 'HJGdgfsjkftghjkewrtjk34hfg;';
$test = new TestCaseInsensitiveSetter([
"createdAt" => $createdAt,
"ThisHasSomeCaseInsentiveFields" => $caseInsentive
]);
$result = $test->toArray();

$this->assertArrayHasKey("createdAt", $result);
$this->assertArrayHasKey("ThisHasSomeCaseInsentiveFields", $result);
$this->assertEquals($createdAt, $result["createdAt"]);
$this->assertEquals(get_class($test).$caseInsentive, $result["ThisHasSomeCaseInsentiveFields"]);
}

public function testToArrayGivesResultFieldsWithResultsSetterWithCamel()
{
$createdAt = 'cvdnfvgdfvghj344';
$caseInsentive = 'HJGdgfsjkftghjkewrtjk34hfg;';
$test = new TestCaseInsensitiveSetterCamelCase([
"createdAt" => $createdAt,
"ThisHas_SomeCase_InsentiveFields" => $caseInsentive
]);
$result = $test->toArray();

$this->assertArrayHasKey("createdAt", $result);
$this->assertArrayHasKey("ThisHas_SomeCase_InsentiveFields", $result);
$this->assertEquals($createdAt, $result["createdAt"]);
$this->assertEquals(get_class($test).$caseInsentive, $result["ThisHas_SomeCase_InsentiveFields"]);
}
}
2 changes: 1 addition & 1 deletion tests/toArray/ExcludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ExcludeTest extends TestCase
{
public function testToArrayDoesGiveResultWithPrefix()
public function testToArrayExcludesDescription()
{
$title = "Hello World!";
$test = new TestExcludeObject(['title'=>$title]);
Expand Down
67 changes: 67 additions & 0 deletions tests/toArray/SetterFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vanengers\PhpJsonObjectLibrary\Tests\toArray;

use PHPUnit\Framework\TestCase;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestCamelSetterObject;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestSetterObject;
use Vanengers\PhpJsonObjectLibrary\Tests\Mocks\TestSetterWithMapperObject;

Expand Down Expand Up @@ -30,4 +31,70 @@ public function testToArrayDoesGiveResultWithPrefixWithMapper()
$this->assertArrayHasKey("title", $result);
$this->assertEquals($test->getTestPrefix().$title, $result["title"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelCase()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['camel_test'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelDouble()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['camel_test_double'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test_double", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test_double"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelTriple()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['camel_test_double_triple'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test_double_triple", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test_double_triple"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelTripleSetWithSetterName()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['CamelTestDoubleTriple'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test_double_triple", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test_double_triple"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelDoubleSetWithSetterName()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['CamelTestDouble'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test_double", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test_double"]);
}

public function testToArrayDoesGiveResultWithPrefixCaseCamelSingleSetWithSetterName()
{
$title = "Hello World!";
$test = new TestCamelSetterObject(['CamelTest'=>$title]);

$result = $test->toArray();

$this->assertArrayHasKey("camel_test", $result);
$this->assertEquals(get_class($test).$title, $result["camel_test"]);
}
}

0 comments on commit 57011e8

Please sign in to comment.