Completed
Push — master ( 0e37ee...600d95 )
by Peter
06:08
created

Document::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan;
15
16
use Maslosoft\Mangan\Interfaces\ActiveRecordInterface;
17
use Maslosoft\Mangan\Sanitizers\MongoObjectId;
18
use Maslosoft\Mangan\Sanitizers\MongoStringId;
19
use Maslosoft\Mangan\Traits\CollectionNameTrait;
20
use Maslosoft\Mangan\Traits\EntityManagerTrait;
21
use Maslosoft\Mangan\Traits\FinderTrait;
22
use Maslosoft\Mangan\Traits\WithCriteriaTrait;
23
use MongoDB;
24
use MongoId;
25
26
/**
27
 * Document
28
 *
29
 * @author Ianaré Sévi
30
 * @author Dariusz Górecki <[email protected]>
31
 * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
32
 * @copyright 2011 CleverIT http://www.cleverit.com.pl
33
 * @property-read MongoDB $db
34
 * @since v1.0
35
 */
36
abstract class Document extends EmbeddedDocument implements ActiveRecordInterface
37
{
38
39
	use EntityManagerTrait,
40
	  FinderTrait,
41
	  CollectionNameTrait,
42
	  WithCriteriaTrait;
43
44
	/**
45
	 * Mongo id field
46
	 * @Label('Database ID')
47
	 * @Sanitizer(MongoObjectId)
48
	 * @see MongoObjectId
49
	 * @var MongoId|null
50
	 */
51
	public $_id = null;
52
53
	/**
54
	 * Alias to _id
55
	 * @Label('Database ID')
56
	 * @Persistent(false)
57
	 * @Alias('_id')
58
	 * @see https://github.com/Maslosoft/Mangan/issues/40
59
	 * @var string|null
60
	 */
61
	public $id = null;
62
63
	/**
64
	 * Constructor
65
	 * @see ScenarioManager
66
	 *
67
	 * @param string $scenario
68
	 * @param string $lang Language code
69
	 * @since v1.0
70
	 */
71 56
	public function __construct($scenario = 'insert', $lang = '')
72
	{
73 56
		$this->_id = new MongoId;
74 56
		$this->_class = get_class($this);
75 56
		$this->setLang($lang);
76
77 56
		$this->setScenario($scenario);
78 56
	}
79
80
	/**
81
	 * Returns the empty model of the specified Document class.
82
	 * It is provided for invoking class-level methods, espacially userfull for finders.
83
	 *
84
	 * Example usage:
85
	 * ```php
86
	 * $user = User::model()->findByPk('5612470866a19540308b4568');
87
	 * ```
88
	 * @param string $lang
89
	 * @return Document model instance.
90
	 */
91
	public static function model($lang = null)
92
	{
93
		return new static(null, $lang);
94
	}
95
96
}
97