Skip to content

Instantly share code, notes, and snippets.

@romainneutron
Last active December 10, 2015 23:55
Show Gist options
  • Save romainneutron/4512101 to your computer and use it in GitHub Desktop.
Save romainneutron/4512101 to your computer and use it in GitHub Desktop.
[ romain @MacBook : /tmp/test-serializer ] php test.php
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'There is neither a public getProbe method, nor a public isProbe method in class Media. Please specify which public method should be used for retrieving the value of the property probe.' in /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/PropertyMetadata.php:60
Stack trace:
#0 /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php(188): JMS\Serializer\Metadata\PropertyMetadata->setAccessor('public_method', NULL, NULL)
#1 /private/tmp/test-serializer/vendor/jms/metadata/src/Metadata/MetadataFactory.php(77): JMS\Serializer\Metadata\Driver\AnnotationDriver->loadMetadataForClass(Object(ReflectionClass))
#2 /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php(177): Metadata\MetadataFactory->getMetadataForClass('Media')
#3 /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Serializer.php(125): JMS\Serializer\GraphNavigator->accep in /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/PropertyMetadata.php on line 60
Fatal error: Uncaught exception 'RuntimeException' with message 'There is neither a public getProbe method, nor a public isProbe method in class Media. Please specify which public method should be used for retrieving the value of the property probe.' in /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/PropertyMetadata.php on line 60
RuntimeException: There is neither a public getProbe method, nor a public isProbe method in class Media. Please specify which public method should be used for retrieving the value of the property probe. in /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/PropertyMetadata.php on line 60
Call Stack:
0.0005 286512 1. {main}() /private/tmp/test-serializer/test.php:0
0.0167 2135488 2. JMS\Serializer\Serializer->serialize() /private/tmp/test-serializer/test.php:93
0.0173 2230496 3. JMS\Serializer\GraphNavigator->accept() /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Serializer.php:125
0.0176 2255304 4. Metadata\MetadataFactory->getMetadataForClass() /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:177
0.0177 2258320 5. JMS\Serializer\Metadata\Driver\AnnotationDriver->loadMetadataForClass() /private/tmp/test-serializer/vendor/jms/metadata/src/Metadata/MetadataFactory.php:77
0.0263 2559824 6. JMS\Serializer\Metadata\PropertyMetadata->setAccessor() /private/tmp/test-serializer/vendor/jms/serializer/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php:188
[ romain @MacBook : /tmp/test-serializer ] php test.php
{"path":"photo01.JPG","width":3264,"height":2448}
<?php
use JMS\Serializer\Annotation\AccessType;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Expose;
use Doctrine\Common\Annotations\AnnotationRegistry;
include 'vendor/autoload.php';
AnnotationRegistry::registerAutoloadNamespace(
'JMS\Serializer\Annotation', __DIR__.'/vendor/jms/serializer/src'
);
$serializer = JMS\Serializer\SerializerBuilder::create()
->setCacheDir(__DIR__ . '/cache')
->setDebug(true)
->build();
/**
* @ExclusionPolicy("all")
* @AccessType("public_method")
*/
class Media
{
/**
* @Exclude
*/
private $probe;
/**
* @Expose
* @Readonly
*/
private $path;
/**
* @Expose
* @Readonly
*/
private $width;
/**
* @Expose
* @Readonly
*/
private $height;
public function __construct($probe, $path)
{
$this->path = $path;
$this->probe = $probe;
}
public function getPath()
{
return $this->path;
}
public function getWidth()
{
return $this->probe->execute('width', $this->path);
}
public function getHeight()
{
return $this->probe->execute('height', $this->path);
}
public static function create($path)
{
return new static(new Probe(), $path);
}
}
class Probe
{
public function execute($what, $path)
{
$dim = getimagesize($path);
switch ($what) {
case 'width':
return $dim[0];
break;
case 'height':
return $dim[1];
break;
default:
throw new \RuntimeException(sprintf('Unknown %s parameter', $what));
}
}
}
print($serializer->serialize(Media::create('photo01.JPG'), 'json')) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment