Last active
December 10, 2015 23:55
-
-
Save romainneutron/4512101 to your computer and use it in GitHub Desktop.
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
[ 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 |
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
[ romain @MacBook : /tmp/test-serializer ] php test.php | |
{"path":"photo01.JPG","width":3264,"height":2448} |
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
<?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