These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace AppBundle\Repository; |
||
4 | |||
5 | use Symfony\Component\Finder\Finder; |
||
6 | use AppBundle\Entity\Post; |
||
7 | use Mni\FrontYAML\Parser; |
||
8 | |||
9 | class PostRepository |
||
10 | { |
||
11 | /** |
||
12 | * PostRepository constructor. |
||
13 | * |
||
14 | * @param string $kernelRootDir |
||
15 | */ |
||
16 | public function __construct($kernelRootDir) |
||
17 | { |
||
18 | $this->path = $kernelRootDir; |
||
0 ignored issues
–
show
|
|||
19 | } |
||
20 | |||
21 | public function findAll() |
||
22 | { |
||
23 | $finder = new Finder(); |
||
24 | $finder->files()->in($this->path.'/Resources/content/blog'); |
||
25 | $finder->files()->name('*.md'); |
||
26 | $finder->sort(function ($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); }); |
||
27 | |||
28 | $posts = []; |
||
29 | foreach ($finder as $file) { |
||
30 | $post = $this->getPostByFile($file->getRealPath()); |
||
31 | $posts[] = $post; |
||
32 | } |
||
33 | |||
34 | return $posts; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Find one Post by parameters. |
||
39 | * |
||
40 | * @param array $params Search parameters. |
||
41 | * @return null|Post |
||
42 | */ |
||
43 | public function findOneBy(array $params) { |
||
44 | $finder = new Finder(); |
||
45 | $fileName = $params['year'].'-'.$params['month'].'-'.$params['day'].($params['num'] === 0 ? '' : '-'.$params['num']).'.md'; |
||
46 | $finder->files()->in($this->path.'/Resources/content/blog')->name($fileName); |
||
47 | $iterator = $finder->getIterator(); |
||
48 | $iterator->rewind(); |
||
49 | $file = $iterator->current(); |
||
50 | |||
51 | $post = $this->getPostByFile($file->getRealPath()); |
||
52 | |||
53 | if ($finder->count() == 0 || $params['slug'] != $post->getSlug()) { |
||
54 | return null; |
||
55 | } |
||
56 | |||
57 | return $post; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Find limited latest posts. |
||
62 | * |
||
63 | * @param int $limit |
||
64 | * @return null|array |
||
65 | */ |
||
66 | public function findLatest($limit = 10) |
||
67 | { |
||
68 | $finder = new Finder(); |
||
69 | $finder->files()->in($this->path.'/Resources/content/blog'); |
||
70 | $finder->files()->name('*.md'); |
||
71 | $finder->sort(function ($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); }); |
||
72 | |||
73 | $posts = []; |
||
74 | foreach (new \LimitIterator($finder->getIterator(), 0, $limit) as $file) { |
||
75 | $post = $this->getPostByFile($file->getRealPath()); |
||
76 | $posts[] = $post; |
||
77 | } |
||
78 | |||
79 | return $posts; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param string $file |
||
84 | * @return Post|null |
||
85 | */ |
||
86 | public function getPostByFile($file) |
||
87 | { |
||
88 | if (!file_exists($file)) { |
||
89 | return null; |
||
90 | } |
||
91 | |||
92 | $parser = new Parser(); |
||
93 | $document = $parser->parse(file_get_contents($file)); |
||
94 | |||
95 | $post = new Post(); |
||
96 | $post->setTitle($document->getYaml()['title']); |
||
97 | $post->setReadTime($document->getYaml()['read_time']); |
||
98 | $post->setIntro($document->getYaml()['intro']); |
||
99 | $post->setCreated(substr(basename($file), 0, 10)); |
||
100 | $post->setUpdated($document->getYAML()['updated']); |
||
101 | $post->setNum(substr(basename($file), 11, -3)); |
||
102 | $post->setSlug($document->getYaml()['slug']); |
||
103 | $post->setContent($document->getContent()); |
||
104 | $post->setFile(basename($file)); |
||
105 | |||
106 | return $post; |
||
107 | } |
||
108 | } |
||
109 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: