Skip to content

Commit

Permalink
Upgrade to PHP 8.2, improve platform and schema interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alphasoft-fr committed Apr 17, 2024
1 parent 06eb060 commit 4c28b42
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 223 deletions.
Empty file modified phpinsights.php
100755 → 100644
Empty file.
196 changes: 98 additions & 98 deletions src/Mapping/Column.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
<?php

namespace AlphaSoft\AsLinkOrm\Mapping;

use AlphaSoft\AsLinkOrm\Types\Type;
use AlphaSoft\AsLinkOrm\Types\TypeFactory;

#[\Attribute(\Attribute::TARGET_CLASS|\Attribute::IS_REPEATABLE)]
class Column
{

public function __construct(
private readonly string $property,
private $defaultValue = null,
private readonly ?string $name = null,
private string $type = 'string',
private readonly bool $unique = false,
private readonly bool $nullable = false,
)
{
}

final public function __toString(): string
{
return $this->getProperty();
}

public function getProperty(): string
{
return $this->property;
}

final public function getName(): ?string
{
return $this->name ?: $this->getProperty();
}

/**
* @return mixed|null
*/
final public function getDefaultValue()
{
return $this->defaultValue;
}

public function getType(): string
{
return $this->type;
}

final public function type(string $type): self
{
$this->type = $type;
return $this;
}

public function isUnique(): bool
{
return $this->unique;
}

public function isNullable(): bool
{
return $this->nullable;
}

/**
* Converts a value to its corresponding database representation.
*
* @param mixed $value The value to be converted.
* @return mixed The converted value.
* @throws \ReflectionException
*/
final function convertToDatabase($value)
{
$type = $this->getType();
if (is_subclass_of($type, Type::class)) {
$value = TypeFactory::create($type)->convertToDatabase($value);
}
return $value;
}

/**
* Converts a value to its corresponding PHP representation.
*
* @param mixed $value The value to be converted.
* @return mixed The converted PHP value.
* @throws \ReflectionException
*/
final function convertToPHP($value)
{
$type = $this->getType();
if (is_subclass_of($type, Type::class)) {
$value = TypeFactory::create($type)->convertToPHP($value);
}
return $value;
}
}
<?php

namespace AlphaSoft\AsLinkOrm\Mapping;

use AlphaSoft\AsLinkOrm\Types\Type;
use AlphaSoft\AsLinkOrm\Types\TypeFactory;

#[\Attribute(\Attribute::TARGET_CLASS|\Attribute::IS_REPEATABLE)]
class Column
{

public function __construct(
private readonly string $property,
private $defaultValue = null,
private readonly ?string $name = null,
private string $type = 'string',
private readonly bool $unique = false,
private readonly bool $nullable = false,
)
{
}

final public function __toString(): string
{
return $this->getProperty();
}

public function getProperty(): string
{
return $this->property;
}

final public function getName(): ?string
{
return $this->name ?: $this->getProperty();
}

/**
* @return mixed|null
*/
final public function getDefaultValue()
{
return $this->defaultValue;
}

public function getType(): string
{
return $this->type;
}

final public function type(string $type): self
{
$this->type = $type;
return $this;
}

public function isUnique(): bool
{
return $this->unique;
}

public function isNullable(): bool
{
return $this->nullable;
}

/**
* Converts a value to its corresponding database representation.
*
* @param mixed $value The value to be converted.
* @return mixed The converted value.
* @throws \ReflectionException
*/
final function convertToDatabase($value)
{
$type = $this->getType();
if (is_subclass_of($type, Type::class)) {
$value = TypeFactory::create($type)->convertToDatabase($value);
}
return $value;
}

/**
* Converts a value to its corresponding PHP representation.
*
* @param mixed $value The value to be converted.
* @return mixed The converted PHP value.
* @throws \ReflectionException
*/
final function convertToPHP($value)
{
$type = $this->getType();
if (is_subclass_of($type, Type::class)) {
$value = TypeFactory::create($type)->convertToPHP($value);
}
return $value;
}
}
28 changes: 14 additions & 14 deletions src/Mapping/PrimaryKeyColumn.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace AlphaSoft\AsLinkOrm\Mapping;

use AlphaSoft\AsLinkOrm\Types\IntegerType;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class PrimaryKeyColumn extends Column
{
public function __construct(string $property, $defaultValue = null, string $name = null, string $type = IntegerType::class)
{
parent::__construct($property, $defaultValue, $name, $type);
}
}
<?php

namespace AlphaSoft\AsLinkOrm\Mapping;

use AlphaSoft\AsLinkOrm\Types\IntegerType;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class PrimaryKeyColumn extends Column
{
public function __construct(string $property, $defaultValue = null, string $name = null, string $type = IntegerType::class)
{
parent::__construct($property, $defaultValue, $name, $type);
}
}
108 changes: 54 additions & 54 deletions tests/Model/Post.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
<?php

namespace Test\AlphaSoft\AsLinkOrm\Model;

use AlphaSoft\AsLinkOrm\Mapping\Column;
use AlphaSoft\AsLinkOrm\Mapping\Entity;
use AlphaSoft\AsLinkOrm\Mapping\JoinColumn;
use AlphaSoft\AsLinkOrm\Mapping\PrimaryKeyColumn;
use AlphaSoft\AsLinkOrm\Entity\AsEntity;
use Test\AlphaSoft\AsLinkOrm\Repository\PostRepository;

#[Entity(table: "post", repositoryClass: PostRepository::class)]
#[PrimaryKeyColumn(property: 'id', type: 'int')]
#[Column(property: 'title')]
#[Column(property: 'content')]
#[JoinColumn(property: 'user', name: 'user_id', referencedColumnName: 'id', targetEntity: User::class)]
final class Post extends AsEntity
{
public function getPrimaryKeyValue(): ?int
{
return $this->get(self::getPrimaryKeyColumn());
}

public function setUser(User $user): self
{
$this->setRelatedOne('user', $user);
return $this;
}

public function getUser(): ?User
{
return $this->getRelatedOne('user');
}

public function getUserHasOneMethod(): ?User
{
return $this->hasOne(User::class, ['id' => $this->get('user_id')]);
}

// static public function getRepositoryName(): string
// {
// return PostRepository::class;
// }

// static public function columnsMapping(): array
// {
// return [
// new PrimaryKeyColumn('id'),
// new Column('title'),
// new Column('content'),
// new JoinColumn('user', 'user_id', 'id',User::class),
// ];
// }
}
<?php

namespace Test\AlphaSoft\AsLinkOrm\Model;

use AlphaSoft\AsLinkOrm\Mapping\Column;
use AlphaSoft\AsLinkOrm\Mapping\Entity;
use AlphaSoft\AsLinkOrm\Mapping\JoinColumn;
use AlphaSoft\AsLinkOrm\Mapping\PrimaryKeyColumn;
use AlphaSoft\AsLinkOrm\Entity\AsEntity;
use Test\AlphaSoft\AsLinkOrm\Repository\PostRepository;

#[Entity(table: "post", repositoryClass: PostRepository::class)]
#[PrimaryKeyColumn(property: 'id', type: 'int')]
#[Column(property: 'title')]
#[Column(property: 'content')]
#[JoinColumn(property: 'user', name: 'user_id', referencedColumnName: 'id', targetEntity: User::class)]
final class Post extends AsEntity
{
public function getPrimaryKeyValue(): ?int
{
return $this->get(self::getPrimaryKeyColumn());
}

public function setUser(User $user): self
{
$this->setRelatedOne('user', $user);
return $this;
}

public function getUser(): ?User
{
return $this->getRelatedOne('user');
}

public function getUserHasOneMethod(): ?User
{
return $this->hasOne(User::class, ['id' => $this->get('user_id')]);
}

// static public function getRepositoryName(): string
// {
// return PostRepository::class;
// }

// static public function columnsMapping(): array
// {
// return [
// new PrimaryKeyColumn('id'),
// new Column('title'),
// new Column('content'),
// new JoinColumn('user', 'user_id', 'id',User::class),
// ];
// }
}
Loading

0 comments on commit 4c28b42

Please sign in to comment.