-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRef.php
More file actions
36 lines (28 loc) · 768 Bytes
/
Ref.php
File metadata and controls
36 lines (28 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace App\Model\Trait;
trait Ref
{
private string $ref = '';
public function getRef(): mixed
{
if ( ! $this->ref ) {
if ( isset( $this->entity ) && is_callable( [ $this->entity, 'getRef' ] ) ) {
$this->ref = (string) $this->entity->getRef();
}
}
return $this->ref;
}
public function createRef( $recreate = false, $prefix = '', $postfix = '' ): void
{
if ( ! $recreate && $this->getRef() ) {
return;
}
$ref = base_convert( time() * 1000, 10, 36 );
$ref = $ref . substr( base_convert( (float) rand() / (float) getrandmax(), 10, 36 ), 1 );
$ref = $prefix . $ref . $postfix;
if ( isset( $this->entity ) && is_callable( [ $this->entity, 'setRef' ] ) ) {
$this->entity->setRef( $ref );
}
$this->ref = $ref;
}
}