Skip to content

Commit

Permalink
better getter,setter
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyraul committed Aug 2, 2024
1 parent 8e75919 commit 99a1ed9
Show file tree
Hide file tree
Showing 40 changed files with 224 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .jeeves.phpunit_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Mygento:
type: varchar
family:
type: varchar
is_active:
active:
type: boolean
product_id:
type: int
Expand Down
45 changes: 45 additions & 0 deletions src/Jeeves/Generators/Crud/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,49 @@ protected function isNullable(array $value): bool

return isset($value['nullable']) && $value['nullable'] === false;
}

protected function createGetterName(string $name, array $params): array
{
$method = $this->snakeCaseToUpperCamelCase($name);
if ($params['type'] === 'boolean') {
if (str_starts_with($name, 'is_') ||
str_starts_with($name, 'has_') ||
str_starts_with($name, 'can_') ||
str_starts_with($name, 'should_')
) {
return [
$this->snakeCaseToCamelCase($name),
ucfirst(str_replace('_', ' ', $name)),
];
}

return [
'is' . $method,
'Is ' . str_replace('_', ' ', $name),
];
}

return [
'get' . $method,
'Get ' . str_replace('_', ' ', $name),
];
}

protected function createSetterName(string $name, array $params): array
{
$method = $this->snakeCaseToUpperCamelCase($name);
if ($params['type'] === 'boolean') {
if (str_starts_with($name, 'is_')) {
return [
'set' . substr($method, 2),
'Set ' . str_replace('_', ' ', substr($name, 3)),
];
}
}

return [
'set' . $method,
'Set ' . str_replace('_', ' ', $name),
];
}
}
31 changes: 21 additions & 10 deletions src/Jeeves/Generators/Crud/Interfaces/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ public function genModelInterface(
if (isset($value['identity']) && $value['identity'] === true) {
$generated = true;
}
$c = $interface->addConstant(strtoupper($name), strtolower($name))->setPublic();
$c = $interface->addConstant(strtoupper($name), strtolower($name))
->setPublic();
if ($hasTypedConst) {
$c->setType('string');
}
$method = $this->snakeCaseToUpperCamelCase($name);
$get = $interface->addMethod('get' . $method)

$getter = $this->createGetterName($name, $value);
$get = $interface->addMethod($getter[0])
->setVisibility('public');
$get->addComment('Get ' . str_replace('_', ' ', $name));
$get->addComment($getter[1]);

$set = $interface->addMethod('set' . $method);
$set->addComment('Set ' . str_replace('_', ' ', $name))
$setter = $this->createSetterName($name, $value);
$set = $interface->addMethod($setter[0]);
$set->addComment($setter[1])
->setVisibility('public');
$param = $set->addParameter($this->snakeCaseToCamelCase($name));

Expand All @@ -73,15 +76,19 @@ public function genModelInterface(
$set->setReturnType('self');

if ($hasApi) {
$get->addComment('@return ' . $this->convertType($value['type']) . ($notNullable ? '' : '|null'));
$get->addComment(
'@return ' . $this->convertType($value['type']) . ($notNullable ? '' : '|null')
);
$set->addComment('@return $this');
}

if ($this->snakeCaseToCamelCase($name) == 'id') {
$param->setNullable(false);
$param->setType(null);

$set->addComment('@param ' . $this->convertType($value['type']) . ' $' . $this->snakeCaseToCamelCase($name));
$set->addComment(
'@param ' . $this->convertType($value['type']) . ' $' . $this->snakeCaseToCamelCase($name)
);
}
}

Expand All @@ -99,7 +106,9 @@ public function genModelInterface(
->setVisibility('public');

if ($hasApi) {
$getId->addComment('@return ' . $this->convertType($item['type']) . ($item['nullable'] ? '|null' : ''));
$getId->addComment(
'@return ' . $this->convertType($item['type']) . ($item['nullable'] ? '|null' : '')
);
}

$getId->setReturnType($this->convertType($item['type']));
Expand All @@ -111,7 +120,9 @@ public function genModelInterface(
->setVisibility('public');

$setId->addParameter(self::DEFAULT_KEY);
$setId->addComment('@param ' . $this->convertType($item['type']) . ' $id');
$setId->addComment(
'@param ' . $this->convertType($item['type']) . ' $id'
);

if ($hasApi) {
$setId->addComment('@return $this');
Expand Down
12 changes: 7 additions & 5 deletions src/Jeeves/Generators/Crud/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ public function genModel(
if (isset($value['identity']) && $value['identity'] === true) {
$generated = true;
}
$method = $this->snakeCaseToUpperCamelCase($name);
$getter = $class->addMethod('get' . $method)
->addComment('Get ' . str_replace('_', ' ', $name))

$getterName = $this->createGetterName($name, $value);
$getter = $class->addMethod($getterName[0])
->addComment($getterName[1])
->setVisibility('public')
->setBody('return $this->getData(self::' . strtoupper($name) . ');');

$setter = $class->addMethod('set' . $method)
->addComment('Set ' . str_replace('_', ' ', $name))
$setterName = $this->createSetterName($name, $value);
$setter = $class->addMethod($setterName[0])
->addComment($setterName[1])
->setVisibility('public');
$setParam = $setter->addParameter($this->snakeCaseToCamelCase($name));
$setter->setBody('return $this->setData(self::' . strtoupper($name) . ', $' . $this->snakeCaseToCamelCase($name) . ');');
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Api/Data/CardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public function getCategoryId(): int;
public function setCategoryId(int $categoryId): self;

/**
* Get is active
* Is active
* @return bool
*/
public function getIsActive(): bool;
public function isActive(): bool;

/**
* Set is active
* Set active
* @return $this
*/
public function setIsActive(bool $isActive): self;
public function setActive(bool $isActive): self;

/**
* Get store id
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Api/Data/CartItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public function getCartId(): ?int;
public function setCartId(?int $cartId): self;

/**
* Get is active
* Is active
*/
public function getIsActive(): bool;
public function isActive(): bool;

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self;
public function setActive(bool $isActive): self;

/**
* Get delivery date
Expand Down
12 changes: 6 additions & 6 deletions test/Expectations/Crud/81/v1/Api/Data/ColumnsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public function getId(): ?int;
public function setId($id): self;

/**
* Get is active
* Is active
*/
public function getIsActive(): bool;
public function isActive(): bool;

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self;
public function setActive(bool $isActive): self;

/**
* Get has flag
* Has flag
*/
public function getHasFlag(): ?bool;
public function hasFlag(): ?bool;

/**
* Set has flag
Expand Down
10 changes: 5 additions & 5 deletions test/Expectations/Crud/81/v1/Api/Data/PosterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface PosterInterface extends IdentityInterface
public const NAME = 'name';
public const SUBNAME = 'subname';
public const FAMILY = 'family';
public const IS_ACTIVE = 'is_active';
public const ACTIVE = 'active';
public const PRODUCT_ID = 'product_id';

/**
Expand Down Expand Up @@ -67,16 +67,16 @@ public function getFamily(): ?string;
public function setFamily(?string $family): self;

/**
* Get is active
* Is active
* @return bool
*/
public function getIsActive(): bool;
public function isActive(): bool;

/**
* Set is active
* Set active
* @return $this
*/
public function setIsActive(bool $isActive): self;
public function setActive(bool $active): self;

/**
* Get product id
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Api/Data/TicketInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function getName(): ?string;
public function setName(?string $name): self;

/**
* Get is active
* Is active
*/
public function getIsActive(): bool;
public function isActive(): bool;

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self;
public function setActive(bool $isActive): self;

/**
* Get ID
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Model/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ public function setCategoryId(int $categoryId): self
}

/**
* Get is active
* Is active
*/
public function getIsActive(): bool
public function isActive(): bool
{
return $this->getData(self::IS_ACTIVE);
}

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self
public function setActive(bool $isActive): self
{
return $this->setData(self::IS_ACTIVE, $isActive);
}
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Model/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public function setCartId(?int $cartId): self
}

/**
* Get is active
* Is active
*/
public function getIsActive(): bool
public function isActive(): bool
{
return $this->getData(self::IS_ACTIVE);
}

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self
public function setActive(bool $isActive): self
{
return $this->setData(self::IS_ACTIVE, $isActive);
}
Expand Down
12 changes: 6 additions & 6 deletions test/Expectations/Crud/81/v1/Model/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ public function setId($id): self
}

/**
* Get is active
* Is active
*/
public function getIsActive(): bool
public function isActive(): bool
{
return $this->getData(self::IS_ACTIVE);
}

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self
public function setActive(bool $isActive): self
{
return $this->setData(self::IS_ACTIVE, $isActive);
}

/**
* Get has flag
* Has flag
*/
public function getHasFlag(): ?bool
public function hasFlag(): ?bool
{
return $this->getData(self::HAS_FLAG);
}
Expand Down
12 changes: 6 additions & 6 deletions test/Expectations/Crud/81/v1/Model/Poster.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ public function setFamily(?string $family): self
}

/**
* Get is active
* Is active
*/
public function getIsActive(): bool
public function isActive(): bool
{
return $this->getData(self::IS_ACTIVE);
return $this->getData(self::ACTIVE);
}

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self
public function setActive(bool $active): self
{
return $this->setData(self::IS_ACTIVE, $isActive);
return $this->setData(self::ACTIVE, $active);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/Expectations/Crud/81/v1/Model/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public function setName(?string $name): self
}

/**
* Get is active
* Is active
*/
public function getIsActive(): bool
public function isActive(): bool
{
return $this->getData(self::IS_ACTIVE);
}

/**
* Set is active
* Set active
*/
public function setIsActive(bool $isActive): self
public function setActive(bool $isActive): self
{
return $this->setData(self::IS_ACTIVE, $isActive);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Expectations/Crud/81/v1/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<column xsi:type="varchar" name="name" nullable="true" length="255" comment="Name"/>
<column xsi:type="varchar" name="subname" nullable="true" length="255" comment="Subname"/>
<column xsi:type="varchar" name="family" nullable="true" length="255" comment="Family"/>
<column xsi:type="boolean" name="is_active" nullable="false" comment="Is_active"/>
<column xsi:type="boolean" name="active" nullable="false" comment="Active"/>
<column xsi:type="int" name="product_id" nullable="true" identity="false" unsigned="true" comment="Product_id"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="id"/>
Expand Down
Loading

0 comments on commit 99a1ed9

Please sign in to comment.