Skip to content

Commit

Permalink
Merge pull request #62 from alex-mashin/CALLABLE
Browse files Browse the repository at this point in the history
This change addresses the feature request #61.
  • Loading branch information
JeroenDeDauw committed Sep 18, 2020
2 parents 2e6d8d0 + 9917cd9 commit 858ed9b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private function addCallbackHandlers( $store, $options ) {
);

$propertyValuesContentAggregator = new PropertyValuesContentAggregator(
$lazySemanticDataLookup
$lazySemanticDataLookup,
$outputPage
);

$propertyValuesContentAggregator->useFallbackChainForMultipleProperties(
Expand Down
2 changes: 2 additions & 0 deletions src/MetaTagsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private function addMetaTagsForAggregatedProperties( $tag, $properties ) {

if ( is_string( $properties ) ) {
$properties = explode( ',', $properties );
} elseif ( is_callable( $properties ) ) {
$properties = [ $properties ];
}

$content = $this->propertyValuesContentAggregator->doAggregateFor(
Expand Down
45 changes: 33 additions & 12 deletions src/PropertyValuesContentAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class PropertyValuesContentAggregator {
*/
private $lazySemanticDataLookup;

/**
* @var \OutputPage $mOutputPage
*/
private $mOutputPage;

/**
* Whether multiple properties should be used through a fallback chain where
* the first available property with content will determine the end of the
Expand All @@ -33,9 +38,11 @@ class PropertyValuesContentAggregator {
* @since 1.0
*
* @param LazySemanticDataLookup $lazySemanticDataLookup
* @param OutputPage $outputPage
*/
public function __construct( LazySemanticDataLookup $lazySemanticDataLookup ) {
public function __construct( LazySemanticDataLookup $lazySemanticDataLookup, \OutputPage $outputPage ) {
$this->lazySemanticDataLookup = $lazySemanticDataLookup;
$this->mOutputPage = $outputPage;
}

/**
Expand Down Expand Up @@ -66,27 +73,41 @@ public function doAggregateFor( array $propertyNames ) {
break;
}

$this->fetchContentForProperty( trim( $property ), $values );
if ( is_string( $property ) ) {
$property = trim( $property );
}
$this->fetchContentForProperty( $property, $values );
}

return implode( ',', $values );
}

private function fetchContentForProperty( $property, &$values ) {
private function fetchContentForProperty( $property, array &$values ) {

$property = DIProperty::newFromUserLabel( $property );
$semanticData = $this->lazySemanticDataLookup->getSemanticData();

$this->iterateToCollectPropertyValues(
$semanticData->getPropertyValues( $property ),
$values
);
if ( is_callable( $property ) ) {
// This is actually a callback function.
$result = $property( $this->mOutputPage );
if ( $result ) {
foreach ( (array)$result as $value ) {
$values[$value] = (string)$value;
}
}
} else {
// This is a real property.
$property = DIProperty::newFromUserLabel( $property );
$semanticData = $this->lazySemanticDataLookup->getSemanticData();

foreach ( $semanticData->getSubSemanticData() as $subSemanticData ) {
$this->iterateToCollectPropertyValues(
$subSemanticData->getPropertyValues( $property ),
$semanticData->getPropertyValues( $property ),
$values
);

foreach ( $semanticData->getSubSemanticData() as $subSemanticData ) {
$this->iterateToCollectPropertyValues(
$subSemanticData->getPropertyValues( $property ),
$values
);
}
}
}

Expand Down
30 changes: 25 additions & 5 deletions tests/phpunit/Unit/PropertyValuesContentAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public function testCanConstruct() {
->disableOriginalConstructor()
->getMock();

$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();

$this->assertInstanceOf(
'\SMT\PropertyValuesContentAggregator',
new PropertyValuesContentAggregator( $lazySemanticDataLookup )
new PropertyValuesContentAggregator( $lazySemanticDataLookup, $outputPage )
);
}

Expand Down Expand Up @@ -56,7 +60,11 @@ public function testFindContentForProperty() {
->method( 'getSemanticData' )
->will( $this->returnValue( $semanticData ) );

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup );
$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup, $outputPage );

$this->assertSame(
'Foo',
Expand Down Expand Up @@ -93,7 +101,11 @@ public function testAggregatePropertyValueContentWithSameHash() {
->method( 'getSemanticData' )
->will( $this->returnValue( $semanticData ) );

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup );
$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup, $outputPage );

$this->assertSame(
'http://username@example.org/foo,Foo',
Expand Down Expand Up @@ -134,7 +146,11 @@ public function testFindContentForSubobjectProperty() {
->method( 'getSemanticData' )
->will( $this->returnValue( $semanticData ) );

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup );
$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup, $outputPage );

$this->assertSame(
'Foo-with-html-"<>"-escaping-to-happen-somewhere-else',
Expand Down Expand Up @@ -230,7 +246,11 @@ private function doAssertContentForMultipleProperties( $fallbackChainUsageState,
->method( 'getSemanticData' )
->will( $this->returnValue( $semanticData ) );

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup );
$outputPage = $this->getMockBuilder( 'OutputPage' )
->disableOriginalConstructor()
->getMock();

$instance = new PropertyValuesContentAggregator( $lazySemanticDataLookup, $outputPage );
$instance->useFallbackChainForMultipleProperties( $fallbackChainUsageState );

$this->assertSame(
Expand Down

0 comments on commit 858ed9b

Please sign in to comment.