Skip to content

Commit

Permalink
wip - property promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
bytestream committed May 23, 2024
1 parent b583389 commit 0714aa6
Show file tree
Hide file tree
Showing 60 changed files with 1,385 additions and 4,075 deletions.
2 changes: 1 addition & 1 deletion src/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ protected function snakeCaseToCamelCase(string $key): string
*/
protected function snakeCaseToPascalCase(string $key): string
{
return str_replace('_', '', ucwords($key, '_'));
return lcfirst(str_replace('_', '', ucwords($key, '_')));
}
}
133 changes: 6 additions & 127 deletions src/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,136 +20,15 @@
use function is_string;
use function property_exists;

/**
* Class BaseModel
* @package SupportPal\ApiClient\Model
*/
abstract class BaseModel implements Model
{
public const REQUIRED_FIELDS = [];

use StringHelper;

/** @var array<mixed>|null */
#[SerializedName('pivot')]
protected ?array $pivot = null;

/**
* @inheritDoc
*/
public function fill(array $data): Model
{
foreach ($data as $key => $value) {
if (! is_string($key)) {
throw new InvalidArgumentException(
'Supplied input must be an associative array of template: array<string, mixed>'
);
}
}

$attributeAwareTransformers = [new IntToBooleanTransformer(new ReflectionExtractor),];
$transformers = [new StringTrimTransformer,];

$this->assertRequiredFieldsExists($data);
foreach ($data as $key => $value) {
if (! property_exists($this, $this->snakeCaseToCamelCase($key))) {
continue;
}

$value = $this->applyAttributeAwareTransformers($attributeAwareTransformers, $key, $value);
$value = $this->applyValueTransformers($transformers, $value);

$this->setAttributeValue($this->snakeCaseToCamelCase($key), $value);
}

return $this;
}

/**
* @return string[]
*/
protected function getRequiredFields(): array
{
return static::REQUIRED_FIELDS;
}

/**
* This functions asserts that all the required values for the API are passed correctly
* @param array<mixed> $data
*/
protected function assertRequiredFieldsExists(array $data): void
{
$missingFields = [];
foreach ($this->getRequiredFields() as $required) {
if (isset($data[$required])) {
continue;
}

array_push($missingFields, $required);
}

if (count($missingFields) > 0) {
throw new MissingRequiredFieldsException(
'incomplete required fields, the following are missing: ' . implode(',', $missingFields)
);
}
}

/**
* @return array<mixed>|null
*/
public function getPivot(): ?array
{
return $this->pivot;
}

/**
* @param AttributeAwareTransformer[] $attributeAwareTransformers
* @return mixed
*/
private function applyAttributeAwareTransformers(array $attributeAwareTransformers, string $key, mixed $value)
{
foreach ($attributeAwareTransformers as $transformer) {
if (! $transformer->canTransform($this->snakeCaseToCamelCase($key), get_class($this), $value)) {
continue;
}

$value = $transformer->transform($value);
}

return $value;
}

/**
* @throws InvalidArgumentException
*/
private function setAttributeValue(string $key, mixed $value): void
{
try {
$this->{$key} = $value;
} catch (TypeError $exception) {
throw new InvalidArgumentException(
$exception->getMessage(),
$exception->getCode(),
$exception->getPrevious()
);
}
}

/**
* @param Transformer[] $transformers
* @return mixed
*/
private function applyValueTransformers(array $transformers, mixed $value)
{
foreach ($transformers as $transformer) {
if (! $transformer->canTransform($value)) {
continue;
}

$value = $transformer->transform($value);
}

return $value;
public function __construct(
/** @var array<mixed>|null */
#[SerializedName('pivot')]
public readonly ?array $pivot = null,
) {
//
}
}
12 changes: 6 additions & 6 deletions src/Model/BaseTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

abstract class BaseTranslation extends BaseModel
{
#[SerializedName('locale')]
protected string $locale;

public function getLocale(): string
{
return $this->locale;
public function __construct(
#[SerializedName('locale')]
public readonly string $locale,
$pivot = null,
) {
parent::__construct($pivot);
}
}
2 changes: 0 additions & 2 deletions src/Model/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

/**
* Wrap collection of Model values
* Class Collection
* @package SupportPal\ApiClient\Model\Collection
*/
class Collection
{
Expand Down
Loading

0 comments on commit 0714aa6

Please sign in to comment.