vendor/jms/metadata/src/ClassMetadata.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Metadata;
  4. /**
  5.  * Base class for class metadata.
  6.  *
  7.  * This class is intended to be extended to add your own application specific
  8.  * properties, and flags.
  9.  *
  10.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  11.  */
  12. class ClassMetadata implements \Serializable
  13. {
  14.     /**
  15.      * @var string
  16.      */
  17.     public $name;
  18.     /**
  19.      * @var MethodMetadata[]
  20.      */
  21.     public $methodMetadata = [];
  22.     /**
  23.      * @var PropertyMetadata[]
  24.      */
  25.     public $propertyMetadata = [];
  26.     /**
  27.      * @var string[]
  28.      */
  29.     public $fileResources = [];
  30.     /**
  31.      * @var int
  32.      */
  33.     public $createdAt;
  34.     public function __construct(string $name)
  35.     {
  36.         $this->name $name;
  37.         $this->createdAt time();
  38.     }
  39.     public function addMethodMetadata(MethodMetadata $metadata): void
  40.     {
  41.         $this->methodMetadata[$metadata->name] = $metadata;
  42.     }
  43.     public function addPropertyMetadata(PropertyMetadata $metadata): void
  44.     {
  45.         $this->propertyMetadata[$metadata->name] = $metadata;
  46.     }
  47.     public function isFresh(?int $timestamp null): bool
  48.     {
  49.         if (null === $timestamp) {
  50.             $timestamp $this->createdAt;
  51.         }
  52.         foreach ($this->fileResources as $filepath) {
  53.             if (!file_exists($filepath)) {
  54.                 return false;
  55.             }
  56.             if ($timestamp filemtime($filepath)) {
  57.                 return false;
  58.             }
  59.         }
  60.         return true;
  61.     }
  62.     /**
  63.      * @return string
  64.      *
  65.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
  66.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
  67.      */
  68.     public function serialize()
  69.     {
  70.         return serialize([
  71.             $this->name,
  72.             $this->methodMetadata,
  73.             $this->propertyMetadata,
  74.             $this->fileResources,
  75.             $this->createdAt,
  76.         ]);
  77.     }
  78.     /**
  79.      * @param string $str
  80.      *
  81.      * @return void
  82.      *
  83.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
  84.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
  85.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
  86.      */
  87.     public function unserialize($str)
  88.     {
  89.         [
  90.             $this->name,
  91.             $this->methodMetadata,
  92.             $this->propertyMetadata,
  93.             $this->fileResources,
  94.             $this->createdAt,
  95.         ] = unserialize($str);
  96.     }
  97. }