|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Nikola Kostadinov<[email protected]> |
|
4
|
|
|
* Date: 19.10.2014 |
|
5
|
|
|
* Time: 10:46 ч. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace nkostadinov\taxonomy; |
|
9
|
|
|
|
|
10
|
|
|
use nkostadinov\taxonomy\components\exceptions\TermNotDefinedException; |
|
11
|
|
|
use nkostadinov\taxonomy\components\terms\BaseTerm; |
|
12
|
|
|
use nkostadinov\taxonomy\components\terms\PropertyTerm; |
|
13
|
|
|
use nkostadinov\taxonomy\components\terms\TagTerm; |
|
14
|
|
|
use nkostadinov\taxonomy\models\TaxonomyDef; |
|
15
|
|
|
use nkostadinov\taxonomy\models\TaxonomyTerms; |
|
16
|
|
|
use yii\base\Component; |
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
use yii\db\Connection; |
|
19
|
|
|
use yii\db\Migration; |
|
20
|
|
|
use yii\db\Schema; |
|
21
|
|
|
use yii\log\Logger; |
|
22
|
|
|
|
|
23
|
|
|
class Taxonomy extends Component |
|
24
|
|
|
{ |
|
25
|
|
|
/* @var Connection The db connection component */ |
|
26
|
|
|
public $db = 'db'; |
|
27
|
|
|
public $table = 'taxonomy'; |
|
28
|
|
|
//cache array of initialized terms |
|
29
|
|
|
private $_taxonomy = []; |
|
30
|
|
|
// |
|
31
|
|
|
public $definitions = []; |
|
32
|
|
|
|
|
33
|
|
|
public function isTermInstalled($termName) |
|
34
|
|
|
{ |
|
35
|
|
|
$term = $this->getTerm($termName); |
|
36
|
|
|
return $term->isInstalled(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function addTerm($term, $object_id, $params) |
|
40
|
|
|
{ |
|
41
|
|
|
$term = $this->getTerm($term); |
|
42
|
|
|
$term->addTerm($object_id, $params); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function removeTerm($term, $object_id, $params = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$term = $this->getTerm($term); |
|
48
|
|
|
return $term->removeTerm($object_id, $params); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getTerms($term, $object_id, $name = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$term = $this->getTerm($term); |
|
54
|
|
|
return $term->getTerms($object_id, $name); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $termName |
|
59
|
|
|
* @return BaseTerm |
|
60
|
|
|
* @throws InvalidConfigException |
|
61
|
|
|
* @throws TermNotDefinedException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getTerm($termName, $reload = false) |
|
64
|
|
|
{ |
|
65
|
|
|
if(!isset($this->_taxonomy[$termName]) or $reload) { |
|
|
|
|
|
|
66
|
|
|
$tax = TaxonomyDef::findOne(['name' => $termName]); |
|
67
|
|
|
\Yii::getLogger()->log("Initialising term $termName", Logger::LEVEL_INFO, 'nkostadinov.taxonomy.terms'); |
|
68
|
|
|
$this->_taxonomy[$termName] = \Yii::createObject($tax->attributes); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->_taxonomy[$termName]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function isInstalled() |
|
74
|
|
|
{ |
|
75
|
|
|
return \Yii::$app->db->getTableSchema(TaxonomyDef::tableName(), true) !== null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getDefinitions() |
|
82
|
|
|
{ |
|
83
|
|
|
return array_merge( |
|
84
|
|
|
[TagTerm::className(), PropertyTerm::className()], |
|
85
|
|
|
$this->definitions |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.