Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokenizer behavior #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
609 changes: 609 additions & 0 deletions doc/tokenizer.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/Gedmo/Mapping/Annotation/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
include __DIR__.'/Timestampable.php';
include __DIR__.'/Blameable.php';
include __DIR__.'/IpTraceable.php';
include __DIR__.'/Tokenizer.php';
include __DIR__.'/Translatable.php';
include __DIR__.'/TranslationEntity.php';
include __DIR__.'/Tree.php';
Expand Down
25 changes: 25 additions & 0 deletions lib/Gedmo/Mapping/Annotation/Tokenizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Gedmo\Mapping\Annotation;

use Doctrine\Common\Annotations\Annotation;

/**
* Tokenizer annotation for Tokenizer behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Tokenizer extends Annotation
{
/** @var string */
public $on = 'update';
/** @var string|array */
public $field;
/** @var mixed */
public $value;
}

77 changes: 77 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Driver;

use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
Doctrine\Common\Annotations\AnnotationReader,
Gedmo\Exception\InvalidMappingException;

/**
* This is an annotation mapping driver for Tokenizer
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for Tokenizer
* extension.
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Annotation extends AbstractAnnotationDriver
{
/**
* Annotation field is Tokenizer
*/
const TOKENIZER = 'Gedmo\\Mapping\\Annotation\\Tokenizer';

/**
* List of types which are valid for timestamp
*
* @var array
*/
protected $validTypes = array(
'string',
);

/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config) {
$class = $this->getMetaReflectionClass($meta);

// property annotations
foreach ($class->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate() ||
$meta->isInheritedField($property->name) ||
isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
if ($tokenizer = $this->reader->getPropertyAnnotation($property, self::TOKENIZER)) {
$field = $property->getName();
if (!$meta->hasField($field)) {
throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
}
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
}
if (!in_array($tokenizer->on, array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($tokenizer->on == 'change') {
if (!isset($tokenizer->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
if (is_array($tokenizer->field) && isset($tokenizer->value)) {
throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $tokenizer->field,
'value' => $tokenizer->value,
);
}
// properties are unique and mapper checks that, no risk here
$config[$tokenizer->on][] = $field;
}
}
}
}
93 changes: 93 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Driver;

use Gedmo\Mapping\Driver\Xml as BaseXml,
Gedmo\Exception\InvalidMappingException;

/**
* This is a xml mapping driver for Timestampable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for Timestampable
* extension.
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Xml extends BaseXml
{

/**
* List of types which are valid for timestamp
*
* @var array
*/
private $validTypes = array(
'string',
);

/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement $mapping
*/
$mapping = $this->_getMapping($meta->name);

if (isset($mapping->field)) {
/**
* @var \SimpleXmlElement $fieldMapping
*/
foreach ($mapping->field as $fieldMapping) {
$fieldMappingDoctrine = $fieldMapping;
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->timestampable)) {
/**
* @var \SimpleXmlElement $data
*/
$data = $fieldMapping->timestampable;

$field = $this->_getAttribute($fieldMappingDoctrine, 'name');
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
}
if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}

if ($this->_getAttribute($data, 'on') == 'change') {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
}

/**
* Checks if $field type is valid
*
* @param object $meta
* @param string $field
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}
94 changes: 94 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Driver;

use Gedmo\Mapping\Driver\File,
Gedmo\Mapping\Driver,
Gedmo\Exception\InvalidMappingException;

/**
* This is a yaml mapping driver for Timestampable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for Timestampable
* extension.
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Yaml extends File implements Driver
{
/**
* File extension
* @var string
*/
protected $_extension = '.dcm.yml';

/**
* List of types which are valid for timestamp
*
* @var array
*/
private $validTypes = array(
'string',
);

/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$mapping = $this->_getMapping($meta->name);

if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['timestampable'])) {
$mappingProperty = $fieldMapping['gedmo']['timestampable'];
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
}
if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}

if ($mappingProperty['on'] == 'change') {
if (!isset($mappingProperty['field'])) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $mappingProperty['field'];
$valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$mappingProperty['on']][] = $field;
}
}
}
}

/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse($file);
}

/**
* Checks if $field type is valid
*
* @param object $meta
* @param string $field
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}
31 changes: 31 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Event\Adapter;

use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
use Gedmo\Tokenizer\Mapping\Event\TokenizerAdapter;

/**
* Doctrine event adapter for ODM adapted
* for Tokenizer behavior
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ODM extends BaseAdapterODM implements TokenizerAdapter
{
/**
* {@inheritDoc}
*/
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
if (isset($mapping['type']) && $mapping['type'] === 'string') {
$date = new \DateTime();
$token =base_convert(sha1(uniqid(mt_rand(1, 999) . $date->format('Y-m-d H:i:s'), true)), 16, 36);

return $token;
}
return null;
}
}
31 changes: 31 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Event\Adapter;

use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Tokenizer\Mapping\Event\TokenizerAdapter;

/**
* Doctrine event adapter for ORM adapted
* for Tokenizer behavior
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ORM extends BaseAdapterORM implements TokenizerAdapter
{
/**
* {@inheritDoc}
*/
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
if (isset($mapping['type']) && $mapping['type'] === 'string') {
$date = new \DateTime();
$token =base_convert(sha1(uniqid(mt_rand(1, 999) . $date->format('Y-m-d H:i:s'), true)), 16, 36);

return $token;
}
return null;
}
}
24 changes: 24 additions & 0 deletions lib/Gedmo/Tokenizer/Mapping/Event/TokenizerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Gedmo\Tokenizer\Mapping\Event;

use Gedmo\Mapping\Event\AdapterInterface;

/**
* Doctrine event adapter interface
* for Tokenizer behavior
*
* @author Ceif Khedhiri <ceif@khedhiri.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface TokenizerAdapter extends AdapterInterface
{
/**
* Get the date value
*
* @param object $meta
* @param string $field
* @return mixed
*/
function getDateValue($meta, $field);
}
Loading