Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changed types and annotations #242

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,34 @@ abstract class AbstractModel implements ModelInterface
* model is queried, it will load its referenced
* models together.
*
* @var array
* @var string[]
*/
public $with = [];
public array $with = [];

/**
* The $dynamic property tells if the object will accept additional fields
* that are not specified in the $fillable or $guarded properties.
* This is useful if you do not have a strict document format or
* if you want to take full advantage of the "schemaless" nature of MongoDB.
*
* @var bool
*/
protected $dynamic = true;
protected bool $dynamic = true;

/**
* Whether the model should manage the `created_at` and `updated_at`
* timestamps automatically.
*
* @var bool
*/
protected $timestamps = true;
protected bool $timestamps = true;

/**
* Name of the collection where this kind of Model is going to be saved or
* retrieved from.
*
* @var string
*/
protected $collection = null;
protected ?string $collection = null;

/**
* @see https://docs.mongodb.com/manual/reference/write-concern/
*
* @var int
*/
protected $writeConcern = 1;
protected int $writeConcern = 1;

/**
* Gets a cursor of this kind of entities that matches the query from the
Expand Down Expand Up @@ -93,10 +85,8 @@ public static function all(): CursorInterface
* @param mixed $query mongoDB selection criteria
* @param array $projection fields to project in Mongo query
* @param boolean $useCache retrieves the first through a CacheableCursor
*
* @return AbstractModel|null
*/
public static function first($query = [], array $projection = [], bool $useCache = false)
public static function first(mixed $query = [], array $projection = [], bool $useCache = false): ?static
{
return self::getBuilderInstance()->first(new static(), $query, $projection, $useCache);
}
Expand All @@ -110,10 +100,8 @@ public static function first($query = [], array $projection = [], bool $useCache
* @param boolean $useCache retrieves the first through a CacheableCursor
*
* @throws ModelNotFoundException If no document was found
*
* @return AbstractModel|null
*/
public static function firstOrFail($query = [], array $projection = [], bool $useCache = false)
public static function firstOrFail(mixed $query = [], array $projection = [], bool $useCache = false): ?static
{
return self::getBuilderInstance()->firstOrFail(new static(), $query, $projection);
}
Expand All @@ -124,10 +112,8 @@ public static function firstOrFail($query = [], array $projection = [], bool $us
* _if field filled.
*
* @param mixed $id document id
*
* @return AbstractModel|null
*/
public static function firstOrNew($id)
public static function firstOrNew(mixed $id): ?static
{
if (!$model = self::first($id)) {
$model = new static();
Expand Down Expand Up @@ -196,10 +182,8 @@ public function fresh(): self
* Dynamically retrieve attributes on the model.
*
* @param string $key name of the attribute
*
* @return mixed
*/
public function &__get(string $key)
public function &__get(string $key): mixed
{
return $this->getDocumentAttribute($key);
}
Expand All @@ -210,7 +194,7 @@ public function &__get(string $key)
* @param string $key attribute name
* @param mixed $value value to be set
*/
public function __set(string $key, $value): void
public function __set(string $key, mixed $value): void
{
$this->setDocumentAttribute($key, $value);
}
Expand Down
62 changes: 18 additions & 44 deletions src/Model/AttributesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,45 @@ class AttributesService
* the attributes specified here will be changed
* with the setDocumentAttribute method.
*
* @var array
* @var string[]
*/
protected $fillable = [];
protected array $fillable = [];

/**
* The attributes that are not mass assignable. The opposite
* to the fillable array;.
*
* @var array
* @var string[]
*/
protected $guarded = [];
protected array $guarded = [];

/**
* Check if model should mutate attributes checking
* the existence of a specific method on model
* class. Default is false.
*
* @var bool
*/
protected $mutable = false;
protected bool $mutable = false;

/**
* Store mutable attribute values to work with `&__get()`.
*
* @var array
* @var string[]
*/
protected $mutableCache = [];
protected array $mutableCache = [];

/**
* The model's attributes.
*
* @var array
* @var string[]
*/
private $attributes = [];
private array $attributes = [];

/**
* The model attribute's original state.
*
* @var array
* @var array<string,mixed>
*/
private $originalAttributes = [];
private array $originalAttributes = [];

/**
* {@inheritdoc}
Expand All @@ -68,7 +66,7 @@ public static function fill(
if ($object instanceof PolymorphableModelInterface) {
$class = $object->polymorph(array_merge($object->getDocumentAttributes(), $input));

if ($class !== get_class($object)) {
if ($class !== $object::class) {
$originalAttributes = $object->getDocumentAttributes();
$object = new $class();

Expand All @@ -92,18 +90,12 @@ public static function fill(
return $object;
}

/**
* {@inheritdoc}
*/
public function hasDocumentAttribute(string $key): bool
{
return !is_null($this->getDocumentAttribute($key));
}

/**
* {@inheritdoc}
*/
public function &getDocumentAttribute(string $key)
public function &getDocumentAttribute(string $key): mixed
{
if ($this->mutable && $this->hasMutatorMethod($key, 'get')) {
$this->mutableCache[$key] = $this->{$this->buildMutatorMethod($key, 'get')}();
Expand All @@ -124,9 +116,6 @@ public function &getDocumentAttribute(string $key)
return $this->attributes[$key];
}

/**
* {@inheritdoc}
*/
public function getDocumentAttributes(): array
{
foreach ($this->attributes as $field => $value) {
Expand All @@ -138,10 +127,7 @@ public function getDocumentAttributes(): array
return $this->attributes ?? [];
}

/**
* {@inheritdoc}
*/
public function cleanDocumentAttribute(string $key)
public function cleanDocumentAttribute(string $key): void
{
unset($this->attributes[$key]);

Expand All @@ -150,10 +136,7 @@ public function cleanDocumentAttribute(string $key)
}
}

/**
* {@inheritdoc}
*/
public function setDocumentAttribute(string $key, $value)
public function setDocumentAttribute(string $key, $value): void
{
if ($this->mutable && $this->hasMutatorMethod($key, 'set')) {
$value = $this->{$this->buildMutatorMethod($key, 'set')}($value);
Expand All @@ -172,29 +155,20 @@ public function setDocumentAttribute(string $key, $value)
}
}

/**
* {@inheritdoc}
*/
public function syncOriginalDocumentAttributes()
public function syncOriginalDocumentAttributes(): void
{
try {
$this->originalAttributes = unserialize(serialize($this->getDocumentAttributes()));
} catch (Exception $e) {
} catch (Exception) {
$this->originalAttributes = $this->getDocumentAttributes();
}
}

/**
* {@inheritdoc}
*/
public function getOriginalDocumentAttributes(): array
{
return $this->originalAttributes;
}

/**
* {@inheritdoc}
*/
public function toArray(): array
{
return $this->getDocumentAttributes();
Expand All @@ -206,7 +180,7 @@ public function toArray(): array
* @param string $key attribute name
* @param string $prefix method prefix to be used (get, set)
*/
protected function hasMutatorMethod(string $key, $prefix): bool
protected function hasMutatorMethod(string $key, string $prefix): bool
{
$method = $this->buildMutatorMethod($key, $prefix);

Expand Down
13 changes: 6 additions & 7 deletions src/Model/DocumentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DocumentEmbedder
*
* @return bool Success
*/
public function embed($parent, string $field, &$entity): bool
public function embed(mixed $parent, string $field, mixed &$entity): bool
{
// In order to update the document if it exists inside the $parent
$this->unembed($parent, $field, $entity);
Expand All @@ -42,7 +42,7 @@ public function embed($parent, string $field, &$entity): bool
*
* @return bool Success
*/
public function unembed($parent, string $field, &$entity): bool
public function unembed(mixed $parent, string $field, mixed &$entity): bool
{
$fieldValue = (array) $parent->$field;
$id = $this->getId($entity);
Expand All @@ -63,11 +63,11 @@ public function unembed($parent, string $field, &$entity): bool
*
* @param mixed $parent the object where $entity will be referenced
* @param string $field the field where the _id reference of $entity will be stored
* @param object|array $entity the object that is being attached
* @param mixed $entity the object that is being attached
*
* @return bool Success
*/
public function attach($parent, string $field, &$entity): bool
public function attach(mixed $parent, string $field, mixed &$entity): bool
{
$fieldValue = (array) $parent->$field;
$newId = $this->getId($entity);
Expand All @@ -93,7 +93,7 @@ public function attach($parent, string $field, &$entity): bool
*
* @return bool Success
*/
public function detach($parent, string $field, &$entity): bool
public function detach(mixed $parent, string $field, mixed &$entity): bool
{
$fieldValue = (array) $parent->$field;
$newId = $this->getId($entity);
Expand All @@ -115,9 +115,8 @@ public function detach($parent, string $field, &$entity): bool
*
* @param mixed $object the object|array that the _id will be retrieved from
*
* @return ObjectId|mixed
*/
protected function getId(&$object)
protected function getId(mixed &$object): mixed
{
if (is_array($object)) {
if (isset($object['_id']) && $object['_id']) {
Expand Down
12 changes: 3 additions & 9 deletions src/Model/Exception/ModelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ class ModelNotFoundException extends RuntimeException

/**
* Name of the affected Mongolid model.
*
* @var string
*/
protected $model;
protected string $model;

/**
* Set the affected Mongolid model.
*
* @param string $model name of the model
*
* @return $this
*/
public function setModel(string $model)
public function setModel(string $model): self
{
$this->model = $model;

Expand All @@ -40,10 +36,8 @@ public function setModel(string $model)

/**
* Get the affected Mongolid model.
*
* @return string
*/
public function getModel()
public function getModel(): string
{
return $this->model;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Model/HasAttributesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ public function hasDocumentAttribute(string $key): bool;
* Get an attribute from the model.
*
* @param string $key the attribute to be accessed
*
* @return mixed
*/
public function getDocumentAttribute(string $key);
public function getDocumentAttribute(string $key): mixed;

/**
* Get all attributes from the model.
Expand All @@ -37,15 +35,15 @@ public function getDocumentAttributes(): array;
*
* @param string $key name of the attribute to be unset
*/
public function cleanDocumentAttribute(string $key);
public function cleanDocumentAttribute(string $key): void;

/**
* Set a given attribute on the model.
*
* @param string $key name of the attribute to be set
* @param mixed $value value to be set
*/
public function setDocumentAttribute(string $key, $value);
public function setDocumentAttribute(string $key, mixed $value): void;

/**
* Stores original attributes from actual data from attributes
Expand All @@ -54,7 +52,7 @@ public function setDocumentAttribute(string $key, $value);
* Ideally should be called once right after retrieving data from
* the database.
*/
public function syncOriginalDocumentAttributes();
public function syncOriginalDocumentAttributes(): void;

/**
* Retrieve original attributes.
Expand Down
Loading
Loading