Completed
Push — master ( d2d28e...1c2760 )
by mw
35:37
created

PropertyAnnotator/PredefinedPropertyAnnotator.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\PropertyAnnotator;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use SMW\PageInfo;
8
use SMW\PropertyAnnotator;
9
use SMWDataItem as DataItem;
10
use SMWDIBlob as DIBlob;
11
use SMWDIBoolean as DIBoolean;
12
use SMWDITime as DITime;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 1.9
17
 *
18
 * @author mwjames
19
 */
20
class PredefinedPropertyAnnotator extends PropertyAnnotatorDecorator {
21
22
	/**
23
	 * @var PageInfo
24
	 */
25
	private $pageInfo;
26
27
	/**
28
	 * @var array
29
	 */
30
	private $predefinedPropertyList = array();
31
32
	/**
33
	 * @since 1.9
34
	 *
35
	 * @param PropertyAnnotator $propertyAnnotator
36
	 * @param PageInfo $pageInfo
37
	 */
38 204
	public function __construct( PropertyAnnotator $propertyAnnotator, PageInfo $pageInfo ) {
39 204
		parent::__construct( $propertyAnnotator );
40 204
		$this->pageInfo = $pageInfo;
41 204
	}
42
43
	/**
44
	 * @since 2.3
45
	 *
46
	 * @param array $predefinedPropertyList
47
	 */
48 203
	public function setPredefinedPropertyList( array $predefinedPropertyList ) {
49 203
		$this->predefinedPropertyList = $predefinedPropertyList;
50 203
	}
51
52 203
	protected function addPropertyValues() {
53
54 203
		$cachedProperties = array();
55
56 203
		foreach ( $this->predefinedPropertyList as $propertyId ) {
57
58 202
			if ( $this->isRegisteredPropertyId( $propertyId, $cachedProperties ) ) {
59 1
				continue;
60
			}
61
62 201
			$propertyDI = new DIProperty( $propertyId );
63
64 201
			if ( $this->getSemanticData()->getPropertyValues( $propertyDI ) !== array() ) {
65 176
				$cachedProperties[$propertyId] = true;
66 176
				continue;
67
			}
68
69 201
			$dataItem = $this->createDataItemByPropertyId( $propertyId );
70
71 201
			if ( $dataItem instanceof DataItem ) {
72 195
				$cachedProperties[$propertyId] = true;
73 201
				$this->getSemanticData()->addPropertyObjectValue( $propertyDI, $dataItem );
74
			}
75
		}
76 203
	}
77
78 202
	protected function isRegisteredPropertyId( $propertyId, $cachedProperties ) {
79 202
		return ( DIProperty::getPredefinedPropertyTypeId( $propertyId ) === '' ) ||
80 202
			array_key_exists( $propertyId, $cachedProperties );
81
	}
82
83 201
	protected function createDataItemByPropertyId( $propertyId ) {
84
85 201
		$dataItem = null;
86
87
		switch ( $propertyId ) {
88 201
			case DIProperty::TYPE_MODIFICATION_DATE :
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
89 186
				$dataItem = DITime::newFromTimestamp( $this->pageInfo->getModificationDate() );
90 186
				break;
91 16
			case DIProperty::TYPE_CREATION_DATE :
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
92 1
				$dataItem = DITime::newFromTimestamp( $this->pageInfo->getCreationDate() );
93 1
				break;
94 15
			case DIProperty::TYPE_NEW_PAGE :
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
95 2
				$dataItem = new DIBoolean( $this->pageInfo->isNewPage() );
96 2
				break;
97 13
			case DIProperty::TYPE_LAST_EDITOR :
0 ignored issues
show
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
98 2
				$dataItem = $this->pageInfo->getLastEditor() ? DIWikiPage::newFromTitle( $this->pageInfo->getLastEditor() ) : null;
99 2
				break;
100 11
			case DIProperty::TYPE_MEDIA : // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
101 7
				$dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMediaType() !== '' && $this->pageInfo->getMediaType() !== null ? new DIBlob( $this->pageInfo->getMediaType() ) : null;
102
				// @codingStandardsIgnoreEnd
103 7
				break;
104 7
			case DIProperty::TYPE_MIME : // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
105 7
				$dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMimeType() !== '' && $this->pageInfo->getMimeType() !== null  ? new DIBlob( $this->pageInfo->getMimeType() ) : null;
106
				// @codingStandardsIgnoreEnd
107 7
				break;
108
		}
109
110 201
		return $dataItem;
111
	}
112
113
}
114