PHP code example of juliangut / mapping

1. Go to this page and download the library: Download juliangut/mapping library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

juliangut / mapping example snippets




use Jgut\Mapping\Driver\AbstractMappingDriver;
use Jgut\Mapping\Driver\Traits\PhpMappingTrait;
use Jgut\Mapping\Metadata\MetadataInterface;

class PhpDriver extends AbstractMappingDriver
{
    use PhpMappingTrait;

    public function getMetadata(): array
    {
        $mappingData = $this->getMappingData();

        // Return your parsed metadata
    }
}

$driver = new PhpDriver(['path/to/classes']);

$driver->getMetadata();

use Jgut\Mapping\Driver\AbstractClassDriver;
use Jgut\Mapping\Metadata\MetadataInterface;

class AttributeDriver extends AbstractClassDriver
{
    public function getMetadata(): array
    {
        $mappingClasses = $this->getMappingClasses();

        // Parse class attributes with PHP Reflection

        // Return your parsed metadata
    }
}

$driver = new AttributeDriver(['path/to/classes']);

$driver->getMetadata();

use Doctrine\Common\Annotations\AnnotationReader;
use Jgut\Mapping\Driver\AbstractAnnotationDriver;
use Jgut\Mapping\Metadata\MetadataInterface;

class AnnotationDriver extends AbstractAnnotationDriver
{
    public function getMetadata(): array
    {
        $mappingClasses = $this->getMappingClasses();

        // Parse class annotations. Annotation reader available on $this->annotationReader

        // Return your parsed metadata
    }
}

$driver = new AnnotationDriver(['path/to/classes'], new AnnotationReader());

$driver->getMetadata();

use Doctrine\Common\Annotations\AnnotationReader;
use Jgut\Mapping\Driver\AbstractDriverFactory;
use Jgut\Mapping\Driver\DriverInterface;

class DriverFactory extends AbstractDriverFactory
{
    protected function getPhpDriver(array $paths): DriverInterface
    {
        return new PhpDriver($paths);
    }

    protected function getAttributeDriver(array $paths): DriverInterface
    {
        return new AttributeDriver($paths);
    }

    protected function getAnnotationDriver(array $paths): DriverInterface
    {
        return new AnnotationDriver($paths, new AnnotationReader());
    }
}

use Jgut\Mapping\Driver\DriverFactoryInterface;
use Jgut\Mapping\Metadata\MetadataResolver;

$mappingSources = [
    [
        'type' => DriverFactoryInterface::DRIVER_ATTRIBUTE,
        'path' => '/path/to/mapping/files',
    ]
];

$metadataResolver = new MetadataResolver(new DriverFactory(), new PSR16Cache());

$metadata = $metadataResolver->getMetadata($mappingSources);

use Jgut\Mapping\Annotation\AbstractAnnotation;

/**
 * @Annotation
 *
 * @Target("CLASS")
 */
class Event extends AbstractAnnotation
{
    protected string $event;

    protected bool $enabled;

    public function setEvent(string $event): void
    {
        $this->event = $event;
    }

    public function setEnabled(bool $enabled): void
    {
        $this->enabled = $enabled;
    }

    protected function getDefaultProperty(): ?string
    {
        return 'event';
    }
}

/**
 * @Event("post_deserialize", enabled=true)
 */
class Example
{
}