PHP code example of lneicelis / transformer

1. Go to this page and download the library: Download lneicelis/transformer 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/ */

    

lneicelis / transformer example snippets


$transformerRegistry = new TransformerRegistry();
$transformer = new Transformer([
    new TransformPipe($transformerRegistry),
]);

class DateTimeTransformer implements CanTransform
{
    public function getReourceClass(): string
    {
        return DateTime::class;
    }

    /**
     * @param DateTime $resource
     * @return string
     */
    public function transform($resource)
    {
        return $resource->format(DateTime::ISO8601);
    }
}

$dateTimeTransformer = new DateTimeTransformer();
$transformerRegistry->addTransformer($dateTimeTransformer);

$resource = new DateTime('2000-10-10 12:00:00');
echo $transformer->transform($resource); 

// outputs:
"2000-10-10T12:00:00+0000"

 class DateTimeTransformer implements CanTransform
 {
     public function getReourceClass(): string
     {
         return DateTime::class;
     }
 
     /**
      * @param DateTime $resource
      * @return array
      */
     public function transform($resource)
     {
         return [
             'iso' => $resource->format(DateTime::ISO8601),
             'timezone' => $resource->getTimezone(),
         ];
     }
 }
 
 class DateTimeZoneTransformer implements CanTransform
 {
     public function getResourceClass(): string
     {
         return DateTimeZone::class;
     }
 
     /**
      * @param DateTimeZone $resource
      * @return string
      */
     public function transform($resource)
     {
         return $resource->getName();
     }
 }
 
 $transformerRegistry = new TransformerRegistry();
 $transformer = new Transformer([
     new TransformPipe($transformerRegistry),
     new LazyPropertiesPipe($transformerRegistry),
 ]);
 
 $transformerRegistry->addTransformer(new DateTimeTransformer());
 $transformerRegistry->addTransformer(new DateTimeZoneTransformer());
 
 $resource = new DateTime('2000-10-10 12:00:00', new DateTimeZone('-0400'));
 
 $data = $transformer->transform($resource);
 var_dump($data);

// Outputs:
array(2) {
  ["iso"]=>string(24) "2000-10-10T12:00:00-0400"
  ["timezone"]=>string(6) "-04:00"
}

 class DateTimeTransformer implements CanTransform, HasLazyProperties
 {
     public function getResourceClass(): string
     {
         return DateTime::class;
     }
 
     /**
      * @param DateTime $resource
      * @return array
      */
     public function transform($resource): array
     {
         return [
            'iso' => $resource->format(DateTime::ISO8601),
         ];
     }
     
     public function timestamp(DateTime $resource): int {
        return $resource->getTimestamp();
     }
 }
 
$transformerRegistry = new TransformerRegistry();
$transformer = new Transformer([
    new TransformPipe($transformerRegistry),
    new LazyPropertiesPipe($transformerRegistry),
]);

$resource = new DateTime('2000-10-10 12:00:00');

$data = $transformer->transform($resource);
var_dump($data);

// Outputs:
array(1) {
  ["iso"] => string(24) "2000-10-10T12:00:00+0000"
}

$schema = [
    'timestamp',
];
$data = $transformer->transform($resource, new Context($schema);
var_dump($data);

//Outputs:
array(2) {
  ["iso"]=>string(24) "2000-10-10T12:00:00+0000"
  ["timestamp"]=>int(971179200)
}

 

class OwnerOnly implements CanGuard {

    public function getName(): string
    {
        return self::class;
    }

    public function canAccess($resource, Context $context): bool
    {
        return false;
    }
}

class DateTimeTransformer implements CanTransform, HasLazyProperties, HasAccessConfig
{
    public function getResourceClass(): string
    {
        return DateTime::class;
    }

    public function getAccessConfig(): AccessConfig
    {
        return new AccessConfig([], [
            'timestamp' => [OwnerOnly::class],
        ]);
    }

    /**
     * @param DateTime $resource
     * @return array
     */
    public function transform($resource): array
    {
        return [
            'iso' => $resource->format(DateTime::ISO8601),
        ];
    }

    public function timestamp(DateTime $resource): int {
        return $resource->getTimestamp();
    }
}

$transformerRegistry = new TransformerRegistry();
$transformer = new Transformer([
    new AccessControlPipe($transformerRegistry, [new OwnerOnly()]),
    new TransformPipe($transformerRegistry),
    new LazyPropertiesPipe($transformerRegistry),
]);

$transformerRegistry->addTransformer(new DateTimeTransformer());

$resource = new DateTime('2000-10-10 12:00:00');

$data = $transformer->transform($resource);

var_dump($data);
// Outputs:
array(1) {
  ["iso"]=>string(24) "2000-10-10T12:00:00+0000"
}

$schema = [
    'timestamp',
];
$data = $transformer->transform($resource, new Context($schema));

// throws Lneicelis\Transformer\Exception\AccessDeniedException