This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JanZerebecki/master
further implementation
- Loading branch information
Showing
18 changed files
with
827 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Ask\Wikitext\Serializers\Description; | ||
|
||
use Serializers\DispatchableSerializer; | ||
use Ask\Language\Description\Conjunction; | ||
use Ask\Language\Description\Description; | ||
use InvalidArgumentException; | ||
use Ask\Wikitext\Serializers\Description\DescriptionCollectionSerializer; | ||
|
||
/** | ||
* @licence GNU GPL v2+ | ||
* @author Jan Zerebecki < jan.wikimedia@zerebecki.de > | ||
*/ | ||
class ConjunctionSerializer implements DispatchableSerializer { | ||
|
||
/** | ||
* @var DescriptionCollectionSerializer | ||
*/ | ||
private $collectionSerializer = null; | ||
|
||
/** | ||
* @see DispatchableSerializer::isSerializerFor | ||
*/ | ||
public function isSerializerFor( $object ) { | ||
return ( $object instanceof Conjunction ); | ||
} | ||
|
||
/** | ||
* @see Serializer::serialize | ||
*/ | ||
public function serialize( $object ) { | ||
if ( $this->isSerializerFor( $object ) ) { | ||
return $this->serializeConjunction( $object ); | ||
} | ||
|
||
throw new InvalidArgumentException( 'Can only serialize instances of Conjunction' ); | ||
} | ||
|
||
/** | ||
* @return DescriptionCollectionSerializer | ||
*/ | ||
private function serializer() { | ||
if (is_null($this->collectionSerializer)) { | ||
$this->collectionSerializer = new DescriptionCollectionSerializer(' '); | ||
} | ||
return $this->collectionSerializer; | ||
} | ||
|
||
private function serializeConjunction( Conjunction $conjunction ) { | ||
return $this->serializer()->serialize( $conjunction ); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
src/Serializers/Description/DescriptionCollectionSerializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Ask\Wikitext\Serializers\Description; | ||
|
||
use Serializers\DispatchableSerializer; | ||
use Ask\Language\Description\DescriptionCollection; | ||
use Ask\Language\Description\Description; | ||
use InvalidArgumentException; | ||
use Ask\Wikitext\Serializers\Factory; | ||
use Serializers\DispatchingSerializer; | ||
|
||
/** | ||
* @licence GNU GPL v2+ | ||
* @author Jan Zerebecki < jan.wikimedia@zerebecki.de > | ||
*/ | ||
class DescriptionCollectionSerializer implements DispatchableSerializer { | ||
|
||
/** | ||
* @var String | ||
*/ | ||
private $operator = ''; | ||
|
||
/** | ||
* @var DispatchingSerializer | ||
*/ | ||
private $descriptionSerializer = null; | ||
|
||
/** | ||
* @param string $operator used to concatenate the parts | ||
*/ | ||
public function __construct($operator) { | ||
$this->operator = $operator; | ||
} | ||
|
||
/** | ||
* @see DispatchableSerializer::isSerializerFor | ||
*/ | ||
public function isSerializerFor( $object ) { | ||
return ( $object instanceof DescriptionCollection ); | ||
} | ||
|
||
/** | ||
* @see Serializer::serialize | ||
*/ | ||
public function serialize( $object ) { | ||
if ( $this->isSerializerFor( $object ) ) { | ||
return $this->serializeCollection( $object ); | ||
} | ||
|
||
throw new InvalidArgumentException( 'Can only serialize instances of DescriptionCollection.' ); | ||
} | ||
|
||
private function serializeCollection( DescriptionCollection $collection ) { | ||
$result = ''; | ||
$firstItem = true; | ||
foreach ( $collection->getDescriptions() as $description) { | ||
if (!$firstItem) { | ||
$result .= $this->operator; | ||
} else { | ||
$firstItem = false; | ||
} | ||
$result .= $this->serializeDescription( $description ); | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* @return DispatchingSerializer | ||
*/ | ||
private function serializer() { | ||
if (is_null($this->descriptionSerializer)) { | ||
$factory = new Factory(); | ||
$this->descriptionSerializer = $factory->createDescriptionSerializer(); | ||
} | ||
return $this->descriptionSerializer; | ||
} | ||
|
||
private function serializeDescription( Description $description ) { | ||
return $this->serializer()->serialize( $description ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Ask\Wikitext\Serializers\Description; | ||
|
||
use Serializers\DispatchableSerializer; | ||
use Ask\Language\Description\Disjunction; | ||
use Ask\Language\Description\Description; | ||
use InvalidArgumentException; | ||
use Ask\Wikitext\Serializers\Description\DescriptionCollectionSerializer; | ||
|
||
/** | ||
* @licence GNU GPL v2+ | ||
* @author Jan Zerebecki < jan.wikimedia@zerebecki.de > | ||
*/ | ||
class DisjunctionSerializer implements DispatchableSerializer { | ||
|
||
/** | ||
* @var DescriptionCollectionSerializer | ||
*/ | ||
private $collectionSerializer = null; | ||
|
||
/** | ||
* @see DispatchableSerializer::isSerializerFor | ||
*/ | ||
public function isSerializerFor( $object ) { | ||
return ( $object instanceof Disjunction ); | ||
} | ||
|
||
/** | ||
* @see Serializer::serialize | ||
*/ | ||
public function serialize( $object ) { | ||
if ( $this->isSerializerFor( $object ) ) { | ||
return $this->serializeDisjunction( $object ); | ||
} | ||
|
||
throw new InvalidArgumentException( 'Can only serialize instances of Disjunction' ); | ||
} | ||
|
||
/** | ||
* @return DescriptionCollectionSerializer | ||
*/ | ||
private function serializer() { | ||
if (is_null($this->collectionSerializer)) { | ||
$this->collectionSerializer = new DescriptionCollectionSerializer(' OR '); | ||
} | ||
return $this->collectionSerializer; | ||
} | ||
|
||
private function serializeDisjunction( Disjunction $disjunction ) { | ||
return $this->serializer()->serialize( $disjunction ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Ask\Wikitext\Serializers; | ||
|
||
use Serializers\DispatchingSerializer; | ||
|
||
/** | ||
* @licence GNU GPL v2+ | ||
* @author Jan Zerebecki < jan.wikimedia@zerebecki.de > | ||
*/ | ||
class Factory { | ||
|
||
/** | ||
* @return DispatchingSerializer | ||
*/ | ||
public function createDescriptionSerializer() { | ||
$serializer = new DispatchingSerializer(); | ||
$serializer->addSerializer( new \Ask\Wikitext\Serializers\Description\AnyValueSerializer() ); | ||
$serializer->addSerializer( new \Ask\Wikitext\Serializers\Description\SomePropertySerializer() ); | ||
$serializer->addSerializer( new \Ask\Wikitext\Serializers\Description\ValueDescriptionSerializer() ); | ||
$serializer->addSerializer( new \Ask\Wikitext\Serializers\Description\ConjunctionSerializer() ); | ||
$serializer->addSerializer( new \Ask\Wikitext\Serializers\Description\DisjunctionSerializer() ); | ||
return $serializer; | ||
} | ||
|
||
} |
Oops, something went wrong.